Programmatic Form Filling

PSPDFKit for Android fully supports the AcroForm standard and can view and fill forms inside the PdfFragment/PdfActivity.

Forms are an optional component that can be licensed. If not licensed, forms will simply be invisible. Forms can also be manually switched off via the disableFormEditing() call when building PdfConfiguration or PdfActivityConfiguration.

You can also modify and query forms in code — for example, you can fill out forms programmatically:

Copy
1
2
3
4
5
6
7
8
9
    val document: PdfDocument = ...

    // This code shouldn't run on the main thread. Alternatively, use `getFormFieldsAsync()` to do this
    // processing in the RxJava chain.
    val formFields = document.formProvider.formFields
    formFields.filter { field -> field.type == FormType.TEXT }
                .forEach { field -> (field.formElement as TextFormElement).setText("Test ${field.name}") }
    formFields.filter { field -> field.type == FormType.CHECKBOX }
                .forEach { field -> (field.formElement as CheckBoxFormElement).toggleSelection() }
Copy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
    PdfDocument document = ...

    // This code shouldn't run on main thread. Alternatively, use `getFormFieldsAsync()` to do this
    // processing in the RxJava chain.
    List<FormField> formFields = document.getFormProvider().getFormFields();
    for (FormField formField : formFields) {
        if (formField.getType() == FormType.TEXT) {
            TextFormElement textFormElement = (TextFormElement) formField.getFormElement();
            textFormElement.setText("Test " + textFormElement.getName());
        } else if (formField.getType() == FormType.CHECKBOX) {
            CheckBoxFormElement checkBoxFormElement = (CheckBoxFormElement)formField.getFormElement();
            checkBoxFormElement.toggleSelection();
        }
    }