An Introduction to PDF Forms and Itext
Disclaimer: The content of this blog are my views, observations and understanding about the topic based on my personal experience.
Developed in the early 90’s as part of the Camelot project, a Portable Document Format (PDF), as we know it today, has grown into the most preferred way of presenting and exchanging documents reliably independent of any software, hardware, or operating system. PDF documents can also contain links and buttons, form fields, audio, video, and even business logic. These are signed electronically, and one can easily view PDF files on Windows, MAC OS, and others, including mobiles using any of the PDF viewers/ browsers. PDFs are a highly secured document and are of the format trusted by businesses around the world.
Now, consider a scenario wherein the layout and design of a complex PDF are modified with several changes in the requirement. It’s extremely difficult to update or maintain such a code wherein a single switch in design might break the other design aspects of the pdf and may end up taking a whole day of work with significantly higher efforts in testing for just a simple design change.
That is where the Interactive PDFs come into the picture. With interactive PDF, the design changes can be accommodated in a bare minimum amount of time, and no code is altered, thus reducing the time and efforts to test and maintain the application.
Interactive PDFs provide us the ability to have form elements like text fields, dropdowns, checkboxes, radio buttons, image fields, etc embedded into the PDF creating fillable PDFs.
The Interactive form breathes new life into old forms and paper documents by turning them into digital, fillable PDFs.
These can easily be created using many tools that are readily available such as “PDF Form Creator”, Adobe Acrobat Pro DC, Adobe Lifecycle Designer, Adobe Experience Manager (AEM), Expert PDF, among many others.
It’s easy to convert the scanned copy of an existing paper form to an interactive Acroform using Acrobat Pro DC. Just click on the Prepared Form button and voila, it’s done! The Software is smart enough to dynamically generate text fields and assign IDs by just scanning the document. This can further be edited to better suit the requirement.
These interactive forms can be broadly categorized as XFA Forms also known as LifeCycle Forms and Acro forms.
AcroForms are the original PDF forms technology, first introduced in 1998. AcroForms accept input in both Forms Data Format (FDF) and XML Forms Data Format (XFDF). These forms contain fields that cannot be dynamically modified as it is similar to a template that has fixed form data. AcroForms can be created using Adobe Acrobat Pro Version.
XFA Forms (XML Forms Architecture) represents a significant change in direction for Adobe from the popular FDF and XFDF methodologies. Adobe’s XML Forms Architecture (XFA) cannot be created using Adobe Acrobat as XFA forms are inherently XML rather than PDF, and are incompatible with conventional PDF files. These forms can be created using Adobe Lifecycle Designer and Acrobat cannot be used to modify a LiveCycle Designer-created document.
XFA Forms allow your PDF forms to grow, shrink, and restructure themselves at runtime, depending on the data entered and user interaction. This allows for a more dynamic user interaction than you find in AcroForms, XFA supports the concept of document reflow allowing a field to resize if needed to accommodate data.
Dynamic XFA Forms can change their layout at runtime, AcroForms on the other hand are static and the layout cannot be altered.
These forms can be filled manually or dynamically using any of the available libraries such as Itext, aspose, etc. supported by various languages.
Having used Itext for filling out these forms with a Java backend server I can comfortably say that this library makes it very easy to interact and fill out these forms dynamically with a very small piece of code.
// load the pdf template document
PdfDocument pdfDoc = new PdfDocument(new PdfReader(SRC), new PdfWriter(dest));
//extract the form fields
PdfAcroForm form = PdfAcroForm.getAcroForm(pdfDoc, true);
//Select the font file
PdfFont font = PdfFontFactory.createFont(FONT, PdfEncodings.IDENTITY_H);
//Set value in the field
form.getField("test_field").setValue("Sample text", font, 12f);
//Flatten the form
form.flattenFields();
//close the document
pdfDoc.close();
Itext is one of the most popular and developer-friendly PDF library which can be used to create/edit and fill out interactive forms. With Itext, the fields in these Interactive Documents can be easily extracted and manipulated to populate data/images and also update the font for these fields, if needed. Itext dynamically can also combine multiple of these forms and create a single PDF merging all together.
After filling the fields, the library can also Flatten (Remove all form-related data) the generated PDF, which makes sure that the filled-out PDF cannot be edited any further, turning them into static pdf documents. It is a way of finalizing your forms and securing its content.
Considering a scenario where dynamically generated PDFs (PDF Generated at runtime) is a need and fast-paced development is required, the Interactive forms template design with Itext can be considered one of the optimal solutions which can be easily integrated with a micro-service architecture.