PDFbox 找不到字体:/Helv [英] PDFbox Could not find font: /Helv
问题描述
我尝试将表单域添加到现有 PDF 文件,但出现以下错误 PDFbox 找不到字体:/Helv
I try to add form fields to existing PDF file but the following error appears PDFbox Could not find font: /Helv
我在 Java 中的代码具有以下视图:
My code in Java has the following view:
PDDocument pdf = PDDocument.load(inputStream);
PDDocumentCatalog docCatalog = pdf.getDocumentCatalog();
PDAcroForm acroForm = docCatalog.getAcroForm();
PDPage page = pdf.getPage(0);
PDTextField textBox = new PDTextField(acroForm);
textBox.setPartialName("SampleField");
acroForm.getFields().add(textBox);
PDAnnotationWidget widget = textBox.getWidgets().get(0);
PDRectangle rect = new PDRectangle(0, 0, 0, 0);
widget.setRectangle(rect);
widget.setPage(page);
widget.setAppearance(acroForm.getFields().get(0).getWidgets().get(0).getAppearance());
widget.setPrinted(false);
page.getAnnotations().add(widget);
acroForm.refreshAppearances();
acroForm.flatten();
pdf.save(outputStream);
pdf.close();
您知道为什么会出现异常吗?
Do you have any ideas why the exception appears?
有栈顶跟踪
java.io.IOException: Could not find font: /Helv
at org.apache.pdfbox.pdmodel.interactive.form.PDDefaultAppearanceString.processSetFont(PDDefaultAppearanceString.java:179)
at org.apache.pdfbox.pdmodel.interactive.form.PDDefaultAppearanceString.processOperator(PDDefaultAppearanceString.java:132)
at org.apache.pdfbox.pdmodel.interactive.form.PDDefaultAppearanceString.processAppearanceStringOperators(PDDefaultAppearanceString.java:108)
at org.apache.pdfbox.pdmodel.interactive.form.PDDefaultAppearanceString.<init>(PDDefaultAppearanceString.java:86)
at org.apache.pdfbox.pdmodel.interactive.form.PDVariableText.getDefaultAppearanceString(PDVariableText.java:93)
at org.apache.pdfbox.pdmodel.interactive.form.AppearanceGeneratorHelper.<init>(AppearanceGeneratorHelper.java:100)
at org.apache.pdfbox.pdmodel.interactive.form.PDTextField.constructAppearances(PDTextField.java:262)
at org.apache.pdfbox.pdmodel.interactive.form.PDAcroForm.refreshAppearances(PDAcroForm.java:368)
at com.workjam.service.impl.PDFService.fillForm(PDFService.java:85)
这是 PDF 的链接:https:///drive.google.com/file/d/0B2--NSDOiujoR3hOZFYteUl2UE0/view?usp=sharing
Here is the link for PDF: https://drive.google.com/file/d/0B2--NSDOiujoR3hOZFYteUl2UE0/view?usp=sharing
推荐答案
您的新文本字段没有默认外观,因此 PDFBox 为您制作了一个 (/Helv 0 Tf 0 g
).
Your new text field doesn't have a default appearance, so PDFBox makes one for you (/Helv 0 Tf 0 g
).
解决方案 1:从您正在使用的字段中获取它(这不适用于每个 PDF,因为您做出了几个假设,即有一个字段并且它是一个文本字段)
Solution 1: get it from the field you're using (this will not work with every PDF because you're making several assumptions, i.e. that there is a field and that it is a text field)
textBox.setDefaultAppearance(((PDTextField)acroForm.getFields().get(0)).getDefaultAppearance());
方案二:初始化默认资源:
Solution 2: initialize the default resources:
PDResources resources = new PDResources();
resources.put(COSName.getPDFName("Helv"), PDType1Font.HELVETICA);
acroForm.setDefaultResources(resources);
另见 CreateSimpleForm.java 源代码下载示例.
See also the CreateSimpleForm.java example from the source code download.
更新:这已在 2.0.8 中修复,请参阅问题 PDFBOX-3943.
Update: this has been fixed in 2.0.8, see issue PDFBOX-3943.
这篇关于PDFbox 找不到字体:/Helv的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!