gpt4 book ai didi

java - PDFbox 找不到字体 :/Helv

转载 作者:行者123 更新时间:2023-11-29 04:25:43 25 4
gpt4 key购买 nike

我尝试将表单域添加到现有 PDF 文件,但出现以下错误 PDFbox Could not find font:/Helv

我的 Java 代码具有以下 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();

您知道异常出现的原因吗?

有栈顶

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

最佳答案

您的新文本字段没有默认外观,因此 PDFBox 为您制作了一个(/Helv 0 Tf 0 g)。

解决方案 1:从您正在使用的字段中获取它(这不适用于每个 PDF,因为您做出了几个假设,即有一个字段并且它是一个文本字段)

textBox.setDefaultAppearance(((PDTextField)acroForm.getFields().get(0)).getDefaultAppearance());

方案二:初始化默认资源:

PDResources resources = new PDResources();
resources.put(COSName.getPDFName("Helv"), PDType1Font.HELVETICA);
acroForm.setDefaultResources(resources);

另见 CreateSimpleForm.java来自源代码下载的示例。

更新:这已在 2.0.8 中修复,请参阅问题 PDFBOX-3943 .

关于java - PDFbox 找不到字体 :/Helv,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46433388/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com