查找并刪除 PDF 中的空白頁
Spire.PDF for .NET 是一款專門對 Word 文檔進行操作的 .NET 類庫。致力于在于幫助開發人員輕松快捷高效地創建、編輯、轉換和打印 Microsoft Word 文檔,而無需安裝 Microsoft Word。
行號用于在每行文本旁邊顯示 Word 自動計算的行數。當我們需要參考合同或法律文件等文檔中的特定行時,它非常有用。word中的行號功能允許我們設置起始值、編號間隔、與文本的距離以及行號的編號方式。使用 Spire.Doc,我們可以實現上述所有功能。本文將介紹如何將 HTML 轉換為 PDF。
歡迎加入spire技術交流群:767755948
PDF 文件中的空白頁并不少見,因為它們可能是作者有意留下的,也可能是在處理文檔時不小心添加的。在閱讀或打印文檔時,這些空白頁可能會很煩人,因此很有必要刪除它們。在本文中,您將學習如何使用 Spire.PDF for .NET 以編程方式查找并刪除 PDF 文檔中的空白頁。
安裝 Spire.PDF for .NET
首先,您需要將 Spire.PDF for.NET 軟件包中包含的 DLL 文件作為引用添加到您的 .NET 項目中。這些 DLL 文件既可以從這個鏈接下載,也可以通過 NuGet 安裝。
PM> Install-Package Spire.PDF
查找并刪除 PDF 文檔中的空白頁
Spire.PDF for .NET提供了一個PdfPageBase.IsBlank()方法來檢測PDF頁面是否絕對空白。但有些看起來空白的頁面實際上包含白色圖像,使用 PdfPageBase.IsBlank() 方法無法將這些頁面視為空白頁面。因此,有必要創建一個自定義方法IsImageBlank()與PdfPageBase.IsBlank()方法結合使用,以檢測這些白色但非空白的頁面。
注:該解決方案將把 PDF 頁面轉換為圖像,并檢測圖像是否為空白。在轉換后的圖像中,有必要應用許可證來刪除評估信息。否則,此方法將無法正常工作。如果您沒有許可證,請聯系 sales@e-iceblue.com 獲取臨時許可證,用于評估目的。
具體步驟如下:
- 創建一個 PdfDocument 實例。
- 使用 PdfDocument.LoadFromFile() 方法加載一個 PDF 文檔。
- 使用PdfPageBase.IsBlank()方法循環瀏覽PDF文檔中的頁面,檢測頁面是否空白。
- 使用PdfDocument.Pages.RemoveAt()方法刪除絕對空白的頁面。
- 對于非絕對空白的頁面,使用PdfDocument.SaveAsImage()方法將其保存為圖像。然后使用自定義方法IsImageBlank()檢測轉換后的圖像是否空白,并使用PdfDocument.Pages.RemoveAt()方法刪除 "空白 "頁面。
- 使用PdfDocument.SaveToFile()方法保存結果文檔。
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

申請臨時許可證
若想從生成的文檔中刪除評估信息,或解除功能限制,申請 30 天試用許可證。