C#实现查找并删除PDF中的空白页面
作者:Eiceblue
PDF 文件中的空白页并不少见,因为它们有可能是作者有意留下的,也有可能是在处理文档时不小心添加的,下面我们来看看如何使用 Spire.PDF for .NET 通过 C# 查找和并删除 PDF 文档中的空白页吧
PDF 文件中的空白页并不少见,因为它们有可能是作者有意留下的,也有可能是在处理文档时不小心添加的。在阅读或打印文档时,这些空白页可能会比较麻烦,因此很有必要删除它们。在本文中,您将学习如何使用 Spire.PDF for .NET 通过 C# 查找和并删除 PDF 文档中的空白页。
安装 Spire.PDF for .NET
首先,您需要添加 Spire.PDF for .NET 包中包含的 DLL 文件作为 .NET 项目中的引用。
PM> Install-Package Spire.PDF
C# 查找并删除 PDF 文档中的空白页
Spire.PDF for .NET 提供了 PdfPageBase.IsBlank() 方法来检测 PDF 页面是否为绝对空白。但有些页面看起来是空白的,但实际上包含了白色 图像,使用 PdfPageBase.IsBlank() 方法并无法将这些页面视为空白页面。因此,需要创建一个自定义方法 IsImageBlank() 来检测这些白色但非空白的页面。
具体步骤如下:
- 创建 PdfDocument 对象。
- 使用 PdfDocument.LoadFromFile() 方法加载 PDF 文件。
- 遍历 PDF 文档中的页面,并使用 PdfPageBase.IsBlank() 方法检测页面是否为空白页面。
- 使用 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)
{
// 应用授权
Spire.Pdf.License.LicenseProvider.SetLicenseKey("your license key");
// 创建 PdfDocument 对象
PdfDocument document = new PdfDocument();
// 加载 PDF 文档
document.LoadFromFile("汇报.pdf");
// 遍历PDF中的页面
for (int i = document.Pages.Count - 1; i >= 0; i--)
{
// 检测页面是否为空白
if (document.Pages[i].IsBlank())
{
// 删除绝对空白页
document.Pages.RemoveAt(i);
}
else
{
// 将 PDF 页面保存为图像
Image image = document.SaveAsImage(i, PdfImageType.Bitmap);
// 检测转换后的图像是否为空白
if (IsImageBlank(image))
{
// 如果是,则删除页面
document.Pages.RemoveAt(i);
}
}
}
// 保存结果文档
document.SaveToFile("删除空白页.pdf", FileFormat.PDF);
}
// 检测图像是否为空白
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;
}
}
}效果如下

C# 添加与删除PDF空白页
工具使用
这里示例中使用的是免费版的.NET PDF控件 Free Spire.PDF for .NET(添加dll引用时,可在安装路径下的Bin文件夹中获取dll文件)
测试文档截图(文档包含两页内容):

添加PDF空白页
1.在默认位置,即文档末插入一张空白页
//创建PDF文档1,并加载测试文档
PdfDocument doc1 = new PdfDocument();
doc1.LoadFromFile("sample.pdf");
//添加一页空白页到文档(默认在文档最后一页添加)
doc1.Pages.Add();
//保存并打开文档
doc1.SaveToFile("result1.pdf");
System.Diagnostics.Process.Start("result1.pdf");测试结果:

2.在指定位置插入空白页
//创建文档2,加载测试文档
PdfDocument doc2 = new PdfDocument();
doc2.LoadFromFile("sample.pdf");
//添加一页空白页作为第2页
doc2.Pages.Insert(1);
//保存并打开文档
doc2.SaveToFile("result2.pdf");
System.Diagnostics.Process.Start("result2.pdf");测试结果:

删除PDF空白页
测试文档:

这里的测试文档中,包含了两页空白页,一页空白页是没有任何内容的;另一页空白页是包含了空白图片的页面,看似没有内容,但是这样的页面实际上也是不需要的。
using Spire.Pdf;
using System.Drawing;
using Spire.Pdf.Graphics;
namespace DeleteBlankPage_PDF
{
class Program
{
static void Main(string[] args)
{
//应用许可证
Spire.License.LicenseProvider.SetLicenseFileName("license.elic.xml");
//创建PdfDocument类对象,并加载PDF文档
PdfDocument document = new PdfDocument();
document.LoadFromFile("Test.pdf");
//遍历文档中所有页面
for (int i = document.Pages.Count - 1; i >= 0; i--)
{
//诊断页面是否为空白页
if (document.Pages[i].IsBlank())
{
//删除空白页
document.Pages.RemoveAt(i);
}
else
{
//将PDF页转换为Bitmap图像
Image image = document.SaveAsImage(i, PdfImageType.Bitmap);
//诊断图片是否为空白图片
if (IsImageBlank(image))
{
//移除包含空白图片的页面
document.Pages.RemoveAt(i);
}
}
}
//保存并打开文档
document.SaveToFile("RemoveBlankPage.pdf", FileFormat.PDF);
System.Diagnostics.Process.Start("RemoveBlankPage.pdf");
}
//自定义方法IsImageBlank()诊断图片是否为空白图片
public static bool IsImageBlank(Image image)
{
//初始化Bitmap类实例,遍历文档中所有图片
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代码(删除PDF空白页)
Imports Spire.Pdf
Imports System.Drawing
Imports Spire.Pdf.Graphics
Namespace DeleteBlankPage_PDF
Class Program
Private Shared Sub Main(ByVal args As String())
Spire.License.LicenseProvider.SetLicenseFileName("license.elic.xml")
Dim document As PdfDocument = New PdfDocument()
document.LoadFromFile("Test.pdf")
For i As Integer = document.Pages.Count - 1 To 0
If document.Pages(i).IsBlank() Then
document.Pages.RemoveAt(i)
Else
Dim image As Image = document.SaveAsImage(i, PdfImageType.Bitmap)
If IsImageBlank(image) Then
document.Pages.RemoveAt(i)
End If
End If
Next
document.SaveToFile("RemoveBlankPage.pdf", FileFormat.PDF)
System.Diagnostics.Process.Start("RemoveBlankPage.pdf")
End Sub
Public Shared Function IsImageBlank(ByVal image As Image) As Boolean
Dim bitmap As Bitmap = New Bitmap(image)
For i As Integer = 0 To bitmap.Width - 1
For j As Integer = 0 To bitmap.Height - 1
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
Next
Next
Return True
End Function
End Class
End Namespace到此这篇关于C#实现查找并删除PDF中的空白页面的文章就介绍到这了,更多相关C#查找与删除PDF空白页内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
