繁体   English   中英

itext将文本添加到页面中导致出现当前但不可见的文本-缺少字体?

[英]itext add text to a page result in present but invisible text - font missing?

我正在尝试使用以下代码在pdf上添加自定义文本。

它在大多数情况下都可以正常工作,但是对于某些pdf文件(主要是扫描的图像),该文本显示为“不可见”:我可以选择并复制该文本,但是它的不透明度为0。

        PdfReader pdfTemplate = new PdfReader(is);
        final PdfStamper stamper = PdfAStamper.createSignature(pdfTemplate, os, '\0');
        PdfContentByte content = stamper.getUnderContent(pageNumber);
        ColumnText ct = new ColumnText(content);
        ct.setSimpleColumn(llx, lly, urx, uly, 0, Element.ALIGN_CENTER);
        Font headerFont = new Font(Font.FontFamily.COURRIER, 7, Font.NORMAL);
        headerFont.setColor(BaseColor.BLACK);
        Chunk c = new Chunk("hello", headerFont);
        Paragraph p = new Paragraph(c);
        p.setAlignment(Element.ALIGN_CENTER);
        ct.addElement(p);
        ct.go();

我想知道pdf是否缺少字体。 运行pdffont "mypdf.pdf"返回错误:

Syntax Warning: May not be a PDF file (continuing anyway)
Syntax Error: Couldn't find trailer dictionary
Syntax Error: Couldn't read xref table

在我的代码中添加pdfTemplate.createFakeFontSubsets()导致以下结果:

name                                 type              encoding         emb sub uni object ID
------------------------------------ ----------------- ---------------- --- --- --- ---------
Times-Roman                          Type 1            WinAnsi          no  no  no      1  0

我猜该字体未嵌入,但以下内容也不起作用:

BaseFont courrier = BaseFont.createFont("courrier", BaseFont.CP1252, BaseFont.EMBEDDED);
Font headerFont = new Font(courrier, 7, Font.NORMAL);  

您是否知道发生了什么,我如何解决我的代码以使其适用于所有类型的pdf(包括扫描的pdf)?

(遗憾的是,我给您提供一个pdf示例,因为它发生在客户文档中,这有点复杂...)

这是罪魁祸首:

PdfContentByte content = stamper.getUnderContent(pageNumber);

您要在现有内容添加文本。 由于现有内容由不透明图像组成,因此存在文本,但被白色像素覆盖。

将行更改为:

PdfContentByte content = stamper.getOverContent(pageNumber);

您的问题将得到解决。

如果您不想完全覆盖现有内容,则可能需要使文本稍微透明。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM