How to remove blank page from PDF file in WPF

2016-04-08 08:25:04 Written by  support iceblue
Rate this item
(0 votes)

Everyone knows how to open and save a PDF file. Sometimes you run into the situation that your PDF file has one or more blank pages. You want to get rid of the blank page to make you PDF file look more neat. Many people don't know how to do this.

In the following sections, I will demonstrate how to remove blank page from PDF file in WPF very easily and effortlessly.

Here is the original PDF document:

How to remove blank page from PDF file in WPF

The code snippets are as followed:

Step 1: Initialize a new instance of PdfDocument class and load the PDF document from the file.

PdfDocument document = new PdfDocument();
document.LoadFromFile("Tornado.pdf");

Step 2: Traverse the PDF file and detect the content. If the page is blank, then remove it.

for (int i = 0; i < document.Pages.Count; i++)
{
    PdfPageBase originalPage = document.Pages[i];
    if (originalPage.IsBlank())
    {
       document.Pages.Remove(originalPage); 
       i--;
     }
}

Step 3: Save the PDF and launch the file.

document.SaveToFile("Tornadowithoutblankpage.pdf", FileFormat.PDF);
System.Diagnostics.Process.Start("Tornadowithoutblankpage.pdf");

Effective screenshot:

How to remove blank page from PDF file in WPF

Full Codes:

[C#]
using System.Windows;
using Spire.Pdf;
using System.Drawing;

namespace Tornadoes
{
    /// 
    /// Interaction logic for MainWindow.xaml
    /// 
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            PdfDocument document = new PdfDocument();
            document.LoadFromFile("Tornado.pdf");
            for (int i = 0; i < document.Pages.Count; i++)
            {
                PdfPageBase originalPage = document.Pages[i];
                if (originalPage.IsBlank())
                {
                    document.Pages.Remove(originalPage);
                    i--;
                }
            }
            document.SaveToFile("Tornadoeswithtidypage.pdf", FileFormat.PDF);
            System.Diagnostics.Process.Start("Tornadoeswithtidypage.pdf");

        }
    }
}
[VB.NET]
Imports System.Windows
Imports Spire.Pdf
Imports System.Drawing

Namespace Tornadoes
	''' 
	''' Interaction logic for MainWindow.xaml
	''' 
	Public Partial Class MainWindow
		Inherits Window
		Public Sub New()
			InitializeComponent()
		End Sub

		Private Sub button1_Click(sender As Object, e As RoutedEventArgs)
			Dim document As New PdfDocument()
			document.LoadFromFile("Tornado.pdf")
			For i As Integer = 0 To document.Pages.Count - 1
				Dim originalPage As PdfPageBase = document.Pages(i)
				If originalPage.IsBlank() Then
					document.Pages.Remove(originalPage)
					i -= 1
				End If
			Next
			document.SaveToFile("Tornadoeswithtidypage.pdf", FileFormat.PDF)
			System.Diagnostics.Process.Start("Tornadoeswithtidypage.pdf")

		End Sub
	End Class
End Namespace

Additional Info

  • tutorial_title: Remove blank page from PDF file in WPF
Last modified on Friday, 24 September 2021 09:55