Adding content to a template
To show how the library works with templates, let's modify a template and replace some variables for text, lists, tables and other contents.
First, create a new document using a DOCX as template with the class CreateDocxFromTemplate:
Now, let's replace the contents of this docx object. The order of replacement is not relevant.
The easiest element for replacing a placeholder is a string. replaceVariableByText is the method to be used:
You have to set one or more values in the HashMap to replace them. A second parameter is available with the following options:
- "target": Where the variables for replacement are: document, header, footer...
- "firstMatch": Replaces the first occurrence only if it's true
- "parseLineBreaks": Replaces all characters "\n" with line breaks
For example, replace the TEXT placeholder by a text with a line break:
Replacing lists is as simple as adding a list with a single element in the template which contains the placeholder. When adding the new values, lists elements remain intact.
To add values to the list, use the replaceListVariable method:
Where the available parameters are, in order:
- "variable": Name of the placeholder of the template
- "listValues": ArrayList with the values to add
- "options": "firstMatch" option replaces only the first occurrence if it's true, "parseLineBreaks" replaces the characters "\n" with line breaks, "target" set the placeholder target.
For example, switch the placeholder LIST in a web browser list:
Just like with the lists, you can add tables to the template by making a table with the styles that you want and define a row with the variables to replace. This row will be replaced for as many rows as needed with new values, keeping the original table styles.
The signatures of the replaceTableVariable method are:
Where the parameters are, in order:
- "variables": ArrayList with the table placeholders and its values. For each position of the ArrayList, a new row is created in the table.
- "options": With the "firstMatch" option replaces only the first occurrence if it's true, "parseLineBreaks" replaces the characters "\n" with line breaks, "target" set the placeholder target.
Sometimes there's no need to replace every placeholder, and some of them remain in the template. To erase them, call removeTemplateVariable:
The available options are:
- "variable": Name of the placeholder to remove
- "type": Delete type. It also removes or not the container paragraph
- "target": To define the target of the placeholder as document, header, footer...
For example, to delete the placeholder FOOTERVAR in the footer code this method:
And, for deleting the placeholder OTHERVAR in the document:
How to retrieve variables with the getTemplateVariables: at any time you can know which placeholder hasn't been replaced with the getTemplateVariables method. The method sends back a HashMap with the variables by document, header, footer, footnote, endnote or comment targets.
Working with prefixes allows to carry an extensive control of every placeholder, divided by groups of work, and even permits to classify them by substitution priority. This makes easier its further replacement and deletion.
Template methods replace placeholders in the main document body. To replace placeholders in other scopes such as header, footer, comment or footnote, the target option is available.
Other methods are available to work with templates. They are detailed on the API documentation pages.