How to embed font after flattening template using iText?

怎甘沉沦 提交于 2019-12-11 00:11:27

问题


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:

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!