Crionics LOGO

TUTORIALS

 

 

Template Tutorial:
Filling form fields - dynamically discovering form fields

As easy as 1, 2, 3...

 

Generate the template PDF

Usually, you only need to take this step one time. Once the template is created, it can be reused as many times as necessary.

  • Using your favorite PDF generation tool, create your PDF template.
  • Add PDF form fields for the areas in the document that will be generated dynamically.

 

Recover the form fields in a document

In the other template tutorial, we saw how to fill form fields when their names are already known.
But let's assume your business has tons of templates and you want to discover their names and types dynamically at runtime.

The code would then look like this:

// Open the template document:
PdfDocument doc = new PdfDocument("Fields.pdf");

// Recover the list of form fields in the document:
List list = doc.getTemplateModule().getFieldList();

 

Update the form fields

As we now have a list containing all the form fields present in a document, updating it becomes quite simple:

for (Iterator i = l.iterator(); i.hasNext();)
{

// Recover the next form field to process:
AbstractField field = (AbstractField) i.next();

// Recover its name (common feature to all the form fields):
String fieldname= field.getName();

// At this stage you will have to check the type of form field:
if (if (field instanceof CheckBoxField)
{
System.out.println("Updating the value of the Check Box Field name " + fieldname);
CheckBoxField cb = (CheckBoxField) field;
cb.setValue("true");
}
else if (field instanceof TextField)
{
System.out.println("Updating the value of the Text Field name " + fieldname);
TextField tf = (TextField) field;
tf.setValue("My New Value");
}
else if (field instanceof RadioButtonField)
{
System.out.println("Updating the value of the Radio Button Field name " + fieldname);
RadioButtonField rb = (RadioButtonField) field;
rb.setValue("true");
}

// jPDF defines many other types of fields. just look in the javadoc for
// children of AbstractField:
}

// Save the modified document to a new location:
doc.saveAs("generatedDocument.pdf");
doc.close();


© 2000-2007 Crionics Inc. Report issues with this page