[Solved] How to detect if a PDF page has an image in it


Using iText 5 you can find out whether images actually are shown on a page by parsing the page content into a custom RenderListener implementation. E.g.

class ImageDetector implements RenderListener {
    public void beginTextBlock() { }
    public void endTextBlock() { }
    public void renderText(TextRenderInfo renderInfo) { }

    public void renderImage(ImageRenderInfo renderInfo) {
        imageFound = true;
    }

    boolean imageFound = false;
}

used like this:

PdfReader reader = new PdfReader(resource);
PdfReaderContentParser parser = new PdfReaderContentParser(reader);
for (int pageNumber = 1; pageNumber <= reader.getNumberOfPages(); pageNumber++)
{
    ImageDetector imageDetector = new ImageDetector();
    parser.processContent(pageNumber, imageDetector);
    if (imageDetector.imageFound) {
        // There is at least one image rendered on page i
        // Thus, handle it as an image page
    } else {
        // There is no image rendered on page i
        // Thus, handle it as a no-image page
    }
}

As a possible improvement: In a comment you mention full-page-size images. Thus, in the ImageDetector method renderImage you might want to check the image size before setting imageFound to true. Via the ImageRenderInfo parameter you can retrieve both information on how large the image is displayed on the page and how large it actually is.

1

solved How to detect if a PDF page has an image in it