无法将图像添加到扫描的PDF或从HTML转换的PDF中。

huangapple go评论67阅读模式
英文:

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生成器首先添加一个覆盖整个页面的白色矩形。在这种文档的情况下,您在下面添加的内容也是不可见的。

改用OverContentstamp.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.

huangapple
  • 本文由 发表于 2023年3月9日 22:25:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/75685893.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定