Extract PDF Form Data in Java

This guide explains how to get values of form fields in PSPDFKit for Java. See the Edit Form Fields guide for information on how to set values.

Form field data is extracted to a JSON object containing all the form data for a document. To extract the form field values, use the FormProvider.getFormFieldValuesJson method:

final PdfDocument document = PdfDocument.open(new FileDataProvider(new File("formDocument.pdf")));
final FormProvider formProvider = document.getFormProvider();
JSONObject formFieldValues = formProvider.getFormFieldValuesJson();

// Check they are what you expected (these values were set in the Edit Form Fields guide.)
assert "John" == formFieldValues.get("Name_First");
assert "Smith" == formFieldValues.get("Name_Last");

Above, you can see the extraction of the form field’s values. The extracted values are held in the exact same key-value pair structure that the form field value setter requires, as mentioned in the Edit Form Fields guide. When you query the Name_First key, you retrieve the string John.