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











up vote
-2
down vote

favorite












I am working with PDF/A style PDF documents that have a mixture of scanned in, full page size images and then a page or two after the image pages that have text in a ColumnText object.



Using Java, how do i detect which pages have an image?



The intent to detect which pages have either images or text is to determine where the first page with text appears. I need to either edit the text or replace the page(s) with text with updated text. The pages with images would remain untouched.



I'm using iText5 and don't currently have the option of upgrading to iText7.



Here's the solution I implemented with the solution provided by @mkl:



ImageDetector.java



package org.test.pdf;

import com.itextpdf.text.pdf.parser.ImageRenderInfo;
import com.itextpdf.text.pdf.parser.RenderListener;
import com.itextpdf.text.pdf.parser.TextRenderInfo;

public class ImageDetector implements RenderListener {
public void beginTextBlock() { }
public void endTextBlock() { }
public void renderText(TextRenderInfo renderInfo) {
textFound = true;
}

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

boolean textFound = false;
boolean imageFound = false;
}


PdfDocumentServiceTest.java



package org.test.pdf;

import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.parser.PdfReaderContentParser;
import com.test.PdfService;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.transaction.annotation.Transactional;

@ActiveProfiles({"local", "testing"})
@DirtiesContext
@Transactional
@RunWith(SpringRunner.class)
@SpringBootTest
public class PdfDocumentServiceTest {

@Autowired
private PdfService pdfService;

@Test
public void testFindImagesInPdf(Long pdfId)) {
final byte resource = PdfService.getPdf(pdfId);
int imagePageCount = 0;
int textPageCount = 0;
if (resource != null && resource.length > 0) {
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) {
imagePageCount++;
}
if (imageDetector.textFound) {
textPageCount++;
}
}
Assert.assertTrue(imagePageCount > 0);
Assert.assertTrue(textPageCount > 0);
}
}
}









share|improve this question
























  • First, you should use some kind of library (for example, Apache PDFBox) to parse and analyze the PDF documents, because PDF is a rather complex document format.
    – Andreas Mayer
    Nov 16 at 18:34










  • The simplest way to find pages with images is to traverse the page tree and see if a page's resource dictionary contains an image object. However, this is not foolproof: a page may also indirectly reference images via nested objects. So you would also have to recursively look into the page's XObjects, Shadings, and Patterns. And to be really sure, you would have to check that these objects are actually referenced in a content stream -- just because the document contains an image, doesn't mean it's actually visible if you open the document in a PDF reader.
    – Andreas Mayer
    Nov 16 at 18:46












  • @AndreasMayer, I'll work on adding an example of what I've worked on so far. I am using PdfReader.
    – DrewShirts
    Nov 16 at 20:53










  • PdfReader sounds like a class name, not a library name. I know at least two different pdf libraries with a class of that name. Please clarify.
    – mkl
    Nov 19 at 5:09










  • @mkl, I am using iText5 ( com.itextpdf.text.pdf.PdfReader ).
    – DrewShirts
    Nov 19 at 15:29















up vote
-2
down vote

favorite












I am working with PDF/A style PDF documents that have a mixture of scanned in, full page size images and then a page or two after the image pages that have text in a ColumnText object.



Using Java, how do i detect which pages have an image?



The intent to detect which pages have either images or text is to determine where the first page with text appears. I need to either edit the text or replace the page(s) with text with updated text. The pages with images would remain untouched.



I'm using iText5 and don't currently have the option of upgrading to iText7.



Here's the solution I implemented with the solution provided by @mkl:



ImageDetector.java



package org.test.pdf;

import com.itextpdf.text.pdf.parser.ImageRenderInfo;
import com.itextpdf.text.pdf.parser.RenderListener;
import com.itextpdf.text.pdf.parser.TextRenderInfo;

public class ImageDetector implements RenderListener {
public void beginTextBlock() { }
public void endTextBlock() { }
public void renderText(TextRenderInfo renderInfo) {
textFound = true;
}

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

boolean textFound = false;
boolean imageFound = false;
}


PdfDocumentServiceTest.java



package org.test.pdf;

