我尝试使用以下代码使用iText设置表单字段值,但无法显示字符ӧ。 test-form-fill.pdf由Adobe LifeCycle Form Designer创建。请帮忙!
InputStream is = new FileInputStream("doc/test-form-fill.pdf");
OutputStream os = new FileOutputStream("doc/test-form-fill-done.pdf");
PdfReader reader = new PdfReader(is);
PdfStamper stamper = new PdfStamper(reader, os);
AcroFields form = stamper.getAcroFields();
form.setField("field1", "ӧ11111");
stamper.setFormFlattening(true);
stamper.close();
reader.close();
is.close();
os.close();
答案 0 :(得分:0)
ӧ
字符可能不会显示,因为用于表单字段的字体不知道如何绘制该字符。请查看导致FillFormSpecialChars2的form_special_chars.pdf示例:
您需要定义这样的替换字体:
public void manipulatePdf(String src, String dest) throws DocumentException, IOException {
PdfReader reader = new PdfReader(src);
PdfStamper stamper = new PdfStamper(reader,
new FileOutputStream(dest));
AcroFields fields = stamper.getAcroFields();
BaseFont bf = BaseFont.createFont(FONT, BaseFont.IDENTITY_H, BaseFont.EMBEDDED, false, null, null, false);
fields.setFieldProperty("Name", "textfont", bf, null);
fields.setField("Name", "\u04e711111");
stamper.setFormFlattening(true);
stamper.close();
}
就我而言,FONT
指的是FreeSans:
public static final String FONT = "resources/fonts/FreeSans.ttf";
FreeSans知道如何绘制ӧ
,但可以随意将FONT
替换为支持这些字符的任何其他字体的路径。
正如您所看到的,我还将ӧ
更改为\u04e7
,以保护您的代码免受编码问题的影响。