Programmatic Form Filling

PSPDFKit for iOS fully supports the AcroForm standard, and forms can be viewed and filled inside the PDFViewController.

Forms are an optional component that can be licensed. If not licensed, forms will simply be invisible. Forms can also be manually switched off by setting the formsEnabled property of a Document to false.

The PSPDFCatalog example in the examples directory of the framework download contains a Programmatic Form Filling example that demonstrates how objects can be queried and modified:

Copy
1
2
3
4
5
6
7
8
9
10
11
12
13
// Model editing needs to happen on the main thread.
let annotations = document.annotationsForPage(at: 0, type: .widget)
for annotation in annotations {
    switch annotation {
    case let textFieldFormElement as TextFieldFormElement:
        guard let fieldName = textFieldFormElement.fieldName else { return }
        textFieldFormElement.contents = String(format: "Test %@", arguments: [fieldName])
    case let buttonFormElement as ButtonFormElement:
        buttonFormElement.toggleButtonSelectionState()
    default:
        break
    }
}
Copy
1
2
3
4
5
6
7
8
9
// Model editing needs to happen on the main thread.
NSArray *annotations = [document annotationsForPageAtIndex:0 type:PSPDFAnnotationTypeWidget];
for (PSPDFFormElement *formElement in annotations) {
    if ([formElement isKindOfClass:PSPDFTextFieldFormElement.class]) {
        formElement.contents = [NSString stringWithFormat:@"Test %@", formElement.fieldName];
    } else if ([formElement isKindOfClass:PSPDFButtonFormElement.class]) {
        [(PSPDFButtonFormElement *)formElement toggleButtonSelectionState];
    }
}