0

I am using the below function

public void renderImage(ImageRenderInfo renderInfo) {
    try {
        String filename;
        FileOutputStream os;
        PdfImageObject image = renderInfo.getImage();


        PdfDictionary imageDict = renderInfo.getImage().getDictionary();
        float widthPx = imageDict.getAsNumber(PdfName.WIDTH).floatValue(); 
        float heightPx = imageDict.getAsNumber(PdfName.HEIGHT).floatValue();
        float widthUu = renderInfo.getImageCTM().get(Matrix.I11);
        float heigthUu = renderInfo.getImageCTM().get(Matrix.I22);

 //while printing in inches I am dividing heightUu and widthUu by 72

        System.out.printf("Image %.0fpx*%.0fpx, %.0fuu*%.0fuu, %.2fin*%.2fin\n", widthPx, heightPx, widthUu, heigthUu, widthUu/72, heigthUu/72);

        if (image == null) return;
        filename = String.format(path, renderInfo.getRef().getNumber(), image.getFileType());
        System.out.println(filename);
        os = new FileOutputStream(filename);
        os.write(image.getImageAsBytes());
        os.flush();
        os.close();


    } catch (IOException e) {
        System.out.println(e.getMessage());
    }
}

When I check the image sizes in photoshop, I am getting the height and width in pixels Perfectly, but not getting the width and height in Inches correctly.

I need it so that I can calculate the DPI of the image.

For Ex:

Image Original Values: width - 450 px and height - 362px

Width - 6.25 inches and height - 5.028 inches(Values got from photoshop)

What I recieve from itext:

width - 450 px and height - 362px (This is perfect)

Width -3.60 inches and height - 2.90 inches(Here is the problem)

Abhinav
  • 8,028
  • 12
  • 48
  • 89
  • You are making a logical error that is hard to explain. iText is correct. Your assumption about image resolution is incorrect. I wouldn't know how to explain why your assumption is incorrect, though. Trivial things are always hard to explain. – Bruno Lowagie Aug 24 '15 at 12:07
  • By logical error you mean in my code? – Abhinav Aug 24 '15 at 12:13
  • No, I mean that your interpretation of the concept "resolution" is wrong. – Bruno Lowagie Aug 24 '15 at 12:59
  • 2
    @Bruno, do you possibly mean that *the inch values in Photoshop* do not mean anything? Because there is no guarantee the image got placed at 100%? Clarification: the DPI value (and, by extension, its Real World size as reported by Photoshop) for 'an' image does not mean anything for an image on its own. As soon as it gets placed into a PDF, you don't need the original dimensions anymore. The values that iText retrieves "are" the actual (Real World™) dimensions and so the DPI value that you calculate from it is correct, for that one instance of that one image. – Jongware Aug 24 '15 at 13:24
  • 1
    @Jongware Yes. For instance: you could have an image with specific dimensions in pixels and a specific DPI. Then that image is inserted into a PDF and it gets transformed. E.g. PDF doesn't support PNG, so the PNG is transformed into a compressed bitmap. The bitmap has the original amount of pixels, but it has no recollection whatsoever about the DPI. If the image used to be 6 inches wide originally, but it is rendered in the PDF as 3 inches wide, the resolution has augmented. That doesn't mean that software returning 3 inch instead of 6 inch is wrong. – Bruno Lowagie Aug 24 '15 at 13:52
  • @BrunoLowagie ,@Jongware : Thanx a lot for your clarifications, I am kind of getting the drift of what you guys are trying to explain. I was trying to get the image resolution to check whether the image resolution is fine for printing and as I know that an image should be of at least of 300 resolution for a good print quality. So Can I judge this scenario by the resolution that I obtain from itext ?? – Abhinav Aug 25 '15 at 05:52
  • If iText tells you that the image is 3.6 inch wide on the page and the image tells you that the width in pixels is 450, then you have sufficient info to calculate the number of pixels per inch, just divide 450 by 3.6 and you know the resolution of the image in that specific context. – Bruno Lowagie Aug 25 '15 at 06:07
  • yeah which means in this case, the dpi comes around 125 and thats not the ideal resolution for printing, rite? – Abhinav Aug 25 '15 at 06:11

0 Answers0