Extract Data from PDF Form Fields Using JavaScript
You can extract the values of all the form fields present in a PDF using Instance#getFormFieldValues
. It returns a simple JavaScript object, where the keys refer to the FormField#name
of the form field and the value is either null
, string
, or Array<string>
, depending upon the type of the FormField
:
const formFieldValues = instance.getFormFieldValues(); console.log(formFieldValues); // { textField: 'Text Value', checkBoxField: ['A', 'B'], buttonField: null }
Extracting Form Data as Instant JSON
Instant JSON is optimized for annotations. However, generating Instant JSON from a document will include form field values as well.
Form fields that have been saved can be exported in the Instant JSON format using the Instance#exportInstantJSON
API method, and you can look for their values within formFieldValues
:
const instantJSON = await instance.exportInstantJSON(); const formFieldValues = instantJSON.formFieldValues; console.log(formFieldValues); // [{"name":"Form Field","type":"pspdfkit/form-field-value","v":1]
Note that this method will only yield the changes (diff) in form field values after saving the document.