CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/maven-org-apache-pdfbox--pdfbox

The Apache PDFBox library is an open source Java tool for working with PDF documents.

Pending
Overview
Eval results
Files

interactive-forms.mddocs/

Interactive Forms

Comprehensive support for PDF interactive forms (AcroForms) including text fields, checkboxes, radio buttons, choice lists, and form submission with full form manipulation capabilities.

Form Access and Management

Access and manipulate PDF forms and their fields.

// Form access methods in org.apache.pdfbox.pdmodel.PDDocument
public PDAcroForm getAcroForm();
public void setAcroForm(PDAcroForm acroForm);

// Form management methods in org.apache.pdfbox.pdmodel.interactive.form.PDAcroForm
public PDAcroForm(PDDocument document);
public PDAcroForm(PDDocument document, COSDictionary form);

public List<PDField> getFields();
public PDField getField(String fullyQualifiedName);
public void setFields(List<PDField> fields);
public void flatten() throws IOException;
public void refreshAppearances() throws IOException;

Field Operations

Common operations available for all form field types.

// Base field methods in org.apache.pdfbox.pdmodel.interactive.form.PDField
public String getPartialName();
public void setPartialName(String partialName);
public String getFullyQualifiedName();
public String getMappingName();
public void setMappingName(String mappingName);
public String getAlternateFieldName();
public void setAlternateFieldName(String alternateFieldName);

// Field value operations
public String getValue();
public void setValue(String value) throws IOException;
public String getDefaultValue();
public void setDefaultValue(String defaultValue);

// Field properties
public boolean isReadOnly();
public void setReadOnly(boolean readOnly);
public boolean isRequired();
public void setRequired(boolean required);
public boolean isNoExport();
public void setNoExport(boolean noExport);

// Widget annotations
public List<PDAnnotationWidget> getWidgets();
public void setWidgets(List<PDAnnotationWidget> widgets);

Text Fields

Handle single-line and multi-line text input fields.

// Constructor and methods in org.apache.pdfbox.pdmodel.interactive.form.PDTextField
public PDTextField(PDAcroForm acroForm);
public PDTextField(PDAcroForm acroForm, COSDictionary field, PDField parent);

// Text field specific properties
public boolean isMultiline();
public void setMultiline(boolean multiline);
public boolean isPassword();
public void setPassword(boolean password);
public boolean isFileSelect();
public void setFileSelect(boolean fileSelect);
public boolean isDoNotSpellCheck();
public void setDoNotSpellCheck(boolean doNotSpellCheck);
public boolean isDoNotScroll();
public void setDoNotScroll(boolean doNotScroll);

// Text formatting
public int getMaxLength();
public void setMaxLength(int maxLength);
public String getDefaultAppearance();
public void setDefaultAppearance(String defaultAppearance);

Checkboxes

Handle checkbox form fields with on/off states.

// Constructor and methods in org.apache.pdfbox.pdmodel.interactive.form.PDCheckBox
public PDCheckBox(PDAcroForm acroForm);
public PDCheckBox(PDAcroForm acroForm, COSDictionary field, PDField parent);

// Checkbox state management
public void check() throws IOException;
public void unCheck() throws IOException;
public boolean isChecked();
public String getOnValue();
public String getOffValue();
public void setValue(String value) throws IOException;

Radio Buttons

Handle radio button groups with multiple options.

// Constructor and methods in org.apache.pdfbox.pdmodel.interactive.form.PDRadioButton
public PDRadioButton(PDAcroForm acroForm);
public PDRadioButton(PDAcroForm acroForm, COSDictionary field, PDField parent);

// Radio button operations
public List<String> getExportValues();
public String getSelectedExportValue();
public void setValue(String value) throws IOException;
public List<PDCheckBox> getKids();

Choice Fields (Lists and Dropdowns)

Handle list boxes and dropdown/combo boxes.

// Constructor and methods in org.apache.pdfbox.pdmodel.interactive.form.PDChoice
public PDChoice(PDAcroForm acroForm);
public PDChoice(PDAcroForm acroForm, COSDictionary field, PDField parent);

// Choice options management
public List<String> getOptions();
public void setOptions(List<String> options);
public List<String> getOptionsExportValues();
public void setOptionsExportValues(List<String> optionsExportValues);
public List<String> getOptionsDisplayValues();
public void setOptionsDisplayValues(List<String> optionsDisplayValues);

// Selection operations
public List<Integer> getSelectedOptionsIndex();
public void setSelectedOptionsIndex(List<Integer> selectedOptionsIndex);
public List<String> getValue();
public void setValue(List<String> value) throws IOException;

// Choice field properties
public boolean isCombo();
public void setCombo(boolean combo);
public boolean isEdit();
public void setEdit(boolean edit);
public boolean isSort();
public void setSort(boolean sort);
public boolean isMultiSelect();
public void setMultiSelect(boolean multiSelect);
public boolean isDoNotSpellCheck();
public void setDoNotSpellCheck(boolean doNotSpellCheck);

List Boxes

Specialized choice field for list display.

// Constructor in org.apache.pdfbox.pdmodel.interactive.form.PDListBox
public PDListBox(PDAcroForm acroForm);
public PDListBox(PDAcroForm acroForm, COSDictionary field, PDField parent);

// List-specific properties inherited from PDChoice
public int getTopIndex();
public void setTopIndex(int topIndex);

Combo Boxes

Specialized choice field for dropdown/combo display.

