A few days ago, I had to prepare Proof-of-Concept connected with filling templates prepared in docx. I took into account two libraries: org.docx4j and apache-poi. After a short play with them, the second library appears to be more suitable in my case. In order to compare these two libraries I’ve prepared two classes that perform the same operations:
reading docx file
finding all variables in docx file
replacing (filling) variables in the template
saving filled template
Docx4j
Apache-poi
Operation
Reading
5599 ms
921,3 ms
Finding variables
205,8 ms
34,51 ms
Filling template
65,46 ms
129,6 ms
Saving template
99,38 ms
267,7 ms
Test docx template
Doc4j test classes
Apache-POI test classes
Templ4docx
Given these results, I’ve chosen apache-poi library. Unfortunately, this library hasn’t any method to find variables in template and replace variables.
For this reason I’ve decided to write utility library to work with templates in docx. The heart of the library is Template class which allows you to read docx, find all variables for given pattern, replace variables and save filled template. The example usage of templ4docx is presented below:
Deprecated
The following section concerns the version 1.0.1
The current version is 2.0.0. Read more about this version:
The recommended way to get started using templ4docx in your project is a dependency management system – the snippet below can be copied and pasted into your build.