Generate documents from templates with GroupDocs.Assembly

GroupDocs.Assembly generates documents from a template and a data source. Unlike the rest of the suite it needs an input you have to author yourself: a template carrying <<[...]>> tags that say where the data goes.

<<[expr]>> inserts a value, <<foreach [x in xs]>>…<</foreach>> repeats a block, and expressions can compute. On a Java object the expression calls a public getter, for example manager.getName(). JSON, XML and spreadsheet fields are referred to by name, for example Name or Client.Name. The output format follows the output file’s extension.

Generate a document from a Java object

Pass any object as a data source and name it. The name is what the template’s tags refer to — here manager. The template single-row.txt contains:

Name:	<<[manager.getName()]>>
Age:	<<[manager.getAge()]>>
import com.groupdocs.assembly.DataSourceInfo;
import com.groupdocs.assembly.DocumentAssembler;

public class Manager {
    private final String name;
    private final int age;

    public Manager(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public int getAge() {
        return age;
    }
}

Manager manager = new Manager("John Smith", 37);

DocumentAssembler assembler = new DocumentAssembler();

// "manager" must match the name the template's tags use
assembler.assembleDocument(
        "single-row.txt",
        "assembly-object.txt",
        new DataSourceInfo(manager, "manager"));

single-row.txt is the template used in this example. Click here to download it.

Name:	John Smith
Age:	37

Download full output

Warning
Data source classes must be public, and the template can only call public methods or read public fields. The engine reflects over the object at generation time, so a non-public class fails at run time rather than compile time. The same applies to the item type inside a collection.

Generate a document from JSON

JsonDataSource reads a JSON file. The name passed to DataSourceInfo is what the template iterates. simple-dataset.docx contains:

<<foreach [in managers]>>Manager: <<[Name]>>
Contracts:
<<foreach [in Contract]>>- <<[Client.Name]>> ($<<[Price]>>)
<</foreach>>
<</foreach>>

The file’s root is an array, so foreach [in managers] walks that array. Nested objects and arrays are reached by name: Contract, then Client.Name.

import com.groupdocs.assembly.DataSourceInfo;
import com.groupdocs.assembly.DocumentAssembler;
import com.groupdocs.assembly.JsonDataSource;

JsonDataSource managers = new JsonDataSource("manager-data.json");

DocumentAssembler assembler = new DocumentAssembler();
assembler.assembleDocument(
        "simple-dataset.docx",
        "assembly-json.docx",
        new DataSourceInfo(managers, "managers"));

simple-dataset.docx is the template and manager-data.json is the data. Download simple-dataset.docx and manager-data.json.

Manager: John Smith
Contracts:
- A Company ($1200000)
- B Ltd. ($750000)
- C & D ($350000)
Manager: Tony Anderson
Contracts:
- E Corp. ($650000)
- F & Partners ($550000)
Manager: July James
Contracts:
- G & Co. ($350000)
- H Group ($250000)
- I & Sons ($100000)
- J Ent. ($100000)

Download full output

The String argument of JsonDataSource is a file path. To assemble JSON held in memory, pass an InputStream instead.

Generate a document from a spreadsheet

DocumentTable reads one worksheet as a table of rows. setFirstRowContainsColumnNames(true) takes the first row as the field names. A space in a heading becomes an underscore, so the column Contract Price is Contract_Price in the template.

contracts-template.docx groups those rows by manager and sums the prices:

Total Contract Prices by Managers
Manager
Total Contract Price
<<foreach [group in contracts.groupBy(c => c.Manager).orderBy(g => g.key)]>><<[group.key]>>
<<[group.sum(c => c.Contract_Price)]>><</foreach>>
import com.groupdocs.assembly.DataSourceInfo;
import com.groupdocs.assembly.DocumentAssembler;
import com.groupdocs.assembly.DocumentTable;
import com.groupdocs.assembly.DocumentTableOptions;

DocumentTableOptions options = new DocumentTableOptions();
options.setFirstRowContainsColumnNames(true);

// First worksheet: Client, Manager, Contract Price
DocumentTable contracts = new DocumentTable("contracts-data.xlsx", 0, options);

DocumentAssembler assembler = new DocumentAssembler();
assembler.assembleDocument(
        "contracts-template.docx",
        "assembly-spreadsheet.docx",
        new DataSourceInfo(contracts, "contracts"));

contracts-template.docx is the template and contracts-data.xlsx is the table. Download contracts-template.docx and contracts-data.xlsx.

Total Contract Prices by Managers
Manager
Total Contract Price
John Smith
2300000.0
July James
800000.0
Tony Anderson
1200000.0

Download full output

Learn more

GroupDocs.Assembly also reads XML, CSV and database data sources, generates tables, lists and charts from collections, supports conditional blocks and master-detail reports, and can insert hyperlinks and images from the data.

Close
Loading

Analyzing your prompt, please hold on...

An error occurred while retrieving the results. Please refresh the page and try again.