Access PDF form data
PDF Form data
The JPedal PDF software internally converts all PDF forms into a PDF FormObject which represents the Form Data. You can access this directly using the PdfFormUtilities class.
Example code showing the API
The PdfFormUtilities class can be used to access the Form objects, GUI Objects and a list of FormNames from a PDF file if they are present. The code example below details shows several different examples for doing this.
final PdfFormUtilities formUtils = new PdfFormUtilities("inputFile.pdf");
try {
if (formUtils.openPDFFile()) {
Object[] returnValues;
//all forms in document (either of the following)
returnValues = formUtils.getFormComponentsFromDocument(null, ReturnValues.FORMOBJECTS_FROM_REF);
returnValues = formUtils.getFormComponentsFromDocument(null, ReturnValues.FORMOBJECTS_FROM_NAME);
//all forms on page 5 (either of the following)
returnValues = formUtils.getFormComponentsFromPage(null, ReturnValues.FORMOBJECTS_FROM_REF, 5);
returnValues = formUtils.getFormComponentsFromPage(null, ReturnValues.FORMOBJECTS_FROM_NAME, 5);
//all formNames
returnValues = formUtils.getFormComponentsFromDocument(null, ReturnValues.FORM_NAMES);
//all FormNames of Forms on page 12
returnValues = formUtils.getFormComponentsFromPage(null, ReturnValues.FORM_NAMES, 12);
//all forms in document called Mabel
returnValues = formUtils.getFormComponentsFromDocument("Mabel", ReturnValues.FORMOBJECTS_FROM_NAME);
//all forms on page 5 called Mabel
returnValues = formUtils.getFormComponentsFromPage("Mabel", ReturnValues.FORMOBJECTS_FROM_NAME, 5);
//form with PDF Reference
returnValues = formUtils.getFormComponentsFromDocument("25 0 R", ReturnValues.FORMOBJECTS_FROM_REF);
//form with PDF Reference on page 5
returnValues = formUtils.getFormComponentsFromPage("25 0 R", ReturnValues.FORMOBJECTS_FROM_REF, 5);
//if you need direct access to the GUI components, you can use
//(this will also trigger resolving these objects if Swing so we recommend you work
//with FormObjects unless you explicitly need this)
returnValues = formUtils.getFormComponentsFromDocument(null, ReturnValues.GUI_FORMS_FROM_NAME);
returnValues = formUtils.getFormComponentsFromPage(null, ReturnValues.GUI_FORMS_FROM_NAME, 5);
}
} catch (PdfException e) {
e.printStackTrace();
} finally {
formUtils.closePDFfile();
}
XFA XML data
PdfFormUtilities also allows you to extract the raw XFA data from a PDF.
final PdfFormUtilities formUtils = new PdfFormUtilities("inputFile.pdf");
try {
if (formUtils.openPDFFile()) {
final byte[] xfaConfig = formUtils.getRawXFAData(PdfDictionary.XFA_CONFIG);
final byte[] xfaDataSet = formUtils.getRawXFAData(PdfDictionary.XFA_DATASET);
final byte[] xfaTemplate = formUtils.getRawXFAData(PdfDictionary.XFA_TEMPLATE);
}
} catch (PdfException e) {
e.printStackTrace();
} finally {
formUtils.closePDFfile();
}