2017-02-22 65 views
0

我现在试图修改只有文本内容的PDF文件。当我使用如何在其他PDF文件中使用字体? (itext7 PDF)

TextRenderInfo.getFont() 

它返回给我一个实际上是间接对象的字体。

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

在关闭PdfDocument时会引发这种情况。

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

我正在使用itext7。

感谢

回答

2

首先,从错误消息我看,你是不是使用最新版本的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)); 
+0

感谢您的回应,现在问题变成如何将“destPath”赋予textRenderInfo。我会试图弄清楚它!谢谢! –