Skip to main content
Skip table of contents

How to get the coordinates of an image?

How do I know whether the extracted image is the same as an image present in database, and the co-ordinates of the image are same as present in database?

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.

JavaScript errors detected

Please note, these errors can depend on your browser setup.

If this problem persists, please contact our support.