how to read or extract graphical componenets such as square ,rect,line etc., from a pdf document using java?

十年热恋 提交于 2019-12-04 16:54:27
npinti

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!