0

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

Munish Dhiman
  • 511
  • 1
  • 7
  • 22
  • 1
    Try adding a fifth parameter (true) to the PDPageContentStream constructor. (Your question is a duplicate, but I'm too busy now :-)) – Tilman Hausherr Aug 21 '18 at 07:28
  • Possible duplicate of [PDFBox : PDPageContentStream's append mode misbehaving](https://stackoverflow.com/a/27919730/1729265) or [How to draw a string at a specific position on a pdf page in java using pdfbox?](https://stackoverflow.com/a/24992794/1729265) – mkl Aug 21 '18 at 09:53
  • Furthermore, you ignore that the lower left needs not be the original of the coordinate system. It is so here and it is so often, but for a general solution you also need to take the lower left coordinates of the crop box into account. – mkl Aug 21 '18 at 09:58

0 Answers0