【发布时间】:2021-11-01 18:21:59
【问题描述】:
我使用 itext 7.1.9 库创建了一个带有文本可填写字段的 PDF 文档。 PdfTextFormField 包含多语言文本。创建 PDF 文档后,我在 Adobe Acrobat Reader 中打开它,非拉丁符号从文本字段中消失,我只看到拉丁符号,但如果我单击该字段,整个文本将可见,包括非拉丁符号。
[! The PDF text field after openning document]1。 [! The PDF text field after clicking to the field]2。为了创建 PDF 文档,我使用如下代码:
public class Main {
public static void main(String[] args) throws IOException, URISyntaxException {
FontProviderAndFormFieldExample app = new FontProviderAndFormFieldExample();
app.createPdf("Test1.pdf");
app.fillExample("Test1.pdf", "Result.pdf", Paths.get(Main.class.getResource("/fonts").toURI()).toString());
}
public static class FontProviderAndFormFieldExample {
public String FIELDNAME = "test";
public Rectangle FIELDRECT = new Rectangle(50,300,300,20);
public String FIELDVALUE = "ПриветHello";
public void createPdf(String dest) throws IOException {
PdfWriter writer = new PdfWriter(dest);
PdfDocument pdfDoc = new PdfDocument(writer);
Document doc = new Document(pdfDoc);
Paragraph para = new Paragraph("Test document for multi-font appearance in a text formfield");
doc.add(para);
PdfAcroForm acroForm = PdfAcroForm.getAcroForm(pdfDoc,true);
PdfTextFormField ff = PdfFormField.createText(pdfDoc,FIELDRECT,"test", FIELDVALUE);
ff.setMultiline(true);
ff.setScroll(true);
acroForm.addField(ff,pdfDoc.getFirstPage());
PdfCanvas pdfCanvas = new PdfCanvas(pdfDoc.getFirstPage());
pdfCanvas.setLineWidth(1f).setStrokeColor(ColorConstants.BLUE).rectangle(FIELDRECT).stroke();
doc.close();
}
public void fillExample(String src, String dest, String srcf) throws IOException, URISyntaxException {
PdfReader reader = new PdfReader(src);
PdfWriter writer = new PdfWriter(dest);
PdfDocument pdfDoc = new PdfDocument(reader,writer);
PdfAcroForm acroForm = PdfAcroForm.getAcroForm(pdfDoc,true);
PdfFormField ff = acroForm.getField(FIELDNAME);
String filename = Main.class.getResource("/fonts/arial unicode.ttf").toURI().toString();
final PdfFont font = PdfFontFactory.createFont(filename, PdfEncodings.UTF8, false);
ff.setFont(font).setValue(FIELDVALUE);
pdfDoc.close();
}
}
}
我试图解决这个问题,我什至找到了the article on itext blog,但它对我没有帮助。我知道使用 ff.setNeedAppearence(true) 方法,但我不能使用它,因为它破坏了我应用程序的另一部分。而且我无法设置PdfEncoding.IDENTITY_H,因为它仅嵌入了以编程方式包含在该字段中的符号子集,并且用户无法填写此字段。
有人可以帮助我吗?我做错了什么?
【问题讨论】:
-
嗨,你试过 7.1.16 吗?考虑到季度发布时间表,这几乎提前了 2 年
-
@AlexeySubach 我试过 7.1.16。我得到了同样的结果。