使用 iText 将图像放在 pdf 的左下角

Place image at bottom left corner of pdf with iText

我在将图像放在 PDF 文档的左下角时遇到了一些问题。

这是我的代码:

 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);

这几乎适用于我尝试过的所有 pdf 文档,除非您可以在此处找到一个:https://ufile.io/50016

对于此文档,左下角从 (50,50) 开始,因此绝对位置应该是 (50,50),这对于所有其他 pdf 都是不正确的。

我找不到将图像放置在 (0,0) 或任何其他固定绝对位置的方法,这会导致图像始终位于左下角。 有什么建议吗?

Pdf 文件用一个名为 MediaBox 的键来描述页面。这是页面的原始大小。还有另一个名为 CropBox 的键,它定义了页面的可见区域。在您的文档中,cropbox 从 54,55.4 开始,这是您必须应用于图像的偏移量。检查 PdfReader.getCropBox() 以获取尺寸。