// Constructor in org.apache.pdfbox.pdmodel.interactive.form.PDComboBox
public PDComboBox(PDAcroForm acroForm);
public PDComboBox(PDAcroForm acroForm, COSDictionary field, PDField parent);

// Combo-specific operations inherited from PDChoice
// Additional combo-specific behavior through base class methods

Signature Fields

Handle digital signature fields in forms.

// Constructor and methods in org.apache.pdfbox.pdmodel.interactive.form.PDSignatureField
public PDSignatureField(PDAcroForm acroForm);
public PDSignatureField(PDAcroForm acroForm, COSDictionary field, PDField parent);

// Signature operations
public PDSignature getSignature();
public void setSignature(PDSignature signature);
public byte[] getSignedByteRange() throws IOException;

Usage Examples

Accessing and Reading Form Fields

PDDocument document = Loader.loadPDF(new File("form.pdf"));
PDAcroForm acroForm = document.getAcroForm();

if (acroForm != null) {
    // Get all fields
    List<PDField> fields = acroForm.getFields();
    
    for (PDField field : fields) {
        System.out.println("Field name: " + field.getFullyQualifiedName());
        System.out.println("Field value: " + field.getValue());
        System.out.println("Field type: " + field.getClass().getSimpleName());
    }
    
    // Get specific field
    PDField nameField = acroForm.getField("Name");
    if (nameField != null) {
        System.out.println("Name field value: " + nameField.getValue());
    }
}

document.close();

Filling Form Fields

PDDocument document = Loader.loadPDF(new File("form.pdf"));
PDAcroForm acroForm = document.getAcroForm();

if (acroForm != null) {
    // Fill text field
    PDTextField nameField = (PDTextField) acroForm.getField("Name");
    nameField.setValue("John Doe");
    
    // Fill checkbox
    PDCheckBox agreeField = (PDCheckBox) acroForm.getField("Agree");
    agreeField.check();
    
    // Fill radio button
    PDRadioButton genderField = (PDRadioButton) acroForm.getField("Gender");
    genderField.setValue("Male");
    
    // Fill choice field (dropdown)
    PDChoice countryField = (PDChoice) acroForm.getField("Country");
    countryField.setValue(Arrays.asList("USA"));
    
    // Refresh form appearance
    acroForm.refreshAppearances();
}

document.save("filled-form.pdf");
document.close();

Creating Form Fields

PDDocument document = new PDDocument();
PDPage page = new PDPage(PDRectangle.A4);
document.addPage(page);

PDAcroForm acroForm = new PDAcroForm(document);
document.setAcroForm(acroForm);

// Create text field
PDTextField textField = new PDTextField(acroForm);
textField.setPartialName("Name");
textField.setDefaultValue("Enter your name");

// Create text field widget
PDAnnotationWidget textWidget = new PDAnnotationWidget();
textWidget.setRectangle(new PDRectangle(100, 700, 200, 20));
textWidget.setPage(page);
page.getAnnotations().add(textWidget);
textField.getWidgets().add(textWidget);

// Create checkbox
PDCheckBox checkBox = new PDCheckBox(acroForm);
checkBox.setPartialName("Subscribe");

PDAnnotationWidget checkWidget = new PDAnnotationWidget();
checkWidget.setRectangle(new PDRectangle(100, 650, 20, 20));
checkWidget.setPage(page);
page.getAnnotations().add(checkWidget);
checkBox.getWidgets().add(checkWidget);

// Add fields to form
acroForm.setFields(Arrays.asList(textField, checkBox));

document.save("new-form.pdf");
document.close();

Working with Choice Fields

PDDocument document = Loader.loadPDF(new File("form.pdf"));
PDAcroForm acroForm = document.getAcroForm();

// Get combo box field
PDComboBox comboBox = (PDComboBox) acroForm.getField("Country");
if (comboBox != null) {
    // Set options
    List<String> options = Arrays.asList("USA", "Canada", "Mexico", "UK", "Germany");
    comboBox.setOptions(options);
    
    // Set default selection
    comboBox.setValue(Arrays.asList("USA"));
    
    // Configure properties
    comboBox.setEdit(true);  // Allow custom text entry
    comboBox.setSort(true);  // Sort options alphabetically
}

// Get list box field
PDListBox listBox = (PDListBox) acroForm.getField("Languages");
if (listBox != null) {
    // Set multiple options with export values
    List<String> displayValues = Arrays.asList("English", "Spanish", "French", "German");
    List<String> exportValues = Arrays.asList("en", "es", "fr", "de");
    
    listBox.setOptionsDisplayValues(displayValues);
    listBox.setOptionsExportValues(exportValues);
    listBox.setMultiSelect(true);
    
    // Select multiple values
    listBox.setValue(Arrays.asList("en", "es"));
}

acroForm.refreshAppearances();
document.save("updated-form.pdf");
document.close();

Form Flattening

PDDocument document = Loader.loadPDF(new File("filled-form.pdf"));
PDAcroForm acroForm = document.getAcroForm();

if (acroForm != null) {
    // Flatten the form (convert fields to static content)
    acroForm.flatten();
    
    // Form is now non-interactive but retains visual appearance
}

document.save("flattened-form.pdf");
document.close();

Install with Tessl CLI

npx tessl i tessl/maven-org-apache-pdfbox--pdfbox

docs

content-stream-processing.md

cos-operations.md

document-operations.md

index.md

interactive-forms.md

multi-pdf-operations.md

rendering-graphics.md

security-encryption.md

text-operations.md

tile.json