0
votes

I am using the very useful PDFBox to build a simple pdf stamping GUI.

I noticed a serious issue with a particular document however.

When I specify a particular scale factor for the rendering, the expected output image size is different.

What is worse? the scaling factor used for the resultant image along the horizontal axis is different from that along the vertical axis.

Here is the code I used:

/**
 * @param pdfPath The path to the pdf document
 * @param page The pdf page number(is zero based)
 */
public BufferedImage loadPdfImage(String pdfPath, int page) {
    File file = new File(pdfPath);

    try (PDDocument doc = PDDocument.load(file)) {

        pageCount = doc.getNumberOfPages();
        PDPage pDPage = doc.getPage(page);

       float w = pDPage.getCropBox().getWidth();
       float h = pDPage.getCropBox().getHeight();

       System.out.println("Pdf opening: width: "+w+", height: "+h);


        PDFRenderer renderer = new PDFRenderer(doc);

        float dpiRatio =  1.5f;

        BufferedImage img = renderer.renderImage(page, dpiRatio);

 float dpiXRatio = img.getWidth() / w;
 float dpiYRatio = img.getHeight()/ h;


       System.out.println("dpiXRatio: "+dpiXRatio+", dpiYRatio: "+dpiYRatio);

        return img;
    } catch (IOException ex) {
        System.out.println( "invalid pdf found. Please check");
    }

    return null;
}

The code above loads most pdf documents that I have tried it on and converts given pages within them to BufferedImage objects.

For the said document however, it seems to be unable to render the converted image at the supplied scale-factor.

Is there anything wrong with my code? or is it a known bug?

Thanks.

EDIT

I am using PDFBOX v2.0.15

And the page has no rotation.

1
Please tell what PDFBox version you are using and share your file. Consider that the page may have a rotation. - Tilman Hausherr
@TilmanHausherr I believe the error was mine. I used the MediaBox rectangle to compute the scale factor. The MediaBox and the CropBox was not same for that file, and I used the MediaBox for the calculation. Using the cropbox, it is much better, though not exactly the same. Scale-factor I supplied: 2.0416667 Observable scale-factors from pdfbox: using cropbox: along x = 2.0415506, along y = 2.0414271 using mediabox: along x = 1.029426, along y = 1.0878998 Are these little differences normal? or can I ignore them? Also the page has no rotation. - gbenroscience
I think the differences are negligible enough to ignore. Thanks @TilmanHausherr - gbenroscience
Yes very small differences can occur because rendered image sizes are integers. - Tilman Hausherr
Don't forget to update to the current version, 2.0.17, there are always many issues fixed, some with speed, some with quality, some with security. If you're using maven, use the maven-versions-plugin which helps you to stay up to date. - Tilman Hausherr

1 Answers

1
votes

The error was mine; for the most part.

I had used the MediaBox to compute the scale factors and unfortunately the MediaBox and CropBox of the pdf file in question were not the same.

For example:

cropbox-rect: [8.50394,34.0157,586.496,807.984]
mediabox-rect: [0.0,0.0,595.0,842.0]

After making corrections for these, the scale-factors matched better along both axes, save for the errors due to the fact that the image sizes are integer numbers.

This is negligible enough for me to neglect, though.

When stamping, all I had to do was to make the necessary corrections for the cropbox. For example to draw the image(stamp) at P(x,y), I would do:

        x += cropBox.getLowerLeftX();
        y += cropBox.getLowerLeftY();

before calling the draw image functionality.

It all came out fine!