Creating Fillable PDF Forms on iOS
PSPDFKit supports programmatically creating and removing form fields from a document. This can be especially useful for adding digital signatures. See the Add a Signature Form Field guide to find out how to solve that particular use case.
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 guide for programmatically creating annotations, although you don’t need to explicitly add the annotation, as it’ll be added automatically when adding the form field. For more information on the difference between a form field and a form element, please see our What Are Forms? guide.
Check out the API Reference for more info about the Forms API.
ButtonFormField with the type
.checkBox supports more than one form element. It also has an
onState property, which represents the value when a button is checked. If it’s unchecked, the value is always set to
// 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)
Here’s how to add a button to submit the current values of a form in XFDF format to an HTTP endpoint. Other available formats are FDF, HTML, or the whole PDF:
// Create the button form element (annotation). let pushButtonElement = ButtonFormElement() pushButtonElement.pageIndex = 0 pushButtonElement.boundingBox = CGRect(x: 100, y: 200, width: 150, height: 50) // Just use a red fill so the button is visible. You may want to set up a custom appearance using the element’s `appearanceStreamGenerator`. pushButtonElement.fillColor = .red // Create the action and set the submission format to XFDF. pushButtonElement.action = SubmitFormAction(url: URL(string: "https://example.com/wherever-you-want-to-send-the-form"), flags: [.XFDF]) // Ask PSPDFKit to create the form field and add it to the document. This will also add the form element to the document. // The `fullyQualifiedName` must not be the same as an existing form field, but otherwise can be whatever you like. // For a push button, there must be only one object in `formElements`. // For a push button, the `buttonValues` can be empty since push buttons run an action rather than alter a value. let pushButtonField = try ButtonFormField.insertedButtonField(with: .pushButton, fullyQualifiedName: "SubmitFormPushButton", documentProvider: documentProvider, formElements: [pushButtonElement], buttonValues: )