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.
- Create a Form facade and bind the source PDF.
- Open the XML input stream and import the data into the form.
- 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.
- Create a Form facade and bind the source PDF.
- Open the output stream for the XML file.
- 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.
- Create a Form facade and bind the source PDF.
- Open the FDF input stream and import the data.
- 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.
- Create a Form facade and bind the source PDF.
- Open the output stream for the FDF file.
- 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.
- Create a Form facade and bind the source PDF.
- Open the XFDF input stream and import the values.
- 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.
- Create a Form facade and bind the source PDF.
- Open the output stream for the XFDF file.
- 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.
- Open the PDF with the Form facade.
- Iterate through field names and serialize their values into JSON text.
- 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.
- Call the existing JSON extraction helper with the source PDF and output path.
- Reuse the same extraction logic without duplicating serialization code.
public static void extractFormFieldsToJsonDoc(Path inputFile, Path outputFile) throws Exception {
extractFormFieldsToJson(inputFile, outputFile);
}