翻譯|行業資訊|編輯:胡濤|2024-03-26 10:55:53.903|閱讀 106 次
概述:在本文中,您將了解如何使用Spire.PDF for .NET以編程方式查找和刪除 PDF 文檔中的空白頁。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關鏈接:
PDF 中的空白頁并不罕見,因為它們可能是作者故意留下的或在操作文檔時意外添加的。當您閱讀或打印文檔時,這些空白頁可能會很煩人,因此可能非常有必要將其刪除。在本文中,您將了解如何使用Spire.PDF for .NET以編程方式查找和刪除 PDF 文檔中的空白頁。
Spire.PDF for .NET 是一款獨立 PDF 控件,用于 .NET 程序中創建、編輯和操作 PDF 文檔。使用 Spire.PDF 類庫,開發人員可以新建一個 PDF 文檔或者對現有的 PDF 文檔進行處理,且無需安裝 Adobe Acrobat。
E-iceblue 功能類庫Spire 系列文檔處理組件均由中國本土團隊研發,不依賴第三方軟件,不受其他國家的技術或法律法規限制,同時適配國產操作系統如中科方德、中標麒麟等,兼容國產文檔處理軟件 WPS(如 .wps/.et/.dps 等格式
Spire.PDF for.net下載 Spire.PDF for java下載
首先,您需要將 Spire.PDF for .NET 包中包含的 DLL 文件添加為 .NET 項目中的引用。DLL 文件可以從此鏈接下載或通過NuGet安裝。
PM> Install-Package Spire.PDF
Spire.PDF for .NET 提供了方法PdfPageBase.IsBlank()來檢測 PDF 頁面是否絕對空白。但有些看起來空白的頁面實際上包含白色圖像,使用PdfPageBase.IsBlank()方法不會將這些頁面視為空白。因此,有必要創建一個自定義方法IsImageBlank()與PdfPageBase.IsBlank()方法結合使用來檢測這些白色但非空白的頁面。
注意:此解決方案會將 PDF 頁面轉換為圖像并檢測圖像是否為空白。需要申請許可證才能刪除轉換圖像中的評估消息。否則,該方法將無法正常工作。如果您沒有許可證,請聯系sales@e-iceblue.com獲取臨時許可證以進行評估。
詳細步驟如下:
[C#]
using Spire.Pdf; using Spire.Pdf.Graphics; using System.Drawing; namespace DeleteBlankPage { class Program { static void Main(string[] args) { //Apply license by license key Spire.License.LicenseProvider.SetLicenseKey("your license key"); //Create a PdfDocument instance PdfDocument document = new PdfDocument(); //Load a sample PDF document document.LoadFromFile("input.pdf"); //Loop through all pages in the PDF for (int i = document.Pages.Count - 1; i >= 0; i--) { //Detect if a page is blank if (document.Pages[i].IsBlank()) { //Remove the absolutely blank page document.Pages.RemoveAt(i); } else { //Save PDF page as image Image image = document.SaveAsImage(i, PdfImageType.Bitmap); //Detect if the converted image is blank if (IsImageBlank(image)) { //Remove the page document.Pages.RemoveAt(i); } } } //Save the result document document.SaveToFile("RemoveBlankPage.pdf", FileFormat.PDF); } //Detect if an image is blank public static bool IsImageBlank(Image image) { Bitmap bitmap = new Bitmap(image); for (int i = 0; i < bitmap.Width; i++) { for (int j = 0; j < bitmap.Height; j++) { Color pixel = bitmap.GetPixel(i, j); if (pixel.R < 240 || pixel.G < 240 || pixel.B < 240) { return false; } } } return true; } } }
【VB.NET】
Imports Spire.Pdf Imports Spire.Pdf.Graphics Namespace DeleteBlankPage Class Program Private Shared Sub Main(ByVal args() As String) 'Apply license by license key Spire.License.LicenseProvider.SetLicenseKey("your license key") 'Create a PdfDocument instance Dim document As PdfDocument = New PdfDocument 'Load a sample PDF document document.LoadFromFile("input.pdf") 'Loop through all pages in the PDF Dim i As Integer = (document.Pages.Count - 1) Do While (i >= 0) 'Detect if a page is blank If document.Pages(i).IsBlank Then 'Remove the absolutely blank page document.Pages.RemoveAt(i) Else 'Save PDF page as image Dim image As Image = document.SaveAsImage(i, PdfImageType.Bitmap) 'Detect if the converted image is blank If Program.IsImageBlank(image) Then 'Remove the page document.Pages.RemoveAt(i) End If End If i = (i - 1) Loop 'Save the result document document.SaveToFile("RemoveBlankPage.pdf", FileFormat.PDF) End Sub 'Detect if an image is blank Public Shared Function IsImageBlank(ByVal image As Image) As Boolean Dim bitmap As Bitmap = New Bitmap(image) Dim i As Integer = 0 Do While (i < bitmap.Width) Dim j As Integer = 0 Do While (j < bitmap.Height) Dim pixel As Color = bitmap.GetPixel(i, j) If ((pixel.R < 240) _ OrElse ((pixel.G < 240) _ OrElse (pixel.B < 240))) Then Return False End If j = (j + 1) Loop i = (i + 1) Loop Return True End Function End Class End Namespace
以上便是如何查找并刪除 PDF 中的空白頁,如果您有其他問題也可以繼續瀏覽本系列文章,獲取相關教程,你還可以給我留言或者加入我們的官方技術交流群。
歡迎下載|體驗更多E-iceblue產品
獲取更多信息請咨詢 ;技術交流Q群(767755948)
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn