How to embed font after flattening template using

2019-07-14 17:30发布

I want to auto generate some Chinese PDF docs using a template, this is what I have done:

  1. Make a template using Acrobat, set all AcroFields at correct places.
  2. Set all AcroField's font to STXiHei.
  3. 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:

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?

0条回答
登录 后发表回答