Flattening form fields
Introduction
Form fields are dynamic elements similar to HTML form fields. They provide an easy way to update PDF contents without the need to regenerate the whole document. For this reason many documents use fields to display dynamic data.
The process of flattening form fields consists in rendering their appearance directly onto the page, thus transforming them as static content of a document.
We will see in this tutorial how jPDF can be used to achieve this transformation.
Recover the form field to flatten
In this example we will flatten all the form fields present in a document. You can, of course, choose to flatten only certain fields, but let's keep it simple for now.
// Open the template document:
PdfDocument doc = new PdfDocument("Fields.pdf");
// Load the template module:
TemplateModule template = doc.getTemplateModule();
// Recover the list of form fields in the document:
List list = template.getFieldList();
It is important to note that in most situations jPDF knows how to render the field appearance even if the appearance was not previously rendered and stored in the document. This feature by itself makes jPDF the leading product in its category.
Update the form fields
We now have a list containing all the form fields present in a document. Flattening them 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();
// Instruct the template module to flatten the current form field:
template.flattenField(fieldName);
}
// Save the modified document to a new location:
doc.saveAs("generatedDocument.pdf");
doc.close();
Copyright © Crionics Inc. 2012 All rights reserved. Terms of Use | Privacy Policy
