Modifying AcroForm

Form maintenance often involves both field-level edits and cleanup of form-related page resources.

Clear text in embedded form resources

Use this example when Typewriter form content should be emptied without removing the form objects themselves.

  1. Open the source PDF Document.
  2. Iterate through page form resources and locate Typewriter forms.
  3. Clear the absorbed text fragments and save the document.
public static void clearTextInForm(Path inputFile, Path outputFile) {
    try (Document document = new Document(inputFile.toString())) {
        for (XForm form : document.getPages().get_Item(1).getResources().getForms()) {
            if ("Typewriter".equals(form.getIT()) && "Form".equals(form.getSubtype())) {
                TextFragmentAbsorber absorber = new TextFragmentAbsorber();
                absorber.visit(form);

                for (TextFragment fragment : absorber.getTextFragments()) {
                    fragment.setText("");
                }
            }
        }
        document.save(outputFile.toString());
    }
}

Set a text field length limit

Use this example when a text field should accept only a limited number of characters.

  1. Create a FormEditor facade and bind the source PDF.
  2. Set the maximum length for the target field.
  3. Save the updated document.
public static void setFieldLimit(Path inputFile, Path outputFile) {
    FormEditor form = new FormEditor();
    form.bindPdf(inputFile.toString());
    try {
        form.setFieldLimit("First Name", 15);
        form.save(outputFile.toString());
    } finally {
        form.close();
    }
}

Get a text field length limit

Use this example when you need to inspect the current maximum length of a text field.

  1. Open the source PDF Document.
  2. Access the target field from the form collection.
  3. Read the limit from the TextBoxField and output it.
public static void getFieldLimit(Path inputFile) {
    try (Document document = new Document(inputFile.toString())) {
        Field field = document.getForm().getFields()[0];
        if (field instanceof TextBoxField textBoxField) {
            System.out.println("Limit: " + textBoxField.getMaxLen());
        }
    }
}

Change a form field font

Use this example when an existing text field should use a different font or appearance.

  1. Open the source PDF Document.
  2. Access the target TextBoxField and set a new default appearance.
  3. Save the updated PDF.
public static void setFormFieldFont(Path inputFile, Path outputFile) {
    try (Document document = new Document(inputFile.toString())) {
        Field field = document.getForm().getFields()[0];
        if (field instanceof TextBoxField textBoxField) {
            textBoxField.setDefaultAppearance(new DefaultAppearance(
                    FontRepository.findFont("Calibri"), 10, com.aspose.pdf.Color.getBlack().toRgb()));
        }

        document.save(outputFile.toString());
    }
}

Delete a form field by name

Use this example when a specific field should be removed from the AcroForm.

  1. Open the source PDF Document.
  2. Delete the target field from the form by its name.
  3. Save the updated document.
public static void deleteFormField(Path inputFile, Path outputFile) {
    try (Document document = new Document(inputFile.toString())) {
        document.getForm().delete("First Name");
        document.save(outputFile.toString());
    }
}