Java Development - Dynamic creation and merging of documents

As a software developer, we firmly believe that “Men must think and machines should work”. So, when we encounter situations where a client spends considerable man hours on repeated tasks, we step-in and look for opportunities to automate.

Our recent customer dealt with orders from the State and Federal courts to extend health care coverage to the children of parents-employees who are divorced, separated or never-married. This customer processed over 125 orders per day and each order went through multiple steps before a child would be enrolled into the employees’ health plan.

Why dynamic document creation?

During this workflow process, an order can reach one or more of the twenty different possible statuses. As the Order progresses through different steps, a letter would be sent to the employee, employer and court.

Previously, our customer would manually create these letters by encapsulating information from the Order and using the letter template appropriate to the status. Obviously, this was highly time-consuming as each letter creation would take about 10 minutes. So, to process 125 orders there were productivity losses to the extent of twenty-one person hours per day.

So, we stepped in and made an application to automate this process

  1. We defined the letter templates for all possible status of the order
  2. In the letter template wherever the dynamic values like name, address had to be retrieved from the order, we put a place holder
  3. When the order got to a certain status, we used docx4j java library to create a document with appropriate letter template and replaced the place holders with the values from the order.

These letters, when generated, would be ready for printing.


docx4j, Mergedocx java library Java Coding using Mergedocx java library and docx4j


Sample Letter Template

Letter to be sent to the following employee.

Name: <<Employee_Name>>
Id: <<Employee_Id>>
Address: <<Employee_Address>>

Note: Fields with ‘<< >>’ will be replaced during letter creation




Code Snippet

// Load the template document
WordprocessingMLPackage templateDoc = Docx4J.load(new FileInputStream(new File(templateFile)));
// Replace the place holders with dynamic values
org.docx4j.model.fields.merge.MailMerger.performMerge(templateDoc, mergeFieldData.get(0), true);
where mergeFieldData.get(0) returns a Hashmap with place holder name as key and value as the dynamic data.
// save the generated file
SaveToZipFile saver = new SaveToZipFile(templateDoc);;

Letter to be sent to the following employee.

Name: Stephen

Id: 1111

Address: 145, Kumar Boulevard, Colorado, 80227

About docx4j

docx4j is an open source Java library used for creating or updating Microsoft Open XML (Word docx, Powerpoint pptx, and Excel xlsx) files. Using docx4j one can open existing or create new document (docx), presentation (pptx), spread sheet (xlsx) and template substitution, CustomXML binding, Save docx to file system as a docx, include common filters, Export as HTML or PDF

Merging document

Another problem that was encountered related to printing of the generated letters sequentially. Since multiple people work on different orders, the printers were normally flooded with a huge pile-up of letters and it became a challenge to order, verify and send it to appropriate recipients.

How did we solve this problem?

When letter were generated they were sent to a ‘Print Queue’ within the application. From this area, an administrator chose the letters to be printed and the application merged all these into one document using Mergedocx java library. During the merging process, the letters were grouped by order and status.

This made life a lot easier for the administrator to print letters and send easily to recipients.

Code snippet:

for (int i = 0; i < files.size(); i++) {
//create block range
WordprocessingMLPackage wmlPkgIn = WordprocessingMLPackage.load(new File(files.get(i)));

// get each document content in the list
BlockRange block = new BlockRange(wmlPkgIn);

// make sure the page break is there

// make sure the numbering if any start freshly in each page

// 2nd param is whether this is your last docx
dbi.addBlockRange(block, i == (files.size() - 1));
// Get the merged output docx
WordprocessingMLPackage mergedDoc = dbi.finish();
// save the generated file
SaveToZipFile saver = new SaveToZipFile(mergedDoc);;

MergeDocx is a commercial extension to docx4j which is capable of appending or concatenating docx files together to create a single docx file. For example, one can merge or concatenate cover letter and a contract into a single docx file, without changing the look and feel of either document.

As a result of automation, our customer gained significant productivity gains by saving time, effort and money. In short, we empowered our customer with more flexibility on letter template management and controlled printing of generated letters.