Read and Modify PDF Form Field Values
- 4 minutes to read
The PDF Document API allows you to read and modify values stored in AcroForm fields. Locate a field in the document before you access its value (see Access and Find PDF Form Fields).
Read and Modify a Text Field
Cast the field to TextBoxField to access its value.
The following code snippet reads and updates the value of a text field:
// Find the text field by its name.
FormField field = pdfDocument.getFields().findByName("FirstName");
if(field instanceof TextBoxField textBoxField) {
// Read the current value.
String value = textBoxField.getValue();
// Update the field value.
textBoxField.setValue("Alice");
}
Read and Modify a Check Box
Use getChecked() and setChecked(boolean value) methods to read or modify the check box state.
The following code snippet selects a check box:
// Find the check box field.
FormField field = pdfDocument.getFields().findByName("AgreeToTerms");
if(field instanceof CheckBoxField checkBoxField) {
// Select the check box.
checkBoxField.setChecked(true);
}
Read and Modify a Combo Box
A combo box stores one selected value.
The following code snippet updates the selected value:
// Find the combo box field.
FormField field = pdfDocument.getFields().findByName("Country");
if(field instanceof ComboBoxField comboBoxField) {
// Change the selected value.
comboBoxField.setValue("Canada");
// Read the current value.
String value = comboBoxField.getValue();
}
Tip
Use the ComboBoxField.getItems() method to access combo box items (ChoiceFieldItem objects).
Read and Modify a Radio Button Group
A radio button group stores the value of the selected option.
The following code snippet selects a radio button:
// Find the radio button group.
FormField field = pdfDocument.getFields().findByName("Gender");
if(field instanceof RadioGroupField radioGroupField) {
// Select an option.
radioGroupField.setValue("Female");
// Read the selected option.
String value = radioGroupField.getValue();
}
Read and Modify a List Box
Use list box selection methods to read or modify selected items.
The following code snippet selects an item in a list box:
// Find the list box field.
FormField field = pdfDocument.getFields().findByName("Country");
if(field instanceof ListBoxField listBoxField) {
// Select an item.
// listBoxField.selectValue("Germany");
// Select items.
listBoxField.setSelectedValues(Arrays.asList(
"USA",
"Canada"
));
// Get selected values.
for (String value : listBoxField.getSelectedValues()) {
System.out.println(value);
}
}
To select or clear a specific item by index, call the setSelected(int index, boolean value) method.
// Select the first item.
listBoxField.setSelected(0, true);
// Clear the second item.
listBoxField.setSelected(1, false);
If multiple item selection is enabled, use the setSelectedValues() method to select specific items:
listBoxField.setSelectedValues(List.of("USA", "Canada"));
Tip
Use the ListBoxField.getItems() method to access list box items (ChoiceFieldItem objects).
Read Signature Information
The following code snippet reads the signer information from a signed signature field:
// Find the signature field.
FormField field = pdfDocument.getFields().findByName("Signature");
if(field instanceof SignatureField signatureField) {
if(signatureField.getIsSigned()) {
// Read signature information.
String signer = signatureField.getSignerName();
String reason = signatureField.getReason();
String contact = signatureField.getContactInfo();
var signingTime = signatureField.getSigningTime();
}
}
Make a Form Field Read-Only
To prevent users from modifying a field, call the FormField.setReadOnly() method and pass true.
The following code snippet makes a field read-only:
// Find the field.
FormField field = pdfDocument.getFields().findByName("FirstName");
// Prevent users from editing the field.
field.setReadOnly(true);
Mark a Form Field as Required
Call the FormField.setRequired(boolean) method to mark a form field as required.
The following code snippet adds a mandatory Email field to the first page in a PDF document:
Page page = pdfDocument.getPages().getFirst();
// Create a mandatory text field.
TextBoxField emailField = new TextBoxField("Email");
emailField.setRequired(true);
pdfDocument.getFields().add(emailField);
// Bind the field to a widget and place it on the page.
RectangleF bounds = new RectangleF(120, 720, 220, 20);
TextBoxWidgetAnnotation widget = new TextBoxWidgetAnnotation(emailField, bounds);
widget.setFontSize(12);
page.getAnnotations().add(widget);
Note
The required flag marks a field as mandatory. PDF viewers and processors determine whether and how to highlight required fields and enforce this requirement during form submission.