Documents in PDF format are widely used in various fields and occasions such as e-books, product descriptions, company announcements, network materials, and e-mails. In our work and study, we may encounter situations where we need to add document pages to add corresponding content, or delete extra blank pages in the document. Therefore, this article will introduce how to add and delete PDF blank pages in C#.
Example gist :
1. Add PDF blank page
1.1 Add blank page in default position (end of document)
1.2 Add blank page at specified position
2. Delete blank page
Tool use :
The free version of the .NET PDF control Free Spire.PDF for .NET is used in this example (when adding a dll reference, the dll file can be obtained in the Bin folder under the installation path)
Screenshot of the test document (the document contains two pages):
1. Add PDF blank pages
1.1 Insert a blank page at the default position, that is, at the end of the document
C#
//创建PDF文档1,并加载测试文档
PdfDocument doc1 = new PdfDocument();
doc1.LoadFromFile("sample.pdf");
//添加一页空白页到文档(默认在文档最后一页添加)
doc1.Pages.Add();
//保存并打开文档
doc1.SaveToFile("result1.pdf");
System.Diagnostics.Process.Start("result1.pdf");
Test Results:
1.2 Insert a blank page at the specified position
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");
Test Results:
2. Delete PDF blank pages
Test document:
The test document here contains two blank pages, one blank page has no content; the other blank page is a page containing blank pictures, which seems to have no content, but such a page is actually not needed. of.
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;
}
}
}
Test Results:
Attached: VB.NET code (remove PDF blank pages)
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
All the above content is the whole content of "C# Add, Delete PDF Blank Page" this time, welcome to reprint (reprint please indicate the source)
Thank you for reading!