Programmatic Form Filling

PSPDFKit for iOS fully supports the AcroForm standard and can view and fill forms inside the PSPDFViewController.

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 setting the formsEnabled property of the PSPDFDocument to false.

The PSPDFCatalog example you find in the examples directory of the framework download contains a "Programmatic Form Filling" 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.
guard let annotations = document.annotationsForPage(at: 0, type: .widget) else { return }
for annotation in annotations {
    switch annotation {
    case let textFieldFormElement as PSPDFTextFieldFormElement:
        guard let fieldName = textFieldFormElement.fieldName else { return }
        textFieldFormElement.contents = String(format: "Test %@", arguments: [fieldName])
    case let buttonFormElement as PSPDFButtonFormElement:
        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];
    }
}