2

I'm having some trouble placing an image in the bottom-left corner of a PDF document.

Here's my code:

 PdfReader reader = new PdfReader("source.pdf");
 PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(pdfTarget));

 Image qrImg = Image.getInstance("qrcode.png");
 qrImg.setAbsolutePosition(0,0);

 // place the image at the i-th page
 PdfContentByte content = stamper.getOverContent(i);
 content.addImage(qrImg);

This works for almost every pdf document I tried unless a single one that you can find here: https://ufile.io/50016

For this document the bottom left corner starts at (50,50) so the absolute position should be (50,50) that is incorrect for all the others pdfs.

I can't find a way to place the image at (0,0) or any other fixed absolute position that results in placing it always at the bottom left corner. Any advice?

redgiun
  • 145
  • 1
  • 1
  • 9

1 Answers1

3

Pdf documents describe the page with a key called MediaBox. This is the raw size of the page. There's another key called CropBox that defines the visible area of the page. In your document the cropbox start at 54,55.4 and that's the offset you'll have to apply to the image. Check PdfReader.getCropBox() to get the dimensions.

Paulo Soares
  • 1,896
  • 8
  • 21
  • 19