import com.snowtide.PDF;
import com.snowtide.pdf.Document;
import com.snowtide.pdf.forms.*;
public class ExtractFormData {
public static void main (String[] args) throws java.io.IOException {
String pdfFilePath = args[0];
Document pdfts = PDF.open(pdfFilePath);
AcroForm form = (AcroForm)pdfts.getFormData();
// access specific fields directly
AcroTextField projectName = (AcroTextField)form.getField("color.1");
AcroCheckboxField isPrivateNonProfit =
(AcroCheckboxField)form.getField("color.10-privatenonprofit");
System.out.printf("Project %s %s run by a nonprofit organization",
projectName.getValue(),
isPrivateNonProfit.isChecked() ? "is" : "is not");
System.out.println();
// access all fields (just a sampling of available data/functionality)
System.out.println(String.format("All form data from %s:", pdfFilePath));
for (AcroFormField field : form) {
Object ftype = field.getType();
if (ftype == AcroFormField.FIELD_TYPE_TEXT) {
System.out.printf("Field %s is a text box; value: %s",
field.getFullName(), field.getValue());
} else if (ftype == AcroFormField.FIELD_TYPE_BUTTON) {
switch (((AcroButtonField)field).getButtonType()) {
case AcroButtonField.BUTTON_TYPE_PUSHBUTTON:
System.out.printf("Field %s is a pushbutton; value: %s",
field.getFullName(), field.getValue());
break;
case AcroButtonField.BUTTON_TYPE_CHECKBOX:
System.out.printf("Field %s is a checkbox; value: %s; is checked? %s",
field.getFullName(), field.getValue(),
((AcroCheckboxField)field).isChecked());
break;
case AcroButtonField.BUTTON_TYPE_RADIO_GROUP:
System.out.printf("Field %s is a radio button group; value: %s; possible values: %s",
field.getFullName(), field.getValue(),
((AcroRadioButtonGroupField)field).getPossibleValues());
break;
}
} else if (ftype == AcroFormField.FIELD_TYPE_CHOICE) {
System.out.printf("Field %s is 'select' dropdown; value: %s; display label: %s",
field.getFullName(), field.getValue(),
((AcroChoiceField)field).getDisplayValue((String)field.getValue()));
} else if (ftype == AcroFormField.FIELD_TYPE_SIGNATURE) {
System.out.printf("Field %s is a signature; value: %s",
field.getFullName(), field.getValue());
} else {
System.out.printf("Field %s is of unknown type; value: %s",
field.getFullName(), field.getValue());
}
System.out.println();
}
pdfts.close();
}
}
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4