java Itext 在 PDF 中嵌入字体
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/626632/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Itext embed font in a PDF
提问by Milhous
I have a pdf that has been created using the Foxit form designer. On my design system, I have the barcode font installed. The barcode font is used in one of the AcroFields. It appears that foxit does not embed the font in the document.
我有一个使用 Foxit 表单设计器创建的 pdf。在我的设计系统上,我安装了条码字体。条形码字体用于 AcroField 之一。看起来 Foxit 没有在文档中嵌入字体。
I also have customers that do not have the barcode font installed in their computers, and thus I would like to embed the font into the PDF. Is there a way for me to embed a font that is used in the AcroFields into the PDF using iText?
我也有客户没有在他们的计算机中安装条码字体,因此我想将字体嵌入到 PDF 中。有没有办法让我使用 iText 将 AcroFields 中使用的字体嵌入到 PDF 中?
EDIT:The font seems to be included in the text, but not the fields, Therefore the font(barcode) will not print.
编辑:字体似乎包含在文本中,但不包含在字段中,因此不会打印字体(条形码)。
采纳答案by work.paul
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 :
我几乎可以肯定您现在已经得到了答案,但也许其他人希望获得有关解决方案的详细信息。下面是我用来在生成的 PDF 中嵌入字体的示例 java 代码(仅在某些情况下有用,因为文档的大小急剧增加)。作为创建 PDF 表单的免费工具,我使用了 OpenOffice writer,通过在文档中添加表单并将文档导出为 PDF 文件:
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();
回答by Pindatjuh
PdfContentByte ab = yourPDFWriter.getDirectContent();
// field: PdfFormField.createTextField(...);
PdfAppearance ap = ab.createAppearance(320, 30); // size of field
field.setDefaultAppearanceString(ap);
That should do the trick.
这应该够了吧。

