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}// `getFormFieldWithFullyQualifiedName()` is a suspend function. `runBlocking()` waits for it to finish,// so only call this on a worker thread.try { // Get a reference to the form fields in the document. final CheckBoxFormField checkbox = (CheckBoxFormField) BuildersKt.runBlocking( EmptyCoroutineContext.INSTANCE, (scope, continuation) -> document.getFormProvider().getFormFieldWithFullyQualifiedName("CheckBox 1", continuation)); // Extract the value. final boolean selected = checkbox.getFormElement().isSelected();} catch (InterruptedException e) { Thread.currentThread().interrupt();}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)) }}// `getFormFields()` is a suspend function. `runBlocking()` waits for it to finish,// and `writeXfdf()` blocks while it writes the file, so only call this on a worker thread.try { final List<? extends FormField> formFields = BuildersKt.runBlocking( EmptyCoroutineContext.INSTANCE, (scope, continuation) -> document.getFormProvider().getFormFields(continuation));
// Create a temporary file for the XFDF output. final File xfdfOutputTempFile = File.createTempFile("tmp-", "ExtractedFormFields.xfdf"); xfdfOutputTempFile.deleteOnExit();
// Write data into it. XfdfFormatter.writeXfdf(document, Collections.emptyList(), formFields, new FileOutputStream(xfdfOutputTempFile));} catch (InterruptedException e) { Thread.currentThread().interrupt();} catch (IOException e) { // Handle the error.}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()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)}// Blocks until the export finishes. Call it on a background thread.final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();DocumentJsonFormatter.exportDocumentJsonBlocking(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.