How to insert conditional expression in docx file template


Aniket Gedam

2024-06-20 18:21:49


How to insert the conditional statement in docx template. 

1. Open the docx file in microsoft word. (preferably 2007 as it is tested one. if you know how mergefield works then you can use whichever you are comfortable with)

2. go to field--->merge field

3. In the field name put your expression . Suppose I have status field which is being passed down from java code and I want conditional statement on it. 
if  status == "Active" the do something. then we need to put  
[#if status=='Active']  as the field name in the mergefield  box. 

4. This opens the if condition. Similary we need to close it. 
Use another merge field  and put [/#if] it will close the if block.
the result of this will look like : 

5. After that you can put your input in between these tags which you want to show as follows . 
<<[#if status=='Active']>>
text to be displayed
<<[/#if]>>

Depending on the condition the you text  will be displayed. 
if status is 'Active' you will be able to see text to be displayed in the output.

similary you can try different expressions as you need.
Please refer https://freemarker.apache.org/docs/dgui_template_exp.html#dgui_template_exp_builtin for more info.