0

I'm using the PDFBox library (see here) to convert an image to PDF. The goal is to have a image scaled to a full A4 page in the PDF file. And it works well, except one thing:

The image height and width seem to be mixed up. The width is always assumed to be the bigger value of them both. I have 2 images: One has the dimensions (according to the Windows file details) 4032x2268 (landscape) and the other one 2268x4032 (portrait).

When i load the images in PDFBox, the width is always 4032 and the height 2268. The goal is to create a landscape PDF for one and a portrait PDF for the other one. This weird "bug" (?) causes the portrait image to convert to a landscape PDF which of course causes the image to be rotated (which is inconventient).

Here's the relevant part of my code:

public byte[] imageToPDF(MultipartFile file) throws IOException {
        PDDocument pdf = new PDDocument();
        PDImageXObject pdImage = PDImageXObject.createFromByteArray(pdf, file.getBytes(), file.getOriginalFilename());
        // scale image to fit the full page
        PDPage page;
        int imageWidth;
        int imageHeight;
        if (pdImage.getWidth() > pdImage.getHeight()) {
            // landscape pdf
            float pageHeight = PDRectangle.A4.getWidth();
            float pageWidth = PDRectangle.A4.getHeight();
            page = new PDPage(new PDRectangle(pageWidth, pageHeight));
            imageWidth = (int)pageWidth;
            imageHeight = (int)(((double)imageWidth / (double)pdImage.getWidth()) * (double)pdImage.getHeight());
        } else {
            // portrait pdf
            float pageHeight = PDRectangle.A4.getHeight();
            float pageWidth = PDRectangle.A4.getWidth();
            page = new PDPage(new PDRectangle(pageWidth, pageHeight));
            imageHeight = (int)pageHeight;
            imageWidth = (int)(((double)imageHeight / (double)pdImage.getHeight()) * (double)pdImage.getWidth());
        }
        ...
    }

pdImage.getWidth() is always greater than pdImage.getHeight(), no matter which of the two images I use. Does anyone have an idea?

Philipp
  • 520
  • 3
  • 7
  • 1
    Have you checked whether your *landscape image* is actually a *portrait* one with a rotation value of 90 or 270? – mkl Dec 06 '20 at 16:06
  • @mkl Good hint! Where would I check that? I took the pictures with my phone, once in landscape and once in portrait mode, the Windows explorer file details show the correct height and width (already correctly rotated). Is there a "rotation" property in PDFBox? Something like PDImageXObject#getRotation? (can't find anything like that) – Philipp Dec 07 '20 at 09:32
  • Well, EXIF viewers should show that. E.g. http://metapicz.com/ (an early google match for "EXIF view" here) shows "Rotate 90 CW" (or whichever angle or mirror axis is there). For programmatic approaches see e.g. the answers to [this question](https://stackoverflow.com/q/21951892/1729265) in particular the [accepted one](https://stackoverflow.com/a/21981275/1729265). – mkl Dec 07 '20 at 13:40

0 Answers0