java 使用 PDFBox 填写 PDF 表单不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13452351/
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
PDF form fill with PDFBox doesn't work
提问by stojke
I have a PDF file with some form fields that I need to fill in from Java code. I use PDFBox library for this, and this code:
我有一个 PDF 文件,其中包含一些需要从 Java 代码填写的表单字段。我为此使用 PDFBox 库,以及此代码:
PDDocument pdfDoc = PDDocument.load("C:\Users\igor\Desktop\test.pdf");
PDDocumentCatalog docCatalog = pdfDoc.getDocumentCatalog();
PDAcroForm acroForm = docCatalog.getAcroForm();
PDField field = acroForm.getField("applicationPrepaid[0].#pageSet[0].Pagina1[0].txtFirstName[0]");
if (field != null) {
field.setValue("Milan");
} else {
System.err.println("No field found with name:" + "applicationPrepaid[0].#pageSet[0].Pagina1[0].txtFirstName[0]");
}
pdfDoc.save("C:\Users\igor\Desktop\testout.pdf");
pdfDoc.close();
The PDF is not created by me, so I don't know what type of form the file uses (if I understand correctly, there are FDF and XFA forms). Since the PDF is not created by me, I used this tool http://support.persits.com/pdf/demo_formfields.aspto find out the names of the form fields, and it gave me this:
PDF 不是我创建的,所以我不知道文件使用什么类型的表单(如果我理解正确,有 FDF 和 XFA 表单)。由于 PDF 不是我创建的,我使用这个工具http://support.persits.com/pdf/demo_formfields.asp找出表单字段的名称,它给了我这个:
applicationPrepaid[0].#pageSet[0].Pagina1[0].txtFirstName[0]
When I use this long field name, I don't get any errors, but the resulting PDF does not contain the value I put in the field. I thought that maybe there was something wrong with the field name, so I used Pdftk toolwhich gave me just txtFirstName
for the field name. But when I use just that, I get the No field found with name: txtFirstName
error. Help?
当我使用这个长字段名时,我没有收到任何错误,但生成的 PDF 不包含我在该字段中输入的值。我想可能是字段名有问题,所以我使用了Pdftk 工具,它只给了我txtFirstName
字段名。但是当我只使用它时,我得到了No field found with name: txtFirstName
错误。帮助?
回答by sam9046
Well I realise this question is very old now, but I stumbled across it and thought I should post my solution.
好吧,我意识到这个问题现在已经很老了,但我偶然发现了它,并认为我应该发布我的解决方案。
You can find out the id's of the fields in the PDF form using PDFBox. It has a very rich API, it just unfortunately requires lots of reading to figure out what you have to do.
您可以使用 PDFBox 找出 PDF 表单中字段的 ID。它有一个非常丰富的 API,不幸的是,它需要大量阅读才能弄清楚你必须做什么。
To get the name of the form field, you want to use getFullyQualifiedName
which is part of the PDField
class.
要获取表单字段的名称,您需要使用getFullyQualifiedName
which 是PDField
类的一部分。
Unfortunately, you can't get all the filenames in one go (that I can tell) from the PDAcroform
class, so you can simply create an array of the form names and then loop through each one.
不幸的是,您无法从PDAcroform
类中一次性获得所有文件名(我可以说),因此您可以简单地创建一个表单名称数组,然后遍历每个文件名。
e.g.
例如
// Load the pdfTemplate
pdfTemplate = PDDocument.load(file);
PDDocumentCatalog docCatalog = pdfTemplate.getDocumentCatalog();
PDAcroForm acroForm = docCatalog.getAcroForm();
// Get field names
List<PDField> fieldList = acroForm.getFields();
// String the object array
String[] fieldArray = new String[fieldList.size()];
int i = 0;
for (PDField sField : fieldList) {
fieldArray[i] = sField.getFullyQualifiedName();
i++;
}
// Loop through each field in the array and do something
for (String f : fieldArray) {
PDField field = acroForm.getField(f);
System.out.println("f is: " + f);
if (f.contains("EXAMPLE FORM FIELD NAME")) {
DO SOMETHING
String value = "example value";
field.setValue(value);
System.out.println("printed: " + value + " to: " + f);
}
}
// Save edited file
pdfTemplate.save(sPdfTemplate);
pdfTemplate.close();
Hope this helps someone.
希望这可以帮助某人。
Cheers
干杯