import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.parser.PdfReaderContentParser;
import com.test.PdfService;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.transaction.annotation.Transactional;

@ActiveProfiles({"local", "testing"})
@DirtiesContext
@Transactional
@RunWith(SpringRunner.class)
@SpringBootTest
public class PdfDocumentServiceTest {

@Autowired
private PdfService pdfService;

@Test
public void testFindImagesInPdf(Long pdfId)) {
final byte resource = PdfService.getPdf(pdfId);
int imagePageCount = 0;
int textPageCount = 0;
if (resource != null && resource.length > 0) {
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) {
imagePageCount++;
}
if (imageDetector.textFound) {
textPageCount++;
}
}
Assert.assertTrue(imagePageCount > 0);
Assert.assertTrue(textPageCount > 0);
}
}
}









share|improve this question
























  • First, you should use some kind of library (for example, Apache PDFBox) to parse and analyze the PDF documents, because PDF is a rather complex document format.
    – Andreas Mayer
    Nov 16 at 18:34










  • The simplest way to find pages with images is to traverse the page tree and see if a page's resource dictionary contains an image object. However, this is not foolproof: a page may also indirectly reference images via nested objects. So you would also have to recursively look into the page's XObjects, Shadings, and Patterns. And to be really sure, you would have to check that these objects are actually referenced in a content stream -- just because the document contains an image, doesn't mean it's actually visible if you open the document in a PDF reader.
    – Andreas Mayer
    Nov 16 at 18:46












  • @AndreasMayer, I'll work on adding an example of what I've worked on so far. I am using PdfReader.
    – DrewShirts
    Nov 16 at 20:53










  • PdfReader sounds like a class name, not a library name. I know at least two different pdf libraries with a class of that name. Please clarify.
    – mkl
    Nov 19 at 5:09










  • @mkl, I am using iText5 ( com.itextpdf.text.pdf.PdfReader ).
    – DrewShirts
    Nov 19 at 15:29













up vote
-2
down vote

favorite









up vote
-2
down vote

favorite











I am working with PDF/A style PDF documents that have a mixture of scanned in, full page size images and then a page or two after the image pages that have text in a ColumnText object.



Using Java, how do i detect which pages have an image?



The intent to detect which pages have either images or text is to determine where the first page with text appears. I need to either edit the text or replace the page(s) with text with updated text. The pages with images would remain untouched.



I'm using iText5 and don't currently have the option of upgrading to iText7.



Here's the solution I implemented with the solution provided by @mkl:



ImageDetector.java



package org.test.pdf;

import com.itextpdf.text.pdf.parser.ImageRenderInfo;
import com.itextpdf.text.pdf.parser.RenderListener;
import com.itextpdf.text.pdf.parser.TextRenderInfo;

public class ImageDetector implements RenderListener {
public void beginTextBlock() { }
public void endTextBlock() { }
public void renderText(TextRenderInfo renderInfo) {
textFound = true;
}

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

boolean textFound = false;
boolean imageFound = false;
}


PdfDocumentServiceTest.java



package org.test.pdf;

import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.parser.PdfReaderContentParser;
import com.test.PdfService;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.transaction.annotation.Transactional;

@ActiveProfiles({"local", "testing"})
@DirtiesContext
@Transactional
@RunWith(SpringRunner.class)
@SpringBootTest
public class PdfDocumentServiceTest {

@Autowired
private PdfService pdfService;

@Test
public void testFindImagesInPdf(Long pdfId)) {
final byte resource = PdfService.getPdf(pdfId);
int imagePageCount = 0;
int textPageCount = 0;
if (resource != null && resource.length > 0) {
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) {
imagePageCount++;
}
if (imageDetector.textFound) {
textPageCount++;
}
}
Assert.assertTrue(imagePageCount > 0);
Assert.assertTrue(textPageCount > 0);
}
}
}









share|improve this question















I am working with PDF/A style PDF documents that have a mixture of scanned in, full page size images and then a page or two after the image pages that have text in a ColumnText object.



Using Java, how do i detect which pages have an image?



The intent to detect which pages have either images or text is to determine where the first page with text appears. I need to either edit the text or replace the page(s) with text with updated text. The pages with images would remain untouched.



I'm using iText5 and don't currently have the option of upgrading to iText7.



Here's the solution I implemented with the solution provided by @mkl:



ImageDetector.java



package org.test.pdf;

import com.itextpdf.text.pdf.parser.ImageRenderInfo;
import com.itextpdf.text.pdf.parser.RenderListener;
import com.itextpdf.text.pdf.parser.TextRenderInfo;

public class ImageDetector implements RenderListener {
public void beginTextBlock() { }
public void endTextBlock() { }
public void renderText(TextRenderInfo renderInfo) {
textFound = true;
}

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

boolean textFound = false;
boolean imageFound = false;
}


PdfDocumentServiceTest.java



package org.test.pdf;

import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.parser.PdfReaderContentParser;
import com.test.PdfService;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.transaction.annotation.Transactional;

@ActiveProfiles({"local", "testing"})
@DirtiesContext
@Transactional
@RunWith(SpringRunner.class)
@SpringBootTest
public class PdfDocumentServiceTest {

@Autowired
private PdfService pdfService;

@Test
public void testFindImagesInPdf(Long pdfId)) {
final byte resource = PdfService.getPdf(pdfId);
int imagePageCount = 0;
int textPageCount = 0;
if (resource != null && resource.length > 0) {
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) {
imagePageCount++;
}
if (imageDetector.textFound) {
textPageCount++;
}
}
Assert.assertTrue(imagePageCount > 0);
Assert.assertTrue(textPageCount > 0);
}
}
}






java pdf itext






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 20 at 21:08

























asked Nov 15 at 16:53









DrewShirts

881313




881313












  • First, you should use some kind of library (for example, Apache PDFBox) to parse and analyze the PDF documents, because PDF is a rather complex document format.
    – Andreas Mayer
    Nov 16 at 18:34










  • The simplest way to find pages with images is to traverse the page tree and see if a page's resource dictionary contains an image object. However, this is not foolproof: a page may also indirectly reference images via nested objects. So you would also have to recursively look into the page's XObjects, Shadings, and Patterns. And to be really sure, you would have to check that these objects are actually referenced in a content stream -- just because the document contains an image, doesn't mean it's actually visible if you open the document in a PDF reader.
    – Andreas Mayer
    Nov 16 at 18:46












  • @AndreasMayer, I'll work on adding an example of what I've worked on so far. I am using PdfReader.
    – DrewShirts
    Nov 16 at 20:53










  • PdfReader sounds like a class name, not a library name. I know at least two different pdf libraries with a class of that name. Please clarify.
    – mkl
    Nov 19 at 5:09










  • @mkl, I am using iText5 ( com.itextpdf.text.pdf.PdfReader ).
    – DrewShirts
    Nov 19 at 15:29


















  • First, you should use some kind of library (for example, Apache PDFBox) to parse and analyze the PDF documents, because PDF is a rather complex document format.
    – Andreas Mayer
    Nov 16 at 18:34










  • The simplest way to find pages with images is to traverse the page tree and see if a page's resource dictionary contains an image object. However, this is not foolproof: a page may also indirectly reference images via nested objects. So you would also have to recursively look into the page's XObjects, Shadings, and Patterns. And to be really sure, you would have to check that these objects are actually referenced in a content stream -- just because the document contains an image, doesn't mean it's actually visible if you open the document in a PDF reader.
    – Andreas Mayer
    Nov 16 at 18:46












  • @AndreasMayer, I'll work on adding an example of what I've worked on so far. I am using PdfReader.
    – DrewShirts
    Nov 16 at 20:53










  • PdfReader sounds like a class name, not a library name. I know at least two different pdf libraries with a class of that name. Please clarify.
    – mkl
    Nov 19 at 5:09










  • @mkl, I am using iText5 ( com.itextpdf.text.pdf.PdfReader ).
    – DrewShirts
    Nov 19 at 15:29
















First, you should use some kind of library (for example, Apache PDFBox) to parse and analyze the PDF documents, because PDF is a rather complex document format.
– Andreas Mayer
Nov 16 at 18:34




First, you should use some kind of library (for example, Apache PDFBox) to parse and analyze the PDF documents, because PDF is a rather complex document format.
– Andreas Mayer
Nov 16 at 18:34












The simplest way to find pages with images is to traverse the page tree and see if a page's resource dictionary contains an image object. However, this is not foolproof: a page may also indirectly reference images via nested objects. So you would also have to recursively look into the page's XObjects, Shadings, and Patterns. And to be really sure, you would have to check that these objects are actually referenced in a content stream -- just because the document contains an image, doesn't mean it's actually visible if you open the document in a PDF reader.
– Andreas Mayer
Nov 16 at 18:46






The simplest way to find pages with images is to traverse the page tree and see if a page's resource dictionary contains an image object. However, this is not foolproof: a page may also indirectly reference images via nested objects. So you would also have to recursively look into the page's XObjects, Shadings, and Patterns. And to be really sure, you would have to check that these objects are actually referenced in a content stream -- just because the document contains an image, doesn't mean it's actually visible if you open the document in a PDF reader.
– Andreas Mayer
Nov 16 at 18:46














@AndreasMayer, I'll work on adding an example of what I've worked on so far. I am using PdfReader.
– DrewShirts
Nov 16 at 20:53




@AndreasMayer, I'll work on adding an example of what I've worked on so far. I am using PdfReader.
– DrewShirts
Nov 16 at 20:53












PdfReader sounds like a class name, not a library name. I know at least two different pdf libraries with a class of that name. Please clarify.
– mkl
Nov 19 at 5:09




PdfReader sounds like a class name, not a library name. I know at least two different pdf libraries with a class of that name. Please clarify.
– mkl
Nov 19 at 5:09












@mkl, I am using iText5 ( com.itextpdf.text.pdf.PdfReader ).
– DrewShirts
Nov 19 at 15:29




@mkl, I am using iText5 ( com.itextpdf.text.pdf.PdfReader ).
– DrewShirts
Nov 19 at 15:29












2 Answers
2






active

oldest

votes

















up vote
0
down vote



accepted










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.






share|improve this answer





















  • This worked great!! I'll add my code to the original question.
    – DrewShirts
    Nov 20 at 19:41


















up vote
0
down vote













Try the below code example (Spire.PDF for Java library is needed), hopefully it works for you.



    PdfDocument doc = new PdfDocument();
doc.loadFromFile("sample.pdf");

for(int i = 0; i < doc.getPages().getCount(); i ++) {
PdfPageBase page = doc.getPages().get(i);
PdfImageInfo imageInfo = page.getImagesInfo();

if (imageInfo != null && imageInfo.length > 0) {
System.out.println("Page" + i + "contains image");
}
else {
System.out.print("Page" + i + "doesn't contain image");
}


Disclaimer: I work for Spire.






share|improve this answer























  • I'll work on testing this out. Thank you.
    – DrewShirts
    Nov 19 at 15:30










  • Does page.getImagesInfo() represent the images in the page resources (both used and unused) or the images actually used on the page (inline, from the page resources, or from xobject resources shown on the page)? As an aside, doc.getPages().indexOf(page) is merely a complicated way to write i, isn't it?
    – mkl
    Nov 19 at 16:03










  • @mkl page.getImagesInfo() represents all of the images in the page. Sorry I made a stupid mistake :(, just updated the answer, thank you for pointing it out.
    – Dheeraj Malik
    Nov 20 at 7:46












  • "all of the images in the page" - in the page as PDF object (i.e. in the page resources), in the page as display unit (i.e. all images used to render the page), or a combination thereof?
    – mkl
    Nov 20 at 9:14











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53324326%2fhow-to-detect-if-a-pdf-page-has-an-image-in-it%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
0
down vote



accepted










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.






share|improve this answer





















  • This worked great!! I'll add my code to the original question.
    – DrewShirts
    Nov 20 at 19:41















up vote
0
down vote



accepted










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.






share|improve this answer





















  • This worked great!! I'll add my code to the original question.
    – DrewShirts
    Nov 20 at 19:41













up vote
0
down vote



accepted







up vote
0
down vote



accepted






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.






share|improve this answer












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.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 20 at 9:47









mkl

52.4k1166143




52.4k1166143












  • This worked great!! I'll add my code to the original question.
    – DrewShirts
    Nov 20 at 19:41


















  • This worked great!! I'll add my code to the original question.
    – DrewShirts
    Nov 20 at 19:41
















This worked great!! I'll add my code to the original question.
– DrewShirts
Nov 20 at 19:41




This worked great!! I'll add my code to the original question.
– DrewShirts
Nov 20 at 19:41












up vote
0
down vote













Try the below code example (Spire.PDF for Java library is needed), hopefully it works for you.



    PdfDocument doc = new PdfDocument();
doc.loadFromFile("sample.pdf");

for(int i = 0; i < doc.getPages().getCount(); i ++) {
PdfPageBase page = doc.getPages().get(i);
PdfImageInfo imageInfo = page.getImagesInfo();

if (imageInfo != null && imageInfo.length > 0) {
System.out.println("Page" + i + "contains image");
}
else {
System.out.print("Page" + i + "doesn't contain image");
}


Disclaimer: I work for Spire.






share|improve this answer























  • I'll work on testing this out. Thank you.
    – DrewShirts
    Nov 19 at 15:30










  • Does page.getImagesInfo() represent the images in the page resources (both used and unused) or the images actually used on the page (inline, from the page resources, or from xobject resources shown on the page)? As an aside, doc.getPages().indexOf(page) is merely a complicated way to write i, isn't it?
    – mkl
    Nov 19 at 16:03










  • @mkl page.getImagesInfo() represents all of the images in the page. Sorry I made a stupid mistake :(, just updated the answer, thank you for pointing it out.
    – Dheeraj Malik
    Nov 20 at 7:46












  • "all of the images in the page" - in the page as PDF object (i.e. in the page resources), in the page as display unit (i.e. all images used to render the page), or a combination thereof?
    – mkl
    Nov 20 at 9:14















up vote
0
down vote













Try the below code example (Spire.PDF for Java library is needed), hopefully it works for you.



    PdfDocument doc = new PdfDocument();
doc.loadFromFile("sample.pdf");

for(int i = 0; i < doc.getPages().getCount(); i ++) {
PdfPageBase page = doc.getPages().get(i);
PdfImageInfo imageInfo = page.getImagesInfo();

if (imageInfo != null && imageInfo.length > 0) {
System.out.println("Page" + i + "contains image");
}
else {
System.out.print("Page" + i + "doesn't contain image");
}


Disclaimer: I work for Spire.






share|improve this answer























  • I'll work on testing this out. Thank you.
    – DrewShirts
    Nov 19 at 15:30










  • Does page.getImagesInfo() represent the images in the page resources (both used and unused) or the images actually used on the page (inline, from the page resources, or from xobject resources shown on the page)? As an aside, doc.getPages().indexOf(page) is merely a complicated way to write i, isn't it?
    – mkl
    Nov 19 at 16:03










  • @mkl page.getImagesInfo() represents all of the images in the page. Sorry I made a stupid mistake :(, just updated the answer, thank you for pointing it out.
    – Dheeraj Malik
    Nov 20 at 7:46












  • "all of the images in the page" - in the page as PDF object (i.e. in the page resources), in the page as display unit (i.e. all images used to render the page), or a combination thereof?
    – mkl
    Nov 20 at 9:14













up vote
0
down vote










up vote
0
down vote









Try the below code example (Spire.PDF for Java library is needed), hopefully it works for you.



    PdfDocument doc = new PdfDocument();
doc.loadFromFile("sample.pdf");

for(int i = 0; i < doc.getPages().getCount(); i ++) {
PdfPageBase page = doc.getPages().get(i);
PdfImageInfo imageInfo = page.getImagesInfo();

if (imageInfo != null && imageInfo.length > 0) {
System.out.println("Page" + i + "contains image");
}
else {
System.out.print("Page" + i + "doesn't contain image");
}


Disclaimer: I work for Spire.






share|improve this answer














Try the below code example (Spire.PDF for Java library is needed), hopefully it works for you.



    PdfDocument doc = new PdfDocument();
doc.loadFromFile("sample.pdf");

for(int i = 0; i < doc.getPages().getCount(); i ++) {
PdfPageBase page = doc.getPages().get(i);
PdfImageInfo imageInfo = page.getImagesInfo();

if (imageInfo != null && imageInfo.length > 0) {
System.out.println("Page" + i + "contains image");
}
else {
System.out.print("Page" + i + "doesn't contain image");
}


Disclaimer: I work for Spire.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 20 at 6:35

























answered Nov 19 at 3:46









Dheeraj Malik

33126




33126












  • I'll work on testing this out. Thank you.
    – DrewShirts
    Nov 19 at 15:30










  • Does page.getImagesInfo() represent the images in the page resources (both used and unused) or the images actually used on the page (inline, from the page resources, or from xobject resources shown on the page)? As an aside, doc.getPages().indexOf(page) is merely a complicated way to write i, isn't it?
    – mkl
    Nov 19 at 16:03










  • @mkl page.getImagesInfo() represents all of the images in the page. Sorry I made a stupid mistake :(, just updated the answer, thank you for pointing it out.
    – Dheeraj Malik
    Nov 20 at 7:46












  • "all of the images in the page" - in the page as PDF object (i.e. in the page resources), in the page as display unit (i.e. all images used to render the page), or a combination thereof?
    – mkl
    Nov 20 at 9:14


















  • I'll work on testing this out. Thank you.
    – DrewShirts
    Nov 19 at 15:30










  • Does page.getImagesInfo() represent the images in the page resources (both used and unused) or the images actually used on the page (inline, from the page resources, or from xobject resources shown on the page)? As an aside, doc.getPages().indexOf(page) is merely a complicated way to write i, isn't it?
    – mkl
    Nov 19 at 16:03










  • @mkl page.getImagesInfo() represents all of the images in the page. Sorry I made a stupid mistake :(, just updated the answer, thank you for pointing it out.
    – Dheeraj Malik
    Nov 20 at 7:46












  • "all of the images in the page" - in the page as PDF object (i.e. in the page resources), in the page as display unit (i.e. all images used to render the page), or a combination thereof?
    – mkl
    Nov 20 at 9:14
















I'll work on testing this out. Thank you.
– DrewShirts
Nov 19 at 15:30




I'll work on testing this out. Thank you.
– DrewShirts
Nov 19 at 15:30












Does page.getImagesInfo() represent the images in the page resources (both used and unused) or the images actually used on the page (inline, from the page resources, or from xobject resources shown on the page)? As an aside, doc.getPages().indexOf(page) is merely a complicated way to write i, isn't it?
– mkl
Nov 19 at 16:03




Does page.getImagesInfo() represent the images in the page resources (both used and unused) or the images actually used on the page (inline, from the page resources, or from xobject resources shown on the page)? As an aside, doc.getPages().indexOf(page) is merely a complicated way to write i, isn't it?
– mkl
Nov 19 at 16:03












@mkl page.getImagesInfo() represents all of the images in the page. Sorry I made a stupid mistake :(, just updated the answer, thank you for pointing it out.
– Dheeraj Malik
Nov 20 at 7:46






@mkl page.getImagesInfo() represents all of the images in the page. Sorry I made a stupid mistake :(, just updated the answer, thank you for pointing it out.
– Dheeraj Malik
Nov 20 at 7:46














"all of the images in the page" - in the page as PDF object (i.e. in the page resources), in the page as display unit (i.e. all images used to render the page), or a combination thereof?
– mkl
Nov 20 at 9:14




"all of the images in the page" - in the page as PDF object (i.e. in the page resources), in the page as display unit (i.e. all images used to render the page), or a combination thereof?
– mkl
Nov 20 at 9:14


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53324326%2fhow-to-detect-if-a-pdf-page-has-an-image-in-it%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Biblatex bibliography style without URLs when DOI exists (in Overleaf with Zotero bibliography)

ComboBox Display Member on multiple fields

Is it possible to collect Nectar points via Trainline?