Itext embed font in a PDF

只愿长相守 提交于 2019-11-28 23:33:08

I'm almost sure that you got an answer by now, but maybe others would like to get a detailed view on the solution. Below is the sample java code I used to embed fonts in the generated PDF (useful only in some cases, as the size of the documents increases dramatically). As a free tool to create the PDF forms, I have used the OpenOffice writer, by adding forms inside the documents and exporting the documents as PDF files :

PdfReader pdfTemplate = new PdfReader(templateName);
ByteArrayOutputStream out = new ByteArrayOutputStream();
BaseFont unicode = BaseFont.createFont(unicodeFontPath, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
PdfStamper stamper = new PdfStamper(pdfTemplate, out);
//assigning the font to the field
stamper.getAcroFields().setFieldProperty("myField", "textfont", unicode, null);
stamper.getAcroFields().setField("myField", someValue);
stamper.close();
pdfTemplate.close();
PdfContentByte ab = yourPDFWriter.getDirectContent();
// field: PdfFormField.createTextField(...);
PdfAppearance ap = ab.createAppearance(320, 30); // size of field
field.setDefaultAppearanceString(ap);

That should do the trick.

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