Articles in this section
Category / Section

How to delete blank pages from an existing PDF document?

6 mins read

The Syncfusion Essential® PDF is a .NET PDF library used to create, read, and edit PDF documents. Using this library, you can delete blank pages from an existing PDF document.

Steps to delete blank pages from an existing PDF document

  1. Create a new Windows Forms application project.

Create Windows Forms application

  1. Install the Syncfusion.Pdf.WinForms NuGet package as a reference to your .NET Framework application from nuget.org.

NuGet package reference

  1. Include the following namespaces in the Form1.cs file.

C#

using Syncfusion.Pdf;
using Syncfusion.Pdf.Parsing;
using System.Drawing;

 

VB.NET

Imports Syncfusion.Pdf
Imports Syncfusion.Pdf.Parsing
Imports System.Drawing

 

  1. Add the following code in button1_Click to delete blank pages from an existing PDF document.

C#

//Load PDF document 
PdfLoadedDocument lDoc = new PdfLoadedDocument("../../Data/Blank.pdf");
 
//Disable the incremental update
lDoc.FileStructure.IncrementalUpdate = false;
 
//Set the cross reference type 
lDoc.FileStructure.CrossReferenceType = PdfCrossReferenceType.CrossReferenceStream;
 
int i = 0;
 
while (i < lDoc.Pages.Count)
{
    //Extract image from the loaded page 
    Image[] img = lDoc.Pages[i].ExtractImages();
 
    //Extract text from the existing page
    string text = lDoc.Pages[i].ExtractText();
 
    //If extracted images and string are empty, then remove the blank page 
    if (img.Length == 0 && string.IsNullOrEmpty(text))
        lDoc.Pages.RemoveAt(i);
 
    i += 1;
}
 
//Save the PDF document 
lDoc.Save("Output.pdf");
 
//Closes the document 
lDoc.Close(true);
 
//This will open the PDF file and the result will be seen in the default PDF Viewer
Process.Start("Output.pdf");

 

VB.NET

'Load PDF document 
Dim lDoc As New PdfLoadedDocument("../../Data/Blank.pdf")
 
'Disable the incremental update
lDoc.FileStructure.IncrementalUpdate = False
 
'Set the cross reference type 
lDoc.FileStructure.CrossReferenceType = PdfCrossReferenceType.CrossReferenceStream
 
Dim i As Integer = 0
 
Do While i < lDoc.Pages.Count
 
    'Extract image from the loaded page  
    Dim img() As Image = lDoc.Pages(i).ExtractImages()
 
    'Extract text from the existing page 
    Dim text As String = lDoc.Pages(i).ExtractText()
 
    'If extracted images and string are empty, then remove the blank page  
    If img.Length = 0 AndAlso String.IsNullOrEmpty(text) Then
        lDoc.Pages.RemoveAt(i)
    End If
 
    i += 1
 
Loop
 
'Save the PDF document  
lDoc.Save("Sample.pdf")
 
'Close the document 
lDoc.Close(True)
 
'This will open the PDF file and the result will be seen in the default PDF Viewer
Process.Start("Output.pdf")

 

A complete working sample can be downloaded from DeleteBlankPage.zip.

 

By executing the program, you will get the output document as follows,

Output document reference

 

Take a moment to peruse the documentation. You can find more about Text extraction and Image extraction. Also, find the other options like drawing right-to-left text, multi-column text, consuming true type fonts, standard fonts, CJK fonts, and more.

 

Refer to here to explore a rich set of Syncfusion Essential® PDF features.

 

Note:

Starting with v16.2.0.x, if you reference Syncfusion® assemblies from trial setup or the NuGet feed, include a license key in your product. Refer to the link to learn about generating and registering the Syncfusion® license key in your application to use the components without trail message.

 

See Also:

Extract text from PDF document

Extract images from PDF document

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied