[英]How to set a value in PDF form using Java pdfbox api
I need to set a value for PDF form using JAVA pdacroform api 我需要使用JAVA pdacroform API为PDF表单设置值
below the code for setting up a value for particular field in PDF file but it throws 下面的代码为PDF文件中的特定字段设置值,但是会抛出
Exception in thread "main" java.lang.NoClassDefFoundError: org/fontbox/afm/AFMParser 线程“主”中的异常java.lang.NoClassDefFoundError:org / fontbox / afm / AFMParser
despite of adding fontbox-1.7.jar 尽管添加了fontbox-1.7.jar
can any one help me out pls 有人可以帮我吗
import java.io.IOException;
import org.pdfbox.pdmodel.interactive.form.PDAcroForm;
import org.pdfbox.pdmodel.interactive.form.PDField;
import org.pdfbox.pdmodel.PDDocument;
import org.pdfbox.pdmodel.PDDocumentCatalog;
import org.pdfbox.exceptions.COSVisitorException;
import org.pdfbox.examples.AbstractExample;
public class SetField extends AbstractExample {
public void setField(PDDocument pdfDocument, String name, String value)
throws IOException {
PDDocumentCatalog docCatalog = pdfDocument.getDocumentCatalog();
PDAcroForm acroForm = docCatalog.getAcroForm();
PDField field = acroForm.getField(name);
if (field != null) {
field.setValue(value);
} else {
System.err.println("No field found with name:" + name);
}
}
public static void main(String[] args) throws IOException,
COSVisitorException {
SetField setter = new SetField();
setter.setField(args);
}
private void setField(String[] args) throws IOException,
COSVisitorException {
PDDocument pdf = null;
try {
if (args.length != 3) {
usage();
} else {
SetField example = new SetField();
pdf = PDDocument.load(args[0]);
example.setField(pdf, args[1], args[2]);
pdf.save(args[0]);
}
} finally {
if (pdf != null) {
pdf.close();
}
}
}
private static void usage() {
System.err
.println("usage: org.apache.pdfbox.examples.fdf.SetField <pdf-file> <field-name> <field-value>");
}
}
Thanks 谢谢
You are getting java.lang.NoClassDefFoundError because of missing jar with class definition. 由于缺少带有类定义的jar,因此您正在获取java.lang.NoClassDefFoundError。
You need to add FontBox jar to your classpath. 您需要将FontBox jar添加到您的类路径中。
According to your comments to @user1951544's answer you seem to use a very old PdfBox version 0.7.3. 根据您对@ user1951544答案的评论,您似乎使用了非常老的PdfBox版本0.7.3。 This very likely is not cooperating well with the current FontBox jar.
这很可能无法与当前的FontBox jar合作。 I would advice updating to the current state.
我建议更新到当前状态。
In that case you should also consider the other hard dependencies required by fontbox: 在这种情况下,您还应该考虑fontbox所需的其他硬依赖项:
The main PDFBox component, pdfbox, has hard dependencies on the fontbox and jempbox components and the commons-logging library.
PDFBox的主要组件pdfbox对fontbox和jempbox组件以及commons-logging库具有严格的依赖性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.