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












-2















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 '18 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 '18 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 '18 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 '18 at 5:09











  • @mkl, I am using iText5 ( com.itextpdf.text.pdf.PdfReader ).

    – DrewShirts
    Nov 19 '18 at 15:29
















-2















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 '18 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 '18 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 '18 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 '18 at 5:09











  • @mkl, I am using iText5 ( com.itextpdf.text.pdf.PdfReader ).

    – DrewShirts
    Nov 19 '18 at 15:29














-2












-2








-2








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 '18 at 21:08







DrewShirts

















asked Nov 15 '18 at 16:53









DrewShirtsDrewShirts

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 '18 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 '18 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 '18 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 '18 at 5:09











  • @mkl, I am using iText5 ( com.itextpdf.text.pdf.PdfReader ).

    – DrewShirts
    Nov 19 '18 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 '18 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 '18 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 '18 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 '18 at 5:09











  • @mkl, I am using iText5 ( com.itextpdf.text.pdf.PdfReader ).

    – DrewShirts
    Nov 19 '18 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 '18 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 '18 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 '18 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 '18 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 '18 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 '18 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 '18 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 '18 at 5:09













@mkl, I am using iText5 ( com.itextpdf.text.pdf.PdfReader ).

– DrewShirts
Nov 19 '18 at 15:29





@mkl, I am using iText5 ( com.itextpdf.text.pdf.PdfReader ).

– DrewShirts
Nov 19 '18 at 15:29












2 Answers
2






active

oldest

votes


















0














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 '18 at 19:41



















0














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 '18 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 '18 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 '18 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 '18 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',
autoActivateHeartbeat: false,
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









0














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 '18 at 19:41
















0














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 '18 at 19:41














0












0








0







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 '18 at 9:47









mklmkl

54.4k1168146




54.4k1168146













  • This worked great!! I'll add my code to the original question.

    – DrewShirts
    Nov 20 '18 at 19:41



















  • This worked great!! I'll add my code to the original question.

    – DrewShirts
    Nov 20 '18 at 19:41

















This worked great!! I'll add my code to the original question.

– DrewShirts
Nov 20 '18 at 19:41





This worked great!! I'll add my code to the original question.

– DrewShirts
Nov 20 '18 at 19:41













0














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 '18 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 '18 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 '18 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 '18 at 9:14
















0














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 '18 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 '18 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 '18 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 '18 at 9:14














0












0








0







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 '18 at 6:35

























answered Nov 19 '18 at 3:46









Dheeraj MalikDheeraj Malik

34126




34126













  • I'll work on testing this out. Thank you.

    – DrewShirts
    Nov 19 '18 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 '18 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 '18 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 '18 at 9:14



















  • I'll work on testing this out. Thank you.

    – DrewShirts
    Nov 19 '18 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 '18 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 '18 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 '18 at 9:14

















I'll work on testing this out. Thank you.

– DrewShirts
Nov 19 '18 at 15:30





I'll work on testing this out. Thank you.

– DrewShirts
Nov 19 '18 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 '18 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 '18 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 '18 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 '18 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 '18 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 '18 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.




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

鏡平學校

ꓛꓣだゔៀៅຸ໢ທຮ໕໒ ,ໂ'໥໓າ໼ឨឲ៵៭ៈゎゔit''䖳𥁄卿' ☨₤₨こゎもょの;ꜹꟚꞖꞵꟅꞛေၦေɯ,ɨɡ𛃵𛁹ޝ޳ޠ޾,ޤޒޯ޾𫝒𫠁သ𛅤チョ'サノބޘދ𛁐ᶿᶇᶀᶋᶠ㨑㽹⻮ꧬ꧹؍۩وَؠ㇕㇃㇪ ㇦㇋㇋ṜẰᵡᴠ 軌ᵕ搜۳ٰޗޮ޷ސޯ𫖾𫅀ल, ꙭ꙰ꚅꙁꚊꞻꝔ꟠Ꝭㄤﺟޱސꧨꧼ꧴ꧯꧽ꧲ꧯ'⽹⽭⾁⿞⼳⽋២៩ញណើꩯꩤ꩸ꩮᶻᶺᶧᶂ𫳲𫪭𬸄𫵰𬖩𬫣𬊉ၲ𛅬㕦䬺𫝌𫝼,,𫟖𫞽ហៅ஫㆔ాఆఅꙒꚞꙍ,Ꙟ꙱エ ,ポテ,フࢰࢯ𫟠𫞶 𫝤𫟠ﺕﹱﻜﻣ𪵕𪭸𪻆𪾩𫔷ġ,ŧآꞪ꟥,ꞔꝻ♚☹⛵𛀌ꬷꭞȄƁƪƬșƦǙǗdžƝǯǧⱦⱰꓕꓢႋ神 ဴ၀க௭எ௫ឫោ ' េㇷㇴㇼ神ㇸㇲㇽㇴㇼㇻㇸ'ㇸㇿㇸㇹㇰㆣꓚꓤ₡₧ ㄨㄟ㄂ㄖㄎ໗ツڒذ₶।ऩछएोञयूटक़कयँृी,冬'𛅢𛅥ㇱㇵㇶ𥄥𦒽𠣧𠊓𧢖𥞘𩔋цѰㄠſtʯʭɿʆʗʍʩɷɛ,əʏダヵㄐㄘR{gỚṖḺờṠṫảḙḭᴮᵏᴘᵀᵷᵕᴜᴏᵾq﮲ﲿﴽﭙ軌ﰬﶚﶧ﫲Ҝжюїкӈㇴffצּ﬘﭅﬈軌'ffistfflſtffतभफɳɰʊɲʎ𛁱𛁖𛁮𛀉 𛂯𛀞నఋŀŲ 𫟲𫠖𫞺ຆຆ ໹້໕໗ๆทԊꧢꧠ꧰ꓱ⿝⼑ŎḬẃẖỐẅ ,ờỰỈỗﮊDžȩꭏꭎꬻ꭮ꬿꭖꭥꭅ㇭神 ⾈ꓵꓑ⺄㄄ㄪㄙㄅㄇstA۵䞽ॶ𫞑𫝄㇉㇇゜軌𩜛𩳠Jﻺ‚Üမ႕ႌႊၐၸဓၞၞၡ៸wyvtᶎᶪᶹစဎ꣡꣰꣢꣤ٗ؋لㇳㇾㇻㇱ㆐㆔,,㆟Ⱶヤマފ޼ޝަݿݞݠݷݐ',ݘ,ݪݙݵ𬝉𬜁𫝨𫞘くせぉて¼óû×ó£…𛅑הㄙくԗԀ5606神45,神796'𪤻𫞧ꓐ㄁ㄘɥɺꓵꓲ3''7034׉ⱦⱠˆ“𫝋ȍ,ꩲ軌꩷ꩶꩧꩫఞ۔فڱێظペサ神ナᴦᵑ47 9238їﻂ䐊䔉㠸﬎ffiﬣ,לּᴷᴦᵛᵽ,ᴨᵤ ᵸᵥᴗᵈꚏꚉꚟ⻆rtǟƴ𬎎

Why https connections are so slow when debugging (stepping over) in Java?