My aim is it to draw an uploaded image of which I do not know the dimensions on a PDF file with one empty page (DIN A4). For horizontal images I have a PDF file with one horizontal empty page and for vertical images I have a PDF file with one vertical page.
This is my code so far:
File image = convertMultipartFileToFile(file); //I get a MultipartFile from my RequestParam (Spring) - converting works fine
BufferedImage awtImage = ImageIO.read(image);
String path = "";
if (awtImage.getWidth() > awtImage.getHeight()) {
path = MyController.class.getResource("/pdf4ImageUploadHorizontal.pdf").getPath();
} else {
path = MyController.class.getResource("/pdf4ImageUploadVertical.pdf").getPath();
}
pdf = new File(path);
PDDocument doc = PDDocument.load(pdf);
PDPage page = doc.getPage(0);
int actualPDFWidth = 0;
int actualPDFHeight = 0;
if (awtImage.getWidth() > awtImage.getHeight()) {
actualPDFWidth = (int) PDRectangle.A4.getHeight();
actualPDFHeight = (int) PDRectangle.A4.getWidth();
} else {
actualPDFWidth = (int) PDRectangle.A4.getWidth();
actualPDFHeight = (int) PDRectangle.A4.getHeight();
}
// Add image to page
PDImageXObject pdImage = PDImageXObject.createFromFileByContent(image, doc);
Dimension scaledDim = getScaledDimension(new Dimension(pdImage.getWidth(), pdImage.getHeight()), new Dimension(actualPDFWidth, actualPDFHeight)); // I'm using this function: https://stackoverflow.com/questions/23223716/scaled-image-blurry-in-pdfbox
PDPageContentStream contentStream = new PDPageContentStream(doc, page);
contentStream.drawImage(pdImage, 0, 0, scaledDim.width, scaledDim.height);
contentStream.close();
doc.save("c:\\xyz\\pdf.pdf");
For vertical images everything works fine (I would prefer the images to be centered on the page but that'd be the next step).
Problem is with horizontal images: instead of my uploaded horizontal image filling the complete horizontal pdf page I get a horizontal pdf page with my image on the left being rotated 90° to the right and fitting from top to bottom (scaling worked but not the way I hoped for):
My wish is to insert uploaded horizontal or vertical pictures correctly without rotation into to the intended PDF page.