Processing PDF Form Fields with Java
Benefits
- Extract, insert, and update data in PDF Forms.
Sample Project File
/*
This program demonstrates easyPDF ability to process pdf form fields.
This program is designed to work best with the supplied pdf file.
This pdf file (Form.pdf) can be found in the same directory as this source code.
Usage: java TestDocument [pdf file]
*/
import com.bcl.easypdf.*;
import com.bcl.easypdf.EasyPDFDocument.*;
import java.io.File;
public class TestDocument
{
public static void main(String[] args) throws Exception
{
if (args.length == 1)
{
IFormField myFormField;
ITextField myTextField;
IComboField myComboField;
File inputFile = new File(args[0]);
String inputFileName = inputFile.getCanonicalPath();
System.out.println("");
System.out.println("------------------------------------);
System.out.println("Document name: " + inputFileName);
System.out.println("------------------------------------);
System.out.println("");
EasyPDF.initialize();
IPDFDocument doc = new IPDFDocument();
doc.Open(args[0]);
//get all form fields from the PDF
IFormFields formFields = doc.getFormFields();
int nItems = formFields.getCount();
for (int i = 0; i < nItems; ++i)
{
//get the (i) field
myFormField = formFields.getItem(new Integer(i));
if (myFormField.getType() == docFormFieldType.DOC_FRMTYPE_TEXT) {
myTextField = (ITextField) myFormField;
//display the value of the text fields
System.out.println("Field name = " + myTextField.getName());
System.out.println("getPassword = " + myTextField.getPassword());
System.out.println("getMaxLen = " + myTextField.getMaxLen());
System.out.println("getMultiline = " + myTextField.getMultiline());
System.out.println("getValue = " + myTextField.getValue());
System.out.println("");
}else if (myFormField.getType() == docFormFieldType.DOC_FRMTYPE_COMBO ) {
//display the value of the combo fields
myComboField = (IComboField) myFormField;
System.out.println("Field name = " + myComboField.getName());
System.out.println("Custom value = " + myComboField.getCustomValue());
System.out.println("Custom value enabled = " + myComboField.getCustomValueEnabled());
System.out.println("Set custom value enabled");
myComboField.setCustomValue("-----");
System.out.println("Custom value = " + myComboField.getCustomValue());
System.out.println("");
} else {
System.out.println("Field name = " + myFormField.getName());
}
}
System.out.println("\n\n");
//get the values in Radio fields
IRadioField myRadioField;
System.out.println("---------Gender field info---------------------\n");
myFormField = formFields.getItem(new String("Gender"));
System.out.println("Field name = " + myFormField.getName());
myRadioField = (IRadioField) myFormField;
System.out.println("Field name = " + myRadioField.getName());
System.out.println("getNoToggleToOff = " + myRadioField.getNoToggleToOff());
System.out.println("getRadiosInUnison = " + myRadioField.getRadiosInUnison());
System.out.println("\n\n");
System.out.println("---------Music field info----------------------\n");
myFormField = formFields.getItem(new String("Music"));
System.out.println("Field name = " + myFormField.getName());
myRadioField = (IRadioField) myFormField;
System.out.println("Field name = " + myRadioField.getName());
System.out.println("getNoToggleToOff = " + myRadioField.getNoToggleToOff());
System.out.println("getRadiosInUnison = " + myRadioField.getRadiosInUnison());
EasyPDF.uninitialize();
} else {
System.out.println("This example is designed to work with the supplied form.pdf file");
System.out.println("Usage: java TestDocument .\\form.pdf");
}
}
}