英文:
Cannot add image to scanned Pdf or Pdf converted from Html
问题
使用 iText(com.lowagie.itext.2.1.7),我有这段代码,用于将一个小图像添加到 PDF 中。它适用于简单的 PDF 文件,但当我从 HTML 创建 PDF 或者 PDF 是扫描文档时,就不起作用。
private final Map<String, Image> images = new HashMap<String, Image>();
images.put("NL" + i, getBlackAndWhiteBarCode("/img/NotLast0" + i + ".png", posX, posY));
images.put("L" + i, getBlackAndWhiteBarCode("/img/Last0" + i + ".png", posX, posY));
public Image getBlackAndWhiteBarCode(String filename, float posX, float posY) throws IOException, DocumentException {
Image im = Image.getInstance(this.getClass().getResource(filename));
im.setOriginalType(Image.ORIGINAL_PNG);
im.setAbsolutePosition(posX, posY);
im.scalePercent(25);
return im;
}
...
PdfContentByte under = stamp.getUnderContent(pageOfPDF);
under.addImage(images.get(feuilletCpt < feuilletTot ? "NL" + seq : "L" + seq), true);
示例图像:输入图像描述
示例 PDF:通过 https://html2pdf.com/ 将简单的 HTML 转换为 PDF。
它适用于各种类型的 PDF 文件,包括生成的和下载的,但在将 HTML 转换为 PDF 或文档扫描为 PDF 时不起作用。
感谢您的帮助。
英文:
Using iText (com.lowagie.itext.2.1.7), I have this code that adds a little image to a Pdf. I works with simple Pdf files, but not when I create the Pdf from Html or when the Pdf is a scanned document.
private final Map<String, Image> images = new HashMap<String, Image>();
images.put("NL" + i, getBlackAndWhiteBarCode("/img/NotLast0" + i + ".png", posX, posY));
images.put("L" + i, getBlackAndWhiteBarCode("/img/Last0" + i + ".png", posX, posY));
public Image getBlackAndWhiteBarCode(String filename, float posX, float posY) throws IOException, DocumentException {
Image im = Image.getInstance(this.getClass().getResource(filename));
im.setOriginalType(Image.ORIGINAL_PNG);
im.setAbsolutePosition(posX, posY);
im.scalePercent(25);
return im;
}
...
PdfContentByte under = stamp.getUnderContent(pageOfPDF);
under.addImage(images.get(feuilletCpt < feuilletTot ? "NL" + seq : "L" + seq), true);
Example image : enter image description here
Example Pdf : a simple html converted to Pdf by https://html2pdf.com/
It works with every kind of Pdf files, generated, downloaded, but not when I convert an Html to Pdf or when a document is Scanned to Pdf.
Thank you for your help
答案1
得分: 0
你添加到UnderContent:
PdfContentByte under = stamp.getUnderContent(pageOfPDF);
under.addImage(images.get(feuilletCpt < feuilletTot ? "NL" + seq : "L" + seq), true);
因此,只有在没有先前内容的地方才能看到添加的内容。在扫描文档的情况下,通常会有一张覆盖整个页面的图像,因此您在下面添加的内容是不可见的。一些PDF生成器首先添加一个覆盖整个页面的白色矩形。在这种文档的情况下,您在下面添加的内容也是不可见的。
改用OverContent:stamp.getOverContent(pageOfPDF)
另一个可能的问题:您显示的代码没有考虑页面的坐标系。您可能需要将页面裁剪框的左下角添加到您的坐标中。
英文:
You add to the UnderContent:
PdfContentByte under = stamp.getUnderContent(pageOfPDF);
under.addImage(images.get(feuilletCpt < feuilletTot ? "NL" + seq : "L" + seq), true);
Thus, the addition can only be seen where there is no previous content. In case of scanned documents there usually is a page-covering image, so your addition underneath is not visible. Some PDF generators first add a page covering white rectangle. In case of such documents, your addition underneath also is not visible.
Use the Overcontent instead: stamp.getOverContent(pageOfPDF)
Another possible issue: The code you show does not take the coordinate system of the page into account. You may have to add the lower left corner of the page crop box to your coordinates.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论