Generate Document from Custom Java Objects
Leave feedback
Overview
One of the most common scenarios in document assembly is generating reports from custom Java objects. GroupDocs.Assembly for Java makes it easy to bind your business objects directly to templates, allowing you to create type-safe, maintainable document generation code.
Here are the steps to generate documents from custom objects:
Define your custom public Java class with public getters
Create an instance of the class and populate it with data
Create a template document with expression tags calling the object’s getters
Use DocumentAssembler to assemble the template with your object
Save the assembled document
Note
Custom classes must be public, and their members must be public methods (such as getters) or public fields to be accessible in templates. Methods are called in templates explicitly, for example customer.getName(). Private members are not accessible by the template engine.
Define a Custom Class
Start by defining a class with getters you want to use in your template:
Generate a document from a single object instance:
importcom.groupdocs.assembly.*;importjava.util.*;publicstaticvoidgenerateFromSingleObject()throwsException{// Create and populate object
Customercustomer=newCustomer();customer.setName("John Doe");customer.setEmail("john.doe@example.com");customer.setAddress("123 Main St, New York, NY 10001");customer.setRegistrationDate(newGregorianCalendar(2023,Calendar.JANUARY,15).getTime());customer.setBalance(1250.75);// Assemble document
DocumentAssemblerassembler=newDocumentAssembler();assembler.assembleDocument("CustomerTemplate.docx","CustomerReport.docx",newDataSourceInfo(customer,"customer"));System.out.println("Document generated from custom object successfully.");}
Template Example
Your template (CustomerTemplate.docx) can call object getters:
Because of Java type erasure, the engine cannot determine the item type of a java.util.List passed directly as a data source, so calling item methods such as getName() fails. Pass an array (as shown above), or expose the list through a getter of a wrapper object (for example, public List<Customer> getCustomers()) and iterate over it in the template: <<foreach [in data.getCustomers()]>>.
Using Nested Objects
Custom objects can contain nested objects:
importcom.groupdocs.assembly.*;importjava.util.Date;publicclassOrder{privateintorderId;privateDateorderDate;privateCustomercustomer;privatedoubletotalAmount;publicintgetOrderId(){returnorderId;}publicvoidsetOrderId(intvalue){this.orderId=value;}publicDategetOrderDate(){returnorderDate;}publicvoidsetOrderDate(Datevalue){this.orderDate=value;}publicCustomergetCustomer(){returncustomer;}publicvoidsetCustomer(Customervalue){this.customer=value;}publicdoublegetTotalAmount(){returntotalAmount;}publicvoidsetTotalAmount(doublevalue){this.totalAmount=value;}}publicstaticvoidgenerateFromNestedObjects()throwsException{Customercustomer=newCustomer();customer.setName("John Doe");customer.setEmail("john@example.com");Orderorder=newOrder();order.setOrderId(1001);order.setOrderDate(newDate());order.setCustomer(customer);order.setTotalAmount(599.99);DocumentAssemblerassembler=newDocumentAssembler();assembler.assembleDocument("OrderTemplate.docx","OrderReport.docx",newDataSourceInfo(order,"order"));System.out.println("Document generated from nested objects successfully.");}
Methods called in templates must be public and should not have side effects. The template engine may call methods multiple times during document assembly.
Advanced Usage Topics
To learn more about working with complex object hierarchies, collections, collection extension methods on objects, and performance optimization, 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: