Java: Find and Remove Empty Pages from PDF

Spire.PDF for Java offers a method PdfPageBase.isBlank() to detect if a PDF page is absolutely empty. But sometimes, a PDF page may contain a scanned white image that looks blank. This article will demonstrate how to remove the empty PDF pages from the following two parts.

Install Spire.PDF for Java

First of all, you're required to add the Spire.PDF.jar file as a dependency in your Java program. The JAR file can be downloaded from this link. If you use Maven, you can easily import the JAR file in your application by adding the following code to your project's pom.xml file.

  • Package Manager
<repositories>
    <repository>
        <id>com.e-iceblue</id>
        <name>e-iceblue</name>
        <url>https://repo.e-iceblue.com/nexus/content/groups/public/</url>
    </repository>
</repositories>
<dependencies>
    <dependency>
        <groupId>e-iceblue</groupId>
        <artifactId>spire.pdf</artifactId>
        <version>4.11.1</version>
    </dependency>
</dependencies>

Remove the Absolutely Empty Pages from PDF

You can find the empty pages from the PDF document using the PdfPageBase.isBlank() method and then remove all the empty pages in PDF.

  • Create an object of PdfDocument class and load a sample PDF document using PdfDocument.loadFromFile() method.
  • Loop through the pages and detect if a page is blank using PdfPageBase.isBlank() method.
  • Remove the blank pages using PdfDocument.getPages().removeAt() method.
  • Save the document to another file using PdfDocument.saveToFile() method.
  • Java
import com.spire.pdf.*;

public class removeBlankPages {
    public static void main(String[] args) throws Exception {

        //Create an object of PdfDocument class.
        PdfDocument pdf = new PdfDocument();

        //Load the file from disk
        pdf.loadFromFile("Sample.pdf");

          //Traverse all the pages
          for (int i = pdf.getPages().getCount() - 1; i >= 0; i--)

              //detect if a page is blank
              if (pdf.getPages().get(i).isBlank()) {

                  //Remove blank page
                  pdf.getPages().removeAt(i);
              }
           
            //Save the pdf document
            pdf.saveToFile("Removeblankpages.pdf");

        }
    }

Java: Find and Remove Empty Pages from PDF

Remove the Pages Containing White Images from PDF

The following code snippets will demonstrate how to remove the PDF page with white images. Firstly, you need to request a 30-day trial license to remove the evaluation message in the converted images. Otherwise, this method won't work properly.

  • Register a license key to remove the warning message
  • Create an object of PdfDocument class and load a sample PDF document using PdfDocument.loadFromFile() method.
  • Convert the PDF pages to image using PdfDocument.saveAsImage() method.
  • Invoke the custom method isImageBlank (BufferedImage image) to detect whether a picture is blank or not.
  • Remove the pages containing blank images using PdfDocument.getPages().removeAt() method.
  • Save the document to another file using PdfDocument.saveToFile() method.
  • Java
import com.spire.pdf.*;

import java.awt.*;
import java.awt.image.*;

import static com.spire.pdf.graphics.PdfImageType.Bitmap;

public class removeBlankPages2 {
    public static void main(String[] args) throws Exception {

        //Register the license key
        com.spire.license.LicenseProvider.setLicenseKey("your license key");

        //Create an object of PdfDocument class.
        PdfDocument pdf = new PdfDocument();

        //Load the file from disk
        pdf.loadFromFile("Sample2.pdf");

        //Traverse all the pages
        for (int i = pdf.getPages().getCount() - 1; i >= 0; i--) {

            //Convert the PDF to images
            BufferedImage image = pdf.saveAsImage(i, Bitmap);

            //Determine whether a picture is blank or not
            if (isImageBlank(image)) {

                //Delete the corresponding PDF page if the picture is blank
                pdf.getPages().removeAt(i);

            }

            //Save the Pdf document
            pdf.saveToFile("Removeblankpages.pdf");

        }
    }
        public static boolean isImageBlank (BufferedImage image)
        {
            for (int i = 0; i < image.getWidth(); i++) {
                for (int j = 0; j < image.getHeight(); j++) {
                    int pixel = image.getRGB(i, j);
                    Color c = new Color(pixel);
                    if (c.getRed() < 240 || c.getGreen() < 240 || c.getBlue() < 240) {
                        return false;
                    }
                }
            }
            return true;
        }
}

Java: Find and Remove Empty Pages from PDF