PDFBox setValue() 不是为每个 PDTextField 设置数据。它节省了几个领域。它不适用于在 getFullyQualifiedName() 中具有相似外观的字段。
注意: field.getFullyQualifiedName() { customdutiesa, customdutiesb, customdutiesc } 它适用于 customdutiesa,但不适用于 customdutiesb 和 customdutiesc 等...
@Test
public void testb3Generator() throws IOException {
File f = new File(inputFile);
outputFile = String.format("%s_b3-3.pdf", "123");
try (PDDocument document = PDDocument.load(f)) {
PDDocumentCatalog catalog = document.getDocumentCatalog();
PDAcroForm acroForm = catalog.getAcroForm();
int i = 0;
for (PDField field : acroForm.getFields()) {
i=i+1;
if (field instanceof PDTextField) {
PDTextField textField = (PDTextField) field;
textField.setValue(Integer.toString(i));
}
}
document.getDocumentCatalog().getAcroForm().flatten();
document.save(new File(outputFile));
document.close();
}
catch (Exception e) {
e.printStackTrace();
}
}
输入 pdf 链接: https ://s3-us-west-2.amazonaws.com/kx-filing-docs/b3-3.pdf 输出 pdf 链接:https://kx-filing-docs.s3-us-west -2.amazonaws.com/123_b3-3.pdf