I am using ApachePdfBox for PDF processing. I am trying to draw an icon onto the of the PDF image but it always ends up by putting the icon on bottom left of the PDF image. I am not able to figure it out how to calculate the right coordinates and resolution of PDF. It is working perfectly with one file while failing for the attached one.
try (PDDocument document = PDDocument.load(pdfInputStream)) {
PDPage page = document.getPage(0);
try (PDPageContentStream contentStream = new PDPageContentStream(document, page, PDPageContentStream.AppendMode.APPEND, false)) {
PDImageXObject imageXObject = PDImageXObject.createFromFileByContent(imageFile);
int pdfWidth = (int) page.getMediaBox().getWidth();
int pdfHeight = (int) page.getMediaBox().getHeight();
contentStream.drawImage(imageXObject, pdfWidth, pdfHeight);
}
}
Path tempFile = tempDirectory.newTempFile(".pdf");
document.save(tempFile.toFile());
You can find the PDF over here