Am 24.02.2016 um 16:28 schrieb Hartmann Toël:
Hi,

The example in exampes/util/PrintImageLocations.java in pdfbox-2.0.0-RC3 seems 
faulty.

Would this output more correct values?

              if ( xobject instanceof PDImageXObject) {
                    PDImageXObject image = (PDImageXObject)xobject;


                    int imageWidth = image.getWidth();
                    int imageHeight = image.getHeight();
                    
System.out.println("*******************************************************************");
                    System.out.println("Found image [" + objectName.getName() + 
"]");

                    Matrix ctmNew = 
getGraphicsState().getCurrentTransformationMatrix();
                    float scalingFactorX = ctmNew.getScalingFactorX();
                    float scalingFactorY = ctmNew.getScalingFactorY();
                    System.out.println("position = " + ctmNew.getTranslateX() + ", 
" + ctmNew.getTranslateY());
                    // size in pixel
                    System.out.println("size = " + imageWidth + "px, " + imageHeight + 
"px");
                    // size in page units
                    System.out.println("size = " + imageWidth*scalingFactorX + "pu, " + 
imageHeight*scalingFactorY + "pu");
                    // size in inches
                    scalingFactorX /= 72;
                    scalingFactorY /= 72;
                    System.out.println("size = " + imageWidth*scalingFactorX + "in, " + 
imageHeight*scalingFactorY + "in");
                    // size in millimeter
                    scalingFactorX *= 25.4;
                    scalingFactorY *= 25.4;
                    System.out.println("size = " + imageWidth*scalingFactorX + "mm, " + 
imageHeight*scalingFactorY + "mm");
                    System.out.println();
                    (…)

How to get the size of the image as it will be actually rendered? The scaling 
factor seem to be 1 even if the image is heavily scaled like in this example 
pdf:
http://files.m3lite.elanders.com/temp/image2.pdf

  I am trying to compute a DPI estimation for each included images, in order to 
have an estimation of the print result quality.


The code you quoted (and changed) is kindof hard to understand.

If an image would be rendered with a scaling 1 and a zero translation (1 0 0 1 0 0), it would appear as a single dot at the bottom left. So it has to be scaled, i.e. the current transformation matrix (CTM) must be set. A simple solution would be to set the CTM scale at the sizes of the image, and the translation (= move) at the wished position: (width 0 0 height xpos ypos)

I don't understand your changes and the output doesn't make sense. However here's some improved code which I intend to commit. The logic is the same, but the output text is different to be less confusing (hopefully). Please try it.

Matrix ctmNew = getGraphicsState().getCurrentTransformationMatrix();
                float imageXScale = ctmNew.getScalingFactorX();
                float imageYScale = ctmNew.getScalingFactorY();

// position in user space units. 1 unit = 1/72 inch at 72 dpi System.out.println("position in PDF = " + ctmNew.getTranslateX() + ", " + ctmNew.getTranslateY() + " in user space units");
                // raw size in pixels
System.out.println("raw image size = " + imageWidth + ", " + imageHeight + " in pixels");
                // displayed size in user space units
System.out.println("displayed size = " + imageXScale + ", " + imageYScale + " in user space units");
                // displayed size in inches at 72 dpi rendering
                imageXScale /= 72;
                imageYScale /= 72;
System.out.println("displayed size = " + imageXScale + ", " + imageYScale + " in inches at 72 dpi rendering");
                // displayed size in millimeters at 72 dpi rendering
                imageXScale *= 25.4;
                imageYScale *= 25.4;
System.out.println("displayed size = " + imageXScale + ", " + imageYScale + " in millimeters at 72 dpi rendering");

                System.out.println();

Here's an additional line from my answer at
https://stackoverflow.com/questions/5472711/dpi-of-image-extracted-from-pdf-with-pdfbox
but which I am starting to doubt, I suspect it is either wrong or incomplete because the dpi of an image would depend of the dpi of a rendering.

System.out.printf("dpi = %.0f dpi (X), %.0f dpi (Y) %n", image.getWidth() * 72 / ctmNew.getScalingFactorX(), image.getHeight() * 72 / ctmNew.getScalingFactorY());

And here's the output that you'd get with the new code:

Found image [Im0]
position in PDF = 196.97, 76.156296 in user space units
raw image size  = 600, 599 in pixels
displayed size  = 192.636, 630.630.492 in user space units
displayed size  = 2.6755, 8.756833 in inches at 72 dpi rendering
displayed size  = 67.957695, 222.42355 in millimeters at 72 dpi rendering
dpi  = 224 dpi (X), 68 dpi (Y)

The Y "dpi" value (68) makes sense because it is close to 72 dpi: your image has an Y pixel size of 599. You're streching it to 630, so it will be slightly less dpi than 72. The X makes sense too: the display size (192) is about 1/3, so the dpi is about 3x. 72 x 3 = 216 which is quite close to the 224.

Hope this helped!

Tilman


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to