Generate Your First Document from Java Object

Overview

This guide walks you through creating your first document with GroupDocs.Assembly for Java. You’ll generate a simple document from an in-memory Java object, demonstrating the basic workflow of template-based document assembly.

The main classes involved are:

Here are the steps to generate your first document:

  • Create a simple public Java class with getters to represent your data
  • Instantiate the class and populate it with sample data
  • Create a template document (DOCX) with expression tags calling the object’s getters
  • Use DocumentAssembler to assemble the template with your data object
  • Save the assembled document to a file

Define a Simple Data Class

First, create a simple class to hold your data:

public class Person
{
    private String name;
    private int age;
    private String email;

    public String getName() { return name; }
    public void setName(String value) { this.name = value; }
    public int getAge() { return age; }
    public void setAge(int value) { this.age = value; }
    public String getEmail() { return email; }
    public void setEmail(String value) { this.email = value; }
}
Note
The data class must be public. Otherwise, the engine rejects it as a data source with the “A data source object must be of a visible type” error.

Generate Document from Object

Now, create a method to generate a document from this object:

import com.groupdocs.assembly.*;

public static void generateFirstDocument() throws Exception
{
    // Create sample data
    Person person = new Person();
    person.setName("John Doe");
    person.setAge(30);
    person.setEmail("john.doe@example.com");

    // Paths for template and output
    String templatePath = "Template.docx";
    String outputPath = "Output.docx";

    // Assemble the document
    DocumentAssembler assembler = new DocumentAssembler();
    assembler.assembleDocument(templatePath, outputPath,
        new DataSourceInfo(person, "person"));

    System.out.println("Document generated successfully: " + outputPath);
}

Template Document Example

Your template document (Template.docx) should contain expression tags like this:

Name: <<[person.getName()]>>
Age: <<[person.getAge()]>>
Email: <<[person.getEmail()]>>

After assembly, the output will be:

Name: John Doe
Age: 30
Email: john.doe@example.com
Note
Members of Java objects are accessed in templates by calling their public methods, such as getters (person.getName()). A property-like reference such as person.Name does not work for Java objects.

Using Contextual Object Member Access

You can also use contextual member access, which allows you to omit the data source name in simple scenarios:

public static void generateDocumentWithContextualAccess() throws Exception
{
    Person person = new Person();
    person.setName("Jane Smith");
    person.setAge(28);
    person.setEmail("jane.smith@example.com");

    DocumentAssembler assembler = new DocumentAssembler();
    // When using contextual access, you can omit the data source name
    assembler.assembleDocument("Template.docx", "Output.docx",
        new DataSourceInfo(person));
}

With contextual access, your template can use:

Name: <<[getName()]>>
Age: <<[getAge()]>>
Email: <<[getEmail()]>>
Note
When using contextual object member access, the template expressions can call the object’s methods directly without the data source name prefix. This is useful for simple, single-object scenarios.

Advanced Usage Topics

To learn more about working with complex data structures, multiple data sources, and advanced template syntax, 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:

Free Online Apps

Along with the full-featured Java library, we provide simple but powerful free online apps.

To assemble documents from templates and data sources, you can use the online apps from the GroupDocs.Assembly App Product Family.

Close
Loading

Analyzing your prompt, please hold on...

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