Fill AcroForm - Fill PDF Form using Java

The Form facade can be used to automate field population in an existing AcroForm.

Fill AcroForm fields with new values

  1. Open the PDF form document with the Form facade.
  2. Iterate through the form fields and update the matching entries with the provided values.
  3. 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();
    }
}