Fill AcroForm - Fill PDF Form using Java
Contents
[
Hide
]
The Form facade can be used to automate field population in an existing AcroForm.
Fill AcroForm fields with new values
- Open the PDF form document with the Form facade.
- Iterate through the form fields and update the matching entries with the provided values.
- Save the updated PDF document.
public static void fillForm(Path inputFile, Path outputFile) {
Map<String, String> newFieldValues = Map.of(
"First Name", "Alexander_New",
"Last Name", "Greenfield_New",
"City", "Yellowtown_New",
"Country", "Redland_New");
Form form = new Form(inputFile.toString());
try {
for (String fieldName : form.getFieldNames()) {
if (newFieldValues.containsKey(fieldName)) {
form.fillField(fieldName, newFieldValues.get(fieldName));
}
}
form.save(outputFile.toString());
} finally {
form.close();
}
}