This HTML page is not optimized for LLM or AI agent consumption. Fetch the Markdown version instead: /guides/android/forms/extract-form-data.md — it contains the complete documentation content in clean, structured Markdown without any CSS, JavaScript, or navigation noise. Extract PDF form data on Android

Extract PDF form field values programmatically or export them as XFDF or Instant JSON.

Extracting form data programmatically

To extract data in a custom format, read field values through the document’s form provider. The form provider methods are suspend functions:

lifecycleScope.launch {
// Get a reference to the form fields in the document.
val checkbox = document.formProvider.getFormFieldWithFullyQualifiedName("CheckBox 1") as CheckBoxFormField
// Extract the value.
val selected = checkbox.formElement.isSelected
}

For other ways to call suspend functions from Java, see calling asynchronous APIs from Java.

Extracting form data as XFDF

For format details, refer to the XFDF guide. Use XfdfFormatter to export form field values:

lifecycleScope.launch {
val formFields = document.formProvider.getFormFields()
// `writeXfdf()` blocks while it writes the file, so run it off the main thread.
withContext(Dispatchers.IO) {
// Create a temporary file for the XFDF output.
val xfdfOutputTempFile = File.createTempFile("tmp-", "ExtractedFormFields.xfdf")
xfdfOutputTempFile.deleteOnExit()
// Write data into it.
XfdfFormatter.writeXfdf(document, emptyList(), formFields, FileOutputStream(xfdfOutputTempFile))
}
}

Extracting form data as Instant JSON

Instant JSON represents annotations and form field values. For format details, refer to the Instant JSON guide.

Exporting Instant JSON produces a diff between the in-memory document and the document on disk, so don’t save the document before exporting the changes. Disable autosave with PdfActivityConfiguration.Builder:

configuration.autosaveEnabled(false).build()

Export Instant JSON to an OutputStream using DocumentJsonFormatter.exportDocumentJson(). In Java, use exportDocumentJsonBlocking():

lifecycleScope.launch(Dispatchers.IO) {
val outputStream = ByteArrayOutputStream()
DocumentJsonFormatter.exportDocumentJson(document, outputStream)
}

The export includes form element appearance properties, such as text color and border style. It also includes other annotations, such as highlights and drawings.

Don’t use Annotation’s toInstantJson() function to export form field values. This function exports form element properties, such as text color and border style, but excludes the associated field value.