Extract Data from AcroForm using Java
Contents
[
Hide
]
Extract all form fields
Use com.aspose.pdf.facades.Form to read field names and values without working through the full document object model.
- Open the source PDF form with the Form facade so AcroForm fields can be read without traversing the full document object model.
- Call
getFieldNames()to collect all field identifiers present in the form. - Iterate through those field names and call
getField(fieldName)to read each field value. - Build the output string from the extracted key-value pairs and print the aggregated form data.
- Close the Form facade in the
finallyblock.
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
- Open the source PDF form with the Form facade.
- Call
getField(fieldName)with the requested field name to read its current value from the AcroForm data. - Print the extracted field value.
- Close the Form facade in the
finallyblock.
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
- Open the source PDF form with the Form facade.
- Call
getFieldNames()to collect all available field identifiers from the AcroForm. - Iterate through those fields, escape the names and values, and build a JSON object string.
- Write the JSON result to the output file.
- Close the Form facade in the
finallyblock.
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
- Create the Form facade without binding a document yet.
- Open an output stream for the XML file and bind the source PDF to the facade with
bindPdf(...). - Call
exportXml(stream)so the current form field data is serialized as XML. - 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();
}
}
- Create the Form facade without binding a document yet.
- Open an output stream for the FDF file and bind the source PDF to the facade with
bindPdf(...). - Call
exportFdf(stream)so the form field data is serialized in FDF format. - 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();
}
}
- Create the Form facade without binding a document yet.
- Open an output stream for the XFDF file and bind the source PDF to the facade with
bindPdf(...). - Call
exportXfdf(stream)so the form field data is serialized in XFDF format. - 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();
}
}