How to get the co-ordinates of an image?

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 extr

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 RenderListener interface.

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

public void renderImage(ImageRenderInfo renderInfo) {
    try {
        String filename;
        FileOutputStream os;
        PdfImageObject image = renderInfo.getImage();
        if (image == null) return;
        filename = String.format(path, renderInfo.getRef().getNumber(), image.getFileType());
        os = new FileOutputStream(filename);
        os.write(image.getImageAsBytes());
        os.flush();
        os.close();
    } catch (IOException e) {
        System.out.println(e.getMessage());
    }
}

It uses the ImageRenderInfo object to obtain a PdfImageObject 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.



Ready to use iText?

Try our iText 7 Library and add-ons FREE for 30 days. Test your proof of concept, and see if our solution is right for you.

Get my FREE trial
Contact

Still have questions? 

We're happy to answer your questions. Reach out to us and we'll get back to you shortly.

Contact us
Stay updated

Join 11,000+ subscribers and become an iText PDF expert by staying up to date with our new products, updates, tips, technical solutions and happenings.

Subscribe Now