Extract Data from AcroForm using Java

Extract all form fields

Use com.aspose.pdf.facades.Form to read field names and values without working through the full document object model.

  1. Open the source PDF form with the Form facade so AcroForm fields can be read without traversing the full document object model.
  2. Call getFieldNames() to collect all field identifiers present in the form.
  3. Iterate through those field names and call getField(fieldName) to read each field value.
  4. Build the output string from the extracted key-value pairs and print the aggregated form data.
  5. Close the Form facade in the finally block.
public static void extractFormFields(Path inputFile) {
    Form form = new Form(inputFile.toString());
    try {
        StringBuilder formValues = new StringBuilder("{");
        String[] fieldNames = form.getFieldNames();
        for (int i = 0; i < fieldNames.length; i++) {
            if (i > 0) {
                formValues.append(", ");
            }
            formValues.append(fieldNames[i]).append("=").append(form.getField(fieldNames[i]));
        }
        formValues.append("}");
        System.out.println(formValues);
    } finally {
        form.close();
    }
}

Retrieve a field value by name

  1. Open the source PDF form with the Form facade.
  2. Call getField(fieldName) with the requested field name to read its current value from the AcroForm data.
  3. Print the extracted field value.
  4. Close the Form facade in the finally block.
public static void extractFormFieldByTitle(Path inputFile, String fieldName) {
    Form form = new Form(inputFile.toString());
    try {
        String formValue = form.getField(fieldName);
        System.out.println(formValue);
    } finally {
        form.close();
    }
}

Export form fields to JSON

  1. Open the source PDF form with the Form facade.
  2. Call getFieldNames() to collect all available field identifiers from the AcroForm.
  3. Iterate through those fields, escape the names and values, and build a JSON object string.
  4. Write the JSON result to the output file.
  5. Close the Form facade in the finally block.
public static void extractFormFieldsJson(Path inputFile, Path outputFile) throws Exception {
    Form form = new Form(inputFile.toString());
    try {
        StringBuilder json = new StringBuilder();
        json.append("{\n");
        String[] fieldNames = form.getFieldNames();
        for (int i = 0; i < fieldNames.length; i++) {
            String fieldName = fieldNames[i];
            json.append("    \"").append(escapeJson(fieldName)).append("\": \"")
                    .append(escapeJson(form.getField(fieldName))).append("\"");
            if (i < fieldNames.length - 1) {
                json.append(",");
            }
            json.append("\n");
        }
        json.append("}\n");
        Files.writeString(outputFile, json.toString());
    } finally {
        form.close();
    }
}

Export form data to XML, FDF, and XFDF

  1. Create the Form facade without binding a document yet.
  2. Open an output stream for the XML file and bind the source PDF to the facade with bindPdf(...).
  3. Call exportXml(stream) so the current form field data is serialized as XML.
  4. Close the Form facade after the export completes.
public static void extractDataToXml(Path inputFile, Path outputFile) throws Exception {
    Form form = new Form();
    try (OutputStream stream = Files.newOutputStream(outputFile)) {
        form.bindPdf(inputFile.toString());
        form.exportXml(stream);
    } finally {
        form.close();
    }
}
  1. Create the Form facade without binding a document yet.
  2. Open an output stream for the FDF file and bind the source PDF to the facade with bindPdf(...).
  3. Call exportFdf(stream) so the form field data is serialized in FDF format.
  4. Close the Form facade after the export completes.
public static void extractDataToFdf(Path inputFile, Path outputFile) throws Exception {
    Form form = new Form();
    try (OutputStream stream = Files.newOutputStream(outputFile)) {
        form.bindPdf(inputFile.toString());
        form.exportFdf(stream);
    } finally {
        form.close();
    }
}
  1. Create the Form facade without binding a document yet.
  2. Open an output stream for the XFDF file and bind the source PDF to the facade with bindPdf(...).
  3. Call exportXfdf(stream) so the form field data is serialized in XFDF format.
  4. Close the Form facade after the export completes.
public static void extractDataToXfdf(Path inputFile, Path outputFile) throws Exception {
    Form form = new Form();
    try (OutputStream stream = Files.newOutputStream(outputFile)) {
        form.bindPdf(inputFile.toString());
        form.exportXfdf(stream);
    } finally {
        form.close();
    }
}