Fill PDF Form Fields Programmatically on iOS

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 PSPDFKit Catalog example in the examples directory of the framework download contains a Programmatic Form Filling example that demonstrates how objects can be queried and modified:

// 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
    }
}
// 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];
    }
}