Java: Find and Remove Blank Pages from PDF

If you are going to print or share a PDF document, it’s better to check if there are blank pages in the document, because they will lead to a waste of paper and a less professional look for your document. However, it will take much time to look through every page to find the empty pages and then delete them. A better way to deal with this problem is to use Spire.PDF for Java. In this article, you will learn how to use Spire.PDF for Java to find and remove blank pages from PDF document easily by programming.

Install Spire.PDF for Java

First, 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.

<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>10.10.7</version>
    </dependency>
</dependencies>
    

Find and Delete Blank Pages from a PDF Document

Spire.PDF for Java provides a method PdfPageBase.isBlank() to detect if a PDF page is absolutely blank. But some pages that look blank actually contain white images, these pages won't be deemed as blank using the PdfPageBase.isBlank() method. Therefore, it is necessary create a custom method isBlankImage() to be used in conjunction with PdfPageBase.isBlank() method to detect blank and white but non-blank pages.

Note: This solution will convert PDF pages into images and detect if an image is blank. It is necessary to apply a license to remove the evaluation message in the converted images. Otherwise, this method won't work properly. If you do not have a license, contact sales@e-iceblue.com for a temporary one for evaluation purpose.

The detailed steps are as follows:

  • Create an object of PdfDocument class.
  • Load a PDF document using PdfDocument.loadFromFile() method.
  • Loop through the pages in the PDF document to detect if the pages are blank using PdfPageBase.isBlank() method.
  • For absolutely blank pages, delete them using PdfDocument.getPages().remove() method.
  • For pages that are not absolutely blank, save them as images using PdfDocument.saveAsImage() method, detect if the converted images are blank using custom method isBlankImage() and then remove the pages that are “balnk” using PdfDocument.getPages().remove().
  • Save the result document using PdfDocument.saveToFile() method.
  • Java
import com.spire.pdf.PdfDocument;
import com.spire.pdf.PdfPageBase;
import com.spire.pdf.graphics.PdfImageType;

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

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

        //Create a PdfDocument class instance
        PdfDocument pdf = new PdfDocument();

        //Load a PDF document
        pdf.loadFromFile("C:/Sample.pdf");

        BufferedImage image;
        //Loop through pages in the PDF
        for(int i = pdf.getPages().getCount()-1; i>=0; i--)
        {
            PdfPageBase page = pdf.getPages().get(i);
            //Detect if a page is blank
            if(page.isBlank())
            {
                //Remove the absolutely blank page
                pdf.getPages().remove(page);
            }
            else
            {
                //Save PDF page as image
                image = pdf.saveAsImage(i, PdfImageType.Bitmap);

                //Detect if the converted image is blank
                if (isBlankImage(image))
                {
                    //Remove the page
                    pdf.getPages().remove(page);
                }
            }

        }

        //Save the result document
        pdf.saveToFile("RemoveBlankPages.pdf");
    }
    //Detect if an image is blank
    public static boolean isBlankImage(BufferedImage image)
    {
        BufferedImage bufferedImage = image;

        Color pixel;
        for (int i = 0; i < bufferedImage.getWidth(); i++)
        {
            for (int j = 0; j < bufferedImage.getHeight(); j++)
            {
                pixel = new Color(bufferedImage.getRGB(i, j));
                if (pixel.getRed() < 240 || pixel.getGreen() < 240 || pixel.getBlue() < 240)
                {
                    return false;
                }
            }
        }
        return true;
    }
}

Java: Find and Remove Blank Pages from PDF

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.