Templ4docx 2.0.0 - Text Variables

The most common situation is when we want to replace simple text variable. Imagine that you have an invitation template and you want to prepare filled invitation with firstname and lastname. So the only thing you need is to find variables in the template and then replace them with the correct values. Suppose that the variables in the template are as follows: ${firstname} and ${lastname}. This pattern of variables is the default one for templ4docx and does not require to define it. However, if the variables in the template have a different form, for example: #{firstname}, you must tell to templ4docx how they look like. In this case you must define the object VariablePattern in the following way new VariablePattern("#{", "}") and then set it to Docx object:

Docx docx = new Docx("template.docx");
docx.setVariablePattern(new VariablePattern("#{", "}"));

At this point you are ready to play with template.

Docx docx = new Docx("template.docx");
docx.setVariablePattern(new VariablePattern("#{", "}"));
    
// preparing variables
Variables variables = new Variables();
variables.addTextVariable(new TextVariable("#{firstname}", "Lukasz"));
variables.addTextVariable(new TextVariable("#{lastname}", "Stypka"));
        
// fill template
docx.fillTemplate(variables);
        
// save filled .docx file
docx.save("lstypka.docx");

Text variables may be placed anywhere: in a paragraph, table cell, numbered \ bullet list … The important thing is that style of text is preserved. So if you want to create, for example bolded lastname, just create bolded variable #{lastname}.


Łukasz Stypka

Java experienced enthusiast, opened for new technologies. Clean code and simple solution fan.

Tomasz Kuryłek

Self-styled and passionate java hipster. Focused on solution that makes user and himself ecstatic..