Modifying AcroForm
Contents
[
Hide
]
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.
- Open the source PDF Document.
- Iterate through page form resources and locate Typewriter forms.
- 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.
- Create a FormEditor facade and bind the source PDF.
- Set the maximum length for the target field.
- 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.
- Open the source PDF Document.
- Access the target field from the form collection.
- 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.
- Open the source PDF Document.
- Access the target TextBoxField and set a new default appearance.
- 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.
- Open the source PDF Document.
- Delete the target field from the form by its name.
- 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());
}
}