我正在尝试使用来自fdf或xfdf的汉字填充pdf字段。
到目前为止,我已经尝试了pdftk,mcpdf,pdfbox和fpdm。
他们都可以将角色带入该字段,但不会显示。当我单击该字段进行编辑时,字符将按预期显示,但是当我再次单击该字段时,它们将消失。如果输入英文,它们显示不正确,例如“ hello”变成“ IFMMP”。
这一切都使我怀疑这是字体/字符图的问题,我尝试将完整字体嵌入到pdf中,并且没有区别。我已经在计算机上安装了字体,但没有成功。
如果我编辑pdf并将其填写在Acrobat中,则它可以接受中文字符而不会出现问题,并且可以在阅读器中查看pdf。我曾尝试在同一台Windows计算机上从命令行使用pdftk,但遇到了同样的问题。
我需要它在Linux环境中工作,最好在python或通过命令行脚本中工作,但实际上,我现在想看看它是否可以工作!我已经附上了示例pdf,fdf,xfdf以及它正在创建的输出,由于我用尽了所有想法,任何帮助将不胜感激。我一直在使用命令:
"pdftk test_form.pdf fill_form test.xfdf output output.pdf verbose"
填写表单字段时,将填充字段值,并且(可选)将生成表单字段的视觉外观,以反映新设置的值。因此,当您单击表单字段时看到该值的原因是,将显示字段值,但只要未激活该字段,就使用字段外观。
如果您尝试使用PDFBox 1.8设置值,则可以尝试使用PDFBox 2.0,因为它现在支持Unicode,并且外观生成已重做。
您还需要确保表单所使用的字体在填充表单的系统上可用。否则,使用PDFBox 2.0可能会收到类似于以下内容的错误消息
Warning: Using fallback font 'TimesNewRomanPSMT' for 'MingLiU'
Exception in thread "main" java.lang.IllegalArgumentException: No glyph for U+5185 in font MingLiU
这是因为MingLiU在系统上不可用,已被TimesNewRomanPSMT取代,后者没有所需的字符。
作为另一种解决方案,您还可以指示Adobe Reader使用以下命令打开表单时为您计算外观
PDAcroForm form = doc.getDocumentCatalog().getAcroForm();
form.setNeedAppearances(true);
再次使用PDFBox 2.0
我使用PDFBox 2创建了一个小示例,但从头开始创建了一个表单来测试它是否可以处理中文文本
// create a new PDF document
PDDocument doc = new PDDocument();
PDPage page = new PDPage();
// add a new AcroForm and add that to the document
PDAcroForm form = new PDAcroForm(doc);
doc.getDocumentCatalog().setAcroForm(form);
// Add and set the resources and default appearance at the form level
PDFont font = PDType0Font.load(doc, new File("/Library/Fonts/Arial Unicode.ttf"));
PDResources res = new PDResources();
COSName fontName = res.add(font);
form.setDefaultResources(res);
String da = "/" + fontName.getName() + " 12 Tf 0 g";
form.setDefaultAppearance(da);
// add a page to the document
doc.addPage(page);
// add a form field to the form
PDTextField textBox = new PDTextField(form);
textBox.setPartialName("Chinese");
form.getFields().add(textBox);
// specify the annotation associated with the field
// and add it to the page
PDAnnotationWidget widget = textBox.getWidget();
PDRectangle rect = new PDRectangle(100f,300f,120f,350f);
widget.setRectangle(rect);
page.getAnnotations().add(widget);
// set the field value
textBox.setValue("木兰辞");
doc.save("ChineseOut.pdf");
效果很好。不幸的是,我还用您正在使用的字体进行了测试,因为MingLiU是TrueType集合,PDFBox当时无法处理它,这是一个错误。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句