【问题标题】:How to use font in other PDF files? (itext7 PDF)如何在其他 PDF 文件中使用字体? (itext7 PDF)
【发布时间】:2017-07-13 05:35:14
【问题描述】:

我现在正在尝试修改仅包含文本内容的 PDF 文件。当我使用

TextRenderInfo.getFont()

它返回一个 Font,它实际上是一个间接对象。

pdf.inderect.object.belong.to.other.pdf.document.Copy.object.to.current.pdf.document 

在这种情况下会在关闭 PdfDocument 时抛出。

有没有办法让我在新的 PDF 文件中重复使用这个字体?或者,有没有办法就地编辑 PDF 中的文本内容(不更改字体、颜色、字体大小)?

我正在使用 itext7。

谢谢

【问题讨论】:

    标签: pdf fonts itext7


    【解决方案1】:

    首先,从错误消息中我看到您没有使用最新版本的 iText,目前是7.0.2。所以我建议你更新你的 iText 版本。

    其次,确实可以在另一个文档中使用一种字体。但要做到这一点,您首先必须将相应的字体对象复制到另一个文档(顺便说一下异常消息中所述)。但是应该警告您,这种方法有一些限制,例如如果是字体子集,您将只能使用源文档中原始字体子集中存在的字形,而不能使用其他字形。

    PdfFont font = textRenderInfo.getFont(); // font from source document
    PdfDocument newPdfDoc = ... // new PdfDocument you want to write some text to
    
    // copy the font dictionary to the new document
    PdfDictionary fontCopy = font.getPdfObject().copyTo(newPdfDoc); 
    
    // create a PdfFont instance corresponding to the font in the new document
    PdfFont newFont = PdfFontFactory.createFont(fontCopy);
    
    // Use newFont in newPdfDoc, e.g.:
    Document doc = new Document(newPdfDoc);
    doc.add(new Paragraph("Hello").setFont(newFont));
    

    【讨论】:

    • 感谢您的回复,现在问题变成了如何将“destPath”提供给 textRenderInfo。我会努力弄清楚的!谢谢!
    猜你喜欢
    • 2018-09-05
    • 2018-01-11
    • 2022-11-25
    • 1970-01-01
    • 1970-01-01
    • 2020-07-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多