Detect and Remove Blank Pages in PDF in Java

Spire.PDF for Java provides a PdfPageBase.isBlank method to detect if a PDF page is absolutely blank, however, some pages look like “blank” but they actually contain white images, these pages won't be deemed as blank using the PdfPageBase.isBlank method. Therefore, we create a custom method isBlankImage to use it in conjunction with the PdfPageBase.isBlank method to detect blank and those white but non-blank pages.

In the following example, we will describe how to detect and remove blank and white pages in a PDF document using Spire.PDF for Java.

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.

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 instance
        PdfDocument pdf = new PdfDocument();
        //Load a PDF document
        pdf.loadFromFile("Input.pdf");

        BufferedImage image;
        //Iterate 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())
            {
                //If yes, remove the page
                pdf.getPages().remove(page);
            }
            else
            {
                //Save PDF page as image
                image = pdf.saveAsImage(i, PdfImageType.Bitmap);

                //Detect if a page contains white image
                if (isBlankImage(image))
                {
                    //If yes, 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;
    }
}

The input PDF:

Detect and Remove Blank Pages in PDF in Java

The output PDF:

Detect and Remove Blank Pages in PDF in Java