Basic Template Syntax - Expression Tags and Fields
Leave feedback
Overview
GroupDocs.Assembly for Java uses a simple template syntax that allows you to embed Java expressions directly in your documents, extended with LINQ-style extension methods for collections. Expression tags are the foundation of template syntax, enabling you to output data values and perform calculations.
Create a template document in Word, Excel, PowerPoint, or HTML format
Insert expression tags using <<[expression]>> syntax
Call public methods (such as getters) of data source objects in expressions
Assemble the document with your data source
Note
Template syntax is case-sensitive and follows Java language rules. Members of Java objects are accessed by calling their public methods, for example person.getName(). Expression tags are evaluated at runtime and replaced with their computed values.
Basic Expression Tags
Expression tags are the simplest way to output data in templates. They use the syntax <<[expression]>>:
importcom.groupdocs.assembly.*;publicstaticvoidbasicExpressionTags()throwsException{// Sample data
Personperson=newPerson("John Doe",30,"john@example.com");// Assemble document
DocumentAssemblerassembler=newDocumentAssembler();assembler.assembleDocument("Template.docx","Output.docx",newDataSourceInfo(person,"person"));System.out.println("Document assembled with expression tags.");}// POJO for the data source
publicclassPerson{privatefinalStringname;privatefinalintage;privatefinalStringemail;publicPerson(Stringname,intage,Stringemail){this.name=name;this.age=age;this.email=email;}publicStringgetName(){returnname;}publicintgetAge(){returnage;}publicStringgetEmail(){returnemail;}}
Template Example with Expression Tags
In your template document (Template.docx), use expression tags like this:
When using a single data source, you can omit the data source name and use contextual access:
importcom.groupdocs.assembly.*;publicstaticvoidcontextualAccess()throwsException{Productproduct=newProduct("Laptop",999.99,50);DocumentAssemblerassembler=newDocumentAssembler();assembler.assembleDocument("Template.docx","Output.docx",newDataSourceInfo(product));System.out.println("Document assembled with contextual access.");}// POJO for the data source
publicclassProduct{privatefinalStringname;privatefinaldoubleprice;privatefinalintstock;publicProduct(Stringname,doubleprice,intstock){this.name=name;this.price=price;this.stock=stock;}publicStringgetName(){returnname;}publicdoublegetPrice(){returnprice;}publicintgetStock(){returnstock;}}
You can perform calculations directly in expression tags:
importcom.groupdocs.assembly.*;publicstaticvoidcalculationsInExpressions()throwsException{Orderorder=newOrder(5,29.99);DocumentAssemblerassembler=newDocumentAssembler();assembler.assembleDocument("Template.docx","Output.docx",newDataSourceInfo(order,"order"));System.out.println("Document assembled with calculations.");}// POJO for the data source
publicclassOrder{privatefinalintquantity;privatefinaldoubleunitPrice;publicOrder(intquantity,doubleunitPrice){this.quantity=quantity;this.unitPrice=unitPrice;}publicintgetQuantity(){returnquantity;}publicdoublegetUnitPrice(){returnunitPrice;}}
Template with calculations:
Quantity: <<[order.getQuantity()]>>
Unit Price: <<[order.getUnitPrice()]>>
Total: <<[order.getQuantity() * order.getUnitPrice()]>>
Note
Arithmetic operators in templates work with primitive numeric types such as int and double. They cannot be applied to BigDecimal values, so use primitive types for the values you calculate with in templates.
Using String Methods
You can use Java String methods in expressions:
importcom.groupdocs.assembly.*;publicstaticvoidstringMethods()throwsException{Companycompany=newCompany("ABC Corporation","www.example.com");DocumentAssemblerassembler=newDocumentAssembler();assembler.assembleDocument("Template.docx","Output.docx",newDataSourceInfo(company,"company"));System.out.println("Document assembled with string methods.");}// POJO for the data source
publicclassCompany{privatefinalStringname;privatefinalStringwebsite;publicCompany(Stringname,Stringwebsite){this.name=name;this.website=website;}publicStringgetName(){returnname;}publicStringgetWebsite(){returnwebsite;}}
You can format expression results using format strings:
importcom.groupdocs.assembly.*;importjava.util.*;publicstaticvoidformattedExpressions()throwsException{Invoiceinvoice=newInvoice(1500.50,newGregorianCalendar(2024,Calendar.JANUARY,15).getTime(),0.15);DocumentAssemblerassembler=newDocumentAssembler();assembler.assembleDocument("Template.docx","Output.docx",newDataSourceInfo(invoice,"invoice"));System.out.println("Document assembled with formatting.");}// POJO for the data source
publicclassInvoice{privatefinaldoubleamount;privatefinalDatedate;privatefinaldoublepercentage;publicInvoice(doubleamount,Datedate,doublepercentage){this.amount=amount;this.date=date;this.percentage=percentage;}publicdoublegetAmount(){returnamount;}publicDategetDate(){returndate;}publicdoublegetPercentage(){returnpercentage;}}
Format strings follow Java formatting conventions: java.text.DecimalFormat patterns for numbers (for example, "$#,##0.00" for currency or "0.00%" for percentage) and java.text.SimpleDateFormat patterns for dates. .NET standard format specifiers such as "C" or "P" are not supported. Decimal and grouping separators depend on the default locale of the JVM.
Advanced Usage Topics
To learn more about data bands, conditional blocks, collection extension methods, and advanced template syntax features, please refer to the advanced usage section.
More resources
GitHub Examples
You may easily run the code above and see the feature in action in our GitHub examples: