Spire.PDF for .NET【文档操作】演示:查找并删除 PDF 中的空白页

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 等格式**(qun:767755948 )**

Spire.PDF for.net下载 Spire.PDF for java下载

安装适用于 .NET 的 Spire.PDF

首先,您需要将 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()**方法保存结果文档。

[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 中的空白页,如果您有其他问题也可以继续浏览本系列文章,获取相关教程~

相关推荐
pass_port_csdn26 分钟前
zotero搬家,迁移换机/重装100% 完美克隆指南:文献PDF、插件配置、文献分类、标签、笔记等所有信息全克隆
笔记·pdf·zotero
开开心心_Every27 分钟前
电脑定时休息软件:久坐提醒养成活动习惯
游戏·微信·pdf·excel·语音识别·散列表·启发式算法
vlln32 分钟前
【调研报告】PDF解析技术现状与趋势:从人类阅读到大模型适配的需求
pdf
PfCoder33 分钟前
WinForm真入门(22)---定时器控件System.Windows.Forms.Timer
windows·c#·.net·timer
aiyo_1 小时前
深入浅出DOTNET技术原理(二)coreclr和hostfxr剖析
.net·.net core
软件资深者12 小时前
全能图片缩略图显示工具,体积较大,直接显示AI,PSD,EPS,PDF,INDD,TIFF,CR2,RAW等格式缩略图的图像解码包
windows·microsoft·pdf·windows11·系统修复
DS随心转小程序18 小时前
AI公式不乱码
人工智能·pdf·deepseek·ds随心转
luyun02020218 小时前
PDF神仙工具,批量处理
windows·pdf·figma
s石有八九19 小时前
PDF/文档LLM作业批改:2025年研究现状、技术进展与研究空白
人工智能·python·pdf·教育·ai教育·作业批改
开开心心_Every20 小时前
PDF转图片工具推荐:免费支持批量转换
linux·运维·服务器·spring boot·edge·pdf·powerpoint