Import and Export Form Data

Aspose.PDF for Java supports several common data-exchange formats for interactive forms.

Import form data from XML

Use this example when form values are stored in an XML file and should be applied to a PDF form.

  1. Create a Form facade and bind the source PDF.
  2. Open the XML input stream and import the data into the form.
  3. Save the updated PDF document.
public static void importDataFromXml(Path inputFile, Path dataFile, Path outputFile) throws Exception {
    Form form = new Form();
    try (InputStream stream = Files.newInputStream(dataFile)) {
        form.bindPdf(inputFile.toString());
        form.importXml(stream);
        form.save(outputFile.toString());
    } finally {
        form.close();
    }
}

Export form data to XML

Use this example when you need to store current AcroForm values in XML format.

  1. Create a Form facade and bind the source PDF.
  2. Open the output stream for the XML file.
  3. Export the form data to XML.
public static void exportDataToXml(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();
    }
}

Import form data from FDF

Use this example when form values arrive in the FDF interchange format.

  1. Create a Form facade and bind the source PDF.
  2. Open the FDF input stream and import the data.
  3. Save the filled PDF document.
public static void importDataFromFdf(Path inputFile, Path dataFile, Path outputFile) throws Exception {
    Form form = new Form();
    try (InputStream stream = Files.newInputStream(dataFile)) {
        form.bindPdf(inputFile.toString());
        form.importFdf(stream);
        form.save(outputFile.toString());
    } finally {
        form.close();
    }
}

Export form data to FDF

Use this example when PDF form values should be shared as an FDF file.

  1. Create a Form facade and bind the source PDF.
  2. Open the output stream for the FDF file.
  3. Export the form data in FDF format.
public static void exportDataToFdf(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();
    }
}

Import form data from XFDF

Use this example when form data is provided in XFDF format and must be merged into a PDF.

  1. Create a Form facade and bind the source PDF.
  2. Open the XFDF input stream and import the values.
  3. Save the updated PDF document.
public static void importDataFromXfdf(Path inputFile, Path dataFile, Path outputFile) throws Exception {
    Form form = new Form();
    try (InputStream stream = Files.newInputStream(dataFile)) {
        form.bindPdf(inputFile.toString());
        form.importXfdf(stream);
        form.save(outputFile.toString());
    } finally {
        form.close();
    }
}

Export form data to XFDF

Use this example when you need an XML-based interchange file for AcroForm values.

  1. Create a Form facade and bind the source PDF.
  2. Open the output stream for the XFDF file.
  3. Export the current form values to XFDF.
public static void exportDataToXfdf(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();
    }
}

Extract form fields to JSON

Use this example when form values should be exported to a lightweight JSON representation.

  1. Open the PDF with the Form facade.
  2. Iterate through field names and serialize their values into JSON text.
  3. Write the JSON content to the target file.
public static void extractFormFieldsToJson(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();
    }
}

Reuse the JSON extraction helper

Use this example when you want a dedicated wrapper method that delegates to the main JSON export routine.

  1. Call the existing JSON extraction helper with the source PDF and output path.
  2. Reuse the same extraction logic without duplicating serialization code.
public static void extractFormFieldsToJsonDoc(Path inputFile, Path outputFile) throws Exception {
    extractFormFieldsToJson(inputFile, outputFile);
}