Extract Data from PDF Form Fields in Flutter

PSPDFKit for Flutter allows you to extract data from form fields programmatically. Each form field has a fully qualified name, which is used to identify and retrieve a specific form field object before extracting its data.

Obtaining the Fully Qualified Name of a Form Field

The example below shows how to obtain the fully qualified name of the first form field on the first page of a document:

dynamic allAnnotations = await pspdfkitWidgetController.getAnnotations(0, 'all');
// Get the fully qualified name for the first form field.
String? formFieldName = (allAnnotations[0]['formFieldName']);

Getting the Data from a Form Field

The example below shows how to get the data from a form field with the fully qualified name, formFieldName:

// Get the data from a form field with the fully qualified name, `formFieldName`.
String? lastName = await pspdfkitWidgetController.getFormFieldValue('formFieldName');

Getting Form Field Properties

You can also get the properties of a form field using the getFormFields method from the PdfDocument class. This method returns a list of FormField objects, and each field contains the most common properties for the form field. This can be useful for validating form fields and extracting additional data from them:

List<FormField> formFields = await pdfdocument.getFormFields();

for (FormField formField in formFields) {
  print('Field name: ${formField.name}');
  print('Field readOnly: ${formField.isReadOnly}');
  print('Field required: ${formField.isRequired}');
  ...
}