Question

I am trying to extract all datas( such as square ,rect,line etc.,) from a pdf document which was generated using iText.But I'm not able extract the content rather than text and image.I want to extract graphical components mentioned above.

Was it helpful?

Solution

There seem to be 3 options for this (at least those are the ones I could find), I do not know what you exactly have, so I will paste all the 3, these are in increasing levels of difficulty)

First Option: You could do something like so: (taken from here)

PDDocument document = null; 
document = PDDocument.load(inFile); 
List pages = document.getDocumentCatalog().getAllPages();
Iterator iter = pages.iterator(); 
while (iter.hasNext()) {
            PDPage page = (PDPage) iter.next();
            PDResources resources = page.getResources();
            Map pageImages = resources.getImages();
            if (pageImages != null) { 
                Iterator imageIter = pageImages.keySet().iterator();
                while (imageIter.hasNext()) {
                    String key = (String) imageIter.next();
                    PDXObjectImage image = (PDXObjectImage) pageImages.get(key);
                    image.write2OutputStream(/* some output stream */);
                }
            }
}

Second option could be to convert your PDF document to HTML, using something along the lines of what is shown here and then, use JSoup to process the HTML and iterate over the img tags, which is how I am assuming that the images will be rendered.

Alternatively, you could take a look at the Hough Transform:

The Hough transform is a feature extraction technique used in image analysis, computer vision, and digital image processing. The purpose of the technique is to find imperfect instances of objects within a certain class of shapes by a voting procedure.

An imaging library, such as OpenCV should be able to yield such functionality out of the box (OpenCV-Java) being a Java wrapper for such library.

This example should point you in the right direction.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top