PDF管理控件Aspose.PDF for .Net使用教程(二十):修剪页面周围的空白
Aspose.PDF for .NET是一种高PDF处理和解析API,用于在跨平台应用程序中执行文档管理和操作任务。API可以轻松用于生成、修改、转换、渲染、保护和打印PDF文档,而无需使用Adobe Acrobat。此外,API还提供PDF压缩选项,表格创建和操作,图形和图像功能,广泛的超链接功能,印章和水印任务,扩展的安全控制和自定义字体处理。
在接下来的系列教程中,将为开发者带来Aspose.PDF for .NET的一系列使用教程,例如进行文档间的转换,如何标记PDF文件,如何使用表单和图表等等。
>>Aspose.PDF for .NET更新至最新版v19.11,欢迎下载体验。
购买Aspose文档系列产品领取优惠券专享折上折,满额更有iPhone 11等豪礼相送!更多活动详情可哦~
第七章:设置PDF文档格式
▲第七节:修剪页面周围的空白
PDF文件由文本,图像,图形和其他各种对象组成。要删除或修剪PDF页面周围的空白CropBox,请为该特定页面设置。要确定正确的CropBox坐标值,首先需要确定对象在页面上的位置。
图形基元边界检测不是一种可靠的方法。很可能在外观中找不到某些对象,并提供API来获取其矩形。更好,更可靠的方法是将PDF页面呈现为图像,然后确定页边距。以下代码段显示了如何:
- 将PDF页面渲染为PNG
- 确定对象的位置
- 使用这些相同的值为CropBoxPDF文件设置一个
// 文档目录的路径
string dataDir = RunExamples.GetDataDir_AsposePdf_WorkingDocuments();
//加载现有的PDF文件
Document doc = new Document(dataDir + "input.pdf");
//使用72 DPI将页面渲染为图像
PngDevice device = new PngDevice(new Resolution(72));
using (MemoryStream imageStr = new MemoryStream())
{
device.Process(doc.Pages[1], imageStr);
Bitmap bmp = (Bitmap)Bitmap.FromStream(imageStr);
System.Drawing.Imaging.BitmapData imageBitmapData = null;
// 确定白色区域
try
{
imageBitmapData = bmp.LockBits(new System.Drawing.Rectangle(0, 0, bmp.Width, bmp.Height),
System.Drawing.Imaging.ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppRgb);
Aspose.Pdf.Rectangle prevCropBox = doc.Pages[1].CropBox;
int toHeight = bmp.Height;
int toWidth = bmp.Width;
int? leftNonWhite = null;
int? rightNonWhite = null;
int? topNonWhite = null;
int? bottomNonWhite = null;
for (int y = 0; y < toHeight; y++)
{
byte[] imageRowBytes = new byte[imageBitmapData.Stride];
// 将行数据复制到字节数组
if (IntPtr.Size == 4)
System.Runtime.InteropServices.Marshal.Copy(new IntPtr(imageBitmapData.Scan0.ToInt32() + y * imageBitmapData.Stride), imageRowBytes, 0, imageBitmapData.Stride);
else
System.Runtime.InteropServices.Marshal.Copy(new IntPtr(imageBitmapData.Scan0.ToInt64() + y * imageBitmapData.Stride), imageRowBytes, 0, imageBitmapData.Stride);
int? leftNonWhite_row = null;
int? rightNonWhite_row = null;
for (int x = 0; x < toWidth; x++)
{
if (imageRowBytes[x * 4] != 255
|| imageRowBytes[x * 4 + 1] != 255
|| imageRowBytes[x * 4 + 2] != 255)
{
if (leftNonWhite_row == null)
leftNonWhite_row = x;
rightNonWhite_row = x;
}
}
if (leftNonWhite_row != null || rightNonWhite_row != null)
{
if (topNonWhite == null)
topNonWhite = y;
bottomNonWhite = y;
}
if (leftNonWhite_row != null
&& (leftNonWhite == null || leftNonWhite > leftNonWhite_row))
{
leftNonWhite = leftNonWhite_row;
}
if (rightNonWhite_row != null
&& (rightNonWhite == null || rightNonWhite < rightNonWhite_row))
{
rightNonWhite = rightNonWhite_row;
}
}
leftNonWhite = leftNonWhite ?? 0;
rightNonWhite = rightNonWhite ?? toWidth;
topNonWhite = topNonWhite ?? 0;
bottomNonWhite = bottomNonWhite ?? toHeight;
//将修正后的裁剪框设置为上一个裁剪框
doc.Pages[1].CropBox =
new Aspose.Pdf.Rectangle(
leftNonWhite.Value + prevCropBox.LLX,
(toHeight + prevCropBox.LLY) - bottomNonWhite.Value,
rightNonWhite.Value + doc.Pages[1].CropBox.LLX,
(toHeight + prevCropBox.LLY) - topNonWhite.Value
);
}
finally
{
if (imageBitmapData != null)
bmp.UnlockBits(imageBitmapData);
}
}
dataDir = dataDir + "TrimWhiteSpace_out.pdf";
// 保存更新的文档
doc.Save(dataDir);还想要更多吗?您可以点击阅读【2019 · Aspose最新资源整合】,查找需要的教程资源。如果您有任何疑问或需求,请随时加入Aspose技术交流群(642018183),我们很高兴为您提供查询和咨询。
如果您对Aspose有任何需求和疑难,记得扫描下方二维码告诉我们哦~


QQ交谈
在线咨询

渝公网安备
50010702500608号

客服热线