问题
I want to auto generate some Chinese PDF docs using a template, this is what I have done:
- Make a template using Acrobat, set all
AcroField
s at correct places. - Set all
AcroField
's font toSTXiHei
. - Use Java and iText to fill these fields.
The codes are like:
BaseFont STXiHei = BaseFont.createFont(getResource("STXiHei.ttf"), BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
PdfReader reader = new PdfReader(getResource("template.pdf"));
FileOutputStream stream = new FileOutputStream("doc.pdf");
PdfStamper stamper = new PdfStamper(reader, stream);
AcroFields form = stamper.getAcroFields();
form.setField("Buyer_Name", "购货方");
stamper.setFormFlattening(true);
form.setFieldProperty("Buyer_Name", "textfont", STXiHei, null);
form.addSubstitutionFont(STXiHei);
stamper.close();
I have to use exactly font STXiHei
, the Chinese text in my template is in this font.
The problem is: I can't completely control my input text's font. Some apps can render correctly(Adobe Reader), some of them use substitution font(Chrome), worst of them crash(some Android PDF rendering lib).
I think this is because I didn't embed STXiHei
for my input text, this can be confirmed from font page:
You can see that the last one is different from others, and the encoding is different from what I input. So how can I fix this?
来源:https://stackoverflow.com/questions/34335481/how-to-embed-font-after-flattening-template-using-itext