翻譯|使用教程|編輯:李顯亮|2019-11-20 10:06:59.180|閱讀 508 次
概述:在接下來的系列教程中,將為開發(fā)者帶來Aspose.PDF for .NET的一系列使用教程,本文將介紹如何修剪頁面周圍的空白。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關鏈接:
Aspose.PDF for .NET是一種高PDF處理和解析API,用于在跨平臺應用程序中執(zhí)行文檔管理和操作任務。API可以輕松用于生成、修改、轉換、渲染、保護和打印PDF文檔,而無需使用Adobe Acrobat。此外,API還提供PDF壓縮選項,表格創(chuàng)建和操作,圖形和圖像功能,廣泛的超鏈接功能,印章和水印任務,擴展的安全控制和自定義字體處理。
在接下來的系列教程中,將為開發(fā)者帶來Aspose.PDF for .NET的一系列使用教程,例如進行文檔間的轉換,如何標記PDF文件,如何使用表單和圖表等等。
>>Aspose.PDF for .NET更新至最新版v19.11,歡迎下載體驗。
購買Aspose文檔系列產(chǎn)品領取優(yōu)惠券專享折上折,滿額更有iPhone 11等豪禮相送!更多活動詳情可哦~
PDF文件由文本,圖像,圖形和其他各種對象組成。要刪除或修剪PDF頁面周圍的空白CropBox,請為該特定頁面設置。要確定正確的CropBox坐標值,首先需要確定對象在頁面上的位置。
圖形基元邊界檢測不是一種可靠的方法。很可能在外觀中找不到某些對象,并提供API來獲取其矩形。更好,更可靠的方法是將PDF頁面呈現(xiàn)為圖像,然后確定頁邊距。以下代碼段顯示了如何:
// 文檔目錄的路徑 string dataDir = RunExamples.GetDataDir_AsposePdf_WorkingDocuments(); //加載現(xiàn)有的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; // 確定白色區(qū)域 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]; // 將行數(shù)據(jù)復制到字節(jié)數(shù)組 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有任何需求和疑難,記得掃描下方二維碼告訴我們哦~
本站文章除注明轉載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內(nèi)容上的異議請郵件反饋至chenjj@fc6vip.cn