How to get the co-ordinates of an image?

Tags: imagesinspect PDFextract contentimage positioniText 7

In my project, I want to find co-ordinates of the images in my PDFs. I tried searching iText, but I was not succesful. Using these co-ordinates and extracted image, I want to verify whether the extracted image is the same as an image present in database, and the co-ordinates of image are same as present in database.

Posted on StackOverflow on Jun 5, 2014 by novice_3

When you say that you've tried with iText, I assume that you've used the ExtractImages example as the starting point for your code. This example uses the helper class MyImageRenderListener, which implements the IEventListener interface.

In that helper class the eventOccurred() method is implemented like this:

public void eventOccurred(IEventData data, EventType type) {
    switch (type) {
        case RENDER_IMAGE:
            try {
                String filename;
                FileOutputStream os;
                ImageRenderInfo renderInfo = (ImageRenderInfo) data;
                PdfImageXObject image = renderInfo.getImage();
                if (image == null) {
                    return;
                }
                byte[] imageByte = null;
                try {
                    imageByte = image.getImageBytes(true);
                    switch (ImageDataFactory.create(imageByte).getOriginalType()) {
                        case JPEG:
                            extension = "jpg";
                            break;
                        case PNG:
                            extension = "png";
                            break;
                        case GIF:
                            extension = "gif";
                            break;
                        case BMP:
                            extension = "bmp";
                            break;
                        case TIFF:
                            extension = "tif";
                            break;
                        case WMF:
                            extension = "wmf";
                            break;
                        case JPEG2000:
                            extension = "jp2";
                            break;
                        case JBIG2:
                            extension = "jbig2";
                            break;
 
                        default:
                            extension = "jpg";
                    }
                } catch (com.itextpdf.io.IOException e) {
                    System.out.println(e.getMessage());
                }
                if (imageByte == null) {
                    return;
                }
                filename = String.format(path, i++, extension);
                os = new FileOutputStream(filename);
                os.write(imageByte);
                os.flush();
                os.close();
            } catch (IOException e) {
                System.out.println(e.getMessage());
            }
            break;
 
        default:
            break;
    }
}

It uses the ImageRenderInfo object to obtain a PdfImageXObject instance and it creates an image file using that object.

If you inspect the ImageRenderInfo class, you'll discover that you can also ask for other info about the image. What you need, is the getImageCtm() method. This method returns a Matrix object. This matrix can be interpreted using ordinary high-school algebra. The values I31 and I32 give you the X and Y position. In most cases I11 and I22 will give you the width and the height (unless the image is rotated). If the image is rotated, you'll have to consult your high-school schoolbooks, more specifically the ones discussing analytic geometry.

Click this link if you want to see how to answer this question in iText 5.