Form Creation

In PSPDFKit 7.0, we added programmatic support for creating and removing form fields from a document. This can be useful, for instance, when a PDF document needs to be digitally signed but doesn’t contain a digital signature field. Below we’ll outline the steps for adding and removing form fields.

Add a Signature Form Field

A form field is a model representation of a visual form in a document. To be able to create a form field, you have to first create a form element (also known as a widget annotation). This works the same as adding any other annotation, as can be seen in the Programmatically Creating Annotations guide. For more information on the difference between a form field and a form element, please see our Introduction to Forms guide.

When the form element has been added to the document on the correct page, you can call insertedSignatureField(withFullyQualifiedName:documentProvider:formElement:) on SignatureFormField. Note that this can fail, and you will need to check that the return value is not nil:

Copy
1
2
3
4
5
6
7
8
9
// Create a new signature form element.
let signatureFormElement = SignatureFormElement()
// Position it in the document.
signatureFormElement.boundingBox = CGRect(x: 100, y: 100, width: 100, height: 20)
// Add it to the third page.
signatureFormElement.pageIndex = 2

// Insert a form field for the form element.
let signatureFormField = try! SignatureFormField.insertedSignatureField(withFullyQualifiedName: "Digital Signature", documentProvider: documentProvider, formElement: signatureFormElement)
Copy
1
2
3
4
5
6
7
8
9
10
11
12
13
// Create a new signature form element.
PSPDFSignatureFormElement *signatureFormElement = [[PSPDFSignatureFormElement alloc] init];
// Position it in the document.
signatureFormElement.boundingBox = CGRectMake(100.f, 100.f, 100.f, 20.f);
// Add it to the third page.
signatureFormElement.pageIndex = 2;

// Insert a form field for the form element.
NSError *error;
PSPDFSignatureFormField *signatureFormField = [PSPDFSignatureFormField insertedSignatureFieldWithFullyQualifiedName:@"Digital Signature" documentProvider:documentProvider formElement:signatureFormElement error:&error];
if (!signatureFormField) {
    // Handle error.
}

You can add any kind of form field to a document, apart from signature form fields. Check out the iOS documentation for more info about the Forms API.

Add Radio Buttons and Checkboxes

A ButtonFormField with the types PDFFormField.Kind.radioButton and PDFFormField.Kind.checkBox supports more than one form element. It also has an onState property, which represents the value when a button is checked. If it is unchecked, the value is always set to Off:

Copy
1
2
3
4
5
6
7
8
9
10
11
12
// Create two radio buttons and position them in the document.
let radio1 = ButtonFormElement()
radio1.boundingBox = CGRect(x: 100, y: 100, width: 20, height: 20)
radio1.pageIndex = 0
let radio2 = ButtonFormElement()
radio2.boundingBox = CGRect(x: 130, y: 100, width: 20, height: 20)
radio2.pageIndex = 0

// The `buttonValues` specify the radio buttons' `onState` value.
let buttonValues = ["RadioButton1", "RadioButton2"]

let radioButtonFormField = try! ButtonFormField.insertedButtonField(with: .radioButton, fullyQualifiedName: "RadioButton", documentProvider: documentProvider, formElements: [radio1, radio2], buttonValues: buttonValues)
Copy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// Create two radio buttons and position them in the document.
PSPDFButtonFormElement *radio1 = [[PSPDFButtonFormElement alloc] init];
radio1.boundingBox = CGRectMake(100.f, 100.f, 20.f, 20.f);
radio1.pageIndex = 0;
PSPDFButtonFormElement *radio2 = [[PSPDFButtonFormElement alloc] init];
radio2.boundingBox = CGRectMake(130.f, 100.f, 20.f, 20.f);
radio2.pageIndex = 0;

// The `buttonValues` specify the radio buttons' `onState` value.
NSArray<NSString *> *buttonValues = @[@"RadioButton1", @"RadioButton2"];

NSError *error;
PSPDFButtonFormField *radioButtonFormField = [PSPDFButtonFormField insertedButtonFieldWithType:PSPDFFormFieldTypeRadioButton fullyQualifiedName:@"RadioButton" documentProvider:document.documentProviders[0] formElements:@[radio1, radio2] buttonValues:buttonValues
error:&error];
if (!radioButtonFormField) {
    // handle error
}

Remove Form Fields and Form Elements

You can easily remove any form field or form element from a document using PDFFormParser. Simply call either remove(_:) or removeFormFields(_:).