C# 添加、删除PDF空白页
PDF格式的文档广泛应用于电子图书、产品说明、公司文告、网络资料、电子邮件等各种领域和场合。我们工作和学习中可能会遇到需要添加文档页来添加相应内容,或者删除文档中多出的空白页等情况,因此,本篇文章将介绍如何在C#中来实现添加、删除PDF空白页。
示例要点
1.添加PDF空白页
1.1 在默认位置添加空白页(文档末)
1.2 在指定位置添加空白页
2.删除空白页
工具使用
这里示例中使用的是免费版的.NET PDF控件 Free Spire.PDF for .NET(添加dll引用时,可在安装路径下的Bin文件夹中获取dll文件)
测试文档截图(文档包含两页内容):
1.添加PDF空白页1.1 在默认位置,即文档末插入一张空白页
C#
//创建PDF文档1,并加载测试文档 PdfDocument doc1 = new PdfDocument(); doc1.LoadFromFile("sample.pdf"); //添加一页空白页到文档(默认在文档最后一页添加) doc1.Pages.Add(); //保存并打开文档 doc1.SaveToFile("result1.pdf"); System.Diagnostics.Process.Start("result1.pdf");
测试结果:
C#
//创建文档2,加载测试文档 PdfDocument doc2 = new PdfDocument(); doc2.LoadFromFile("sample.pdf"); //添加一页空白页作为第2页 doc2.Pages.Insert(1); //保存并打开文档 doc2.SaveToFile("result2.pdf"); System.Diagnostics.Process.Start("result2.pdf");
测试结果:
2. 删除PDF空白页
测试文档:
这里的测试文档中,包含了两页空白页,一页空白页是没有任何内容的;另一页空白页是包含了空白图片的页面,看似没有内容,但是这样的页面实际上也是不需要的。
C#
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.PdfImports System.DrawingImports Spire.Pdf.GraphicsNamespace 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 ClassEnd Namespace
以上全部内容为本次关于“C# 添加、删除PDF空白页”的全部内容,欢迎转载(转载请注明出处)
感谢阅读!
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。