Generate Word Documents from Templates Dynamically using Java

Generate word from template in java

The report generation process often consists of populating the predefined document templates containing the placeholders for desired fields. The reporting engines take the template document as input, populate the placeholders with the data dynamically and generate the resultant reports. If you are searching for a Java program to generate Word documents from templates, this article is going to give you the best solution. So let’s see how to create Word documents by populating the templates programmatically in Java.

Java Library to Generate Word Documents from Templates#

To generate Word documents from DOCX templates, we’ll use the LINQ Reporting Engine offered by Aspose.Words for Java. LINQ Reporting Engine supports a variety of tags for text, images, lists, tables, hyperlinks, and bookmarks for Word templates. The template documents containing these tags are populated by the engine with data coming from Java objects as well as XML, JSON, or CSV data sources. So let’s begin generating the Word documents from the templates using Java.

This article will cover how to generate a Word document from a template using:

Installing Aspose.Words for Java#

You can download Aspose.Words for Java JAR or add it to your Maven-based application using the following configurations.

Repository:

 AsposeJavaAPI Aspose Java API https://repository.aspose.com/repo/  

Dependency:

 com.aspose aspose-words 22.11 pom  

Generate Word Documents from Template using Java Objects#

To begin the report generation, let’s first create a Word document by populating the template with values from the Java objects. To define a document template, create a new Word document, insert the following tags and save it as a DOCX document.

> says: ">." 

In the above template, “s” will be treated as an object of Java class that will be used to populate the tags. So let’s create a class named Sender with two data members.

Now, it is time to pass the Word template to the LINQ Reporting Engine and generate the Word document based on the values of Sender’s object. The following are the steps for generating the Word document:

The following code sample shows how to generate a Word document from a Word template using the values of the Java object.

Output#

generate word document by template with Java objects

Java: Create Word Documents from Template using XML Data#

Let’s now take one step further and see how to populate the Word templates using an XML data source in a bit complex scenario. The following is the XML data source that we’ll use to populate the Word template.

In this scenario, we’ll use the following tags in the template document for multiple records in the XML data source.

>Name: >, Age: >, Date of Birth: > > Average age: <<[persons.average(p =>p.Age)]>> 

The Java code for generating Word document, in this case, would be the same and except for passing Java object as the data source, we’ll pass the XmlDataSource object in ReportingEngine.buildReport() method. The following code sample shows how to create a Word document by populating the document template using XML data source in Java.

Output#

generate word document by template with XML in Java

Java: Generate Word Documents from Template using JSON Data#

Let’s now have a look at how to generate Word document from the DOCX template using a JSON data source. Following is the JSON data that we’ll use in this example.

In this example, we’ll generate the Word document containing a list of the clients grouped by their managers. Following this scenario, the DOCX template will look like the following:

>Manager: > Contracts: >- > ($>) > > 

For loading JSON data source, Aspose.Words provides JsonDataSource class. The following code sample shows how to create a Word document from the template using a JSON data source in Java.

Output#

create word document with JSON in Java

Generate Word Documents with CSV Data in Java#

Last but not least, let’s check out how to generate the Word document by populating the Word template using the following CSV data source.

The following Word template will be used in this example:

>Name: >, Age: >, Date of Birth: > > Average age: <<[persons.average(p =>p.Column2)]>> 

For dealing with CSV data sources, Aspose.Words offers CsvDataSource class. The following code sample shows how to generate a Word document using a CSV data source in Java.

Output#

create word document with CSV in java

Generate Word Documents with a Free License#

You can get a free temporary license and generate Word documents without evaluation limitations.

Java LINQ Reporting Engine - Read More#

LINQ Reporting Engine supports a wide range of tags for generating fully-featured Word documents dynamically in Java. You can explore more about these tags along with their syntax in this article.

In case you would have any questions or queries, you can post to Aspose.Words Forum.

Related Article(s)#