Load Template Documents from File or Stream

Overview

GroupDocs.Assembly for Java supports loading template documents from both file paths and streams. This flexibility allows you to work with templates stored on disk, in memory, or retrieved from remote sources.

The main class involved is:

Here are the steps to load and use templates:

  • Load template from a file path using a String path parameter
  • Load template from a stream using an InputStream parameter
  • Choose the appropriate method based on your use case (file system vs memory/remote)
  • Save assembled document to a file path or an OutputStream accordingly
Note
When using streams, ensure proper closing of stream resources. The DocumentAssembler does not close the input and output streams passed to it, so you should manage the stream lifecycle yourself (for example, with try-with-resources).

Load Template from File Path

The simplest way to load a template is from a file path:

import com.groupdocs.assembly.*;

public static void loadTemplateFromFile() throws Exception
{
    // Template and output file paths
    String templatePath = "C:\\Templates\\InvoiceTemplate.docx";
    String outputPath = "C:\\Output\\Invoice.docx";

    // Sample data
    InvoiceData invoiceData = new InvoiceData("ABC Corp", 1500.00);

    // Assemble document from file path
    DocumentAssembler assembler = new DocumentAssembler();
    assembler.assembleDocument(templatePath, outputPath,
        new DataSourceInfo(invoiceData, "invoice"));

    System.out.println("Document assembled successfully.");
}

// POJO for the data source
public class InvoiceData
{
    private final String customerName;
    private final double amount;

    public InvoiceData(String customerName, double amount)
    {
        this.customerName = customerName;
        this.amount = amount;
    }

    public String getCustomerName() { return customerName; }
    public double getAmount() { return amount; }
}

Load Template from Stream

When working with templates in memory or from remote sources, use streams:

import com.groupdocs.assembly.*;
import java.io.*;
import java.nio.file.*;
import java.util.Date;

public static void loadTemplateFromStream() throws Exception
{
    // Load template into memory stream
    byte[] templateBytes = Files.readAllBytes(Paths.get("Template.docx"));
    try (InputStream templateStream = new ByteArrayInputStream(templateBytes);
         // Create output stream
         OutputStream outputStream = new FileOutputStream("Output.docx"))
    {
        ReportData data = new ReportData("Report", new Date());

        DocumentAssembler assembler = new DocumentAssembler();
        assembler.assembleDocument(templateStream, outputStream,
            new DataSourceInfo(data, "report"));

        System.out.println("Document assembled from stream successfully.");
    }
}

// POJO for the data source
public class ReportData
{
    private final String title;
    private final Date date;

    public ReportData(String title, Date date)
    {
        this.title = title;
        this.date = date;
    }

    public String getTitle() { return title; }
    public Date getDate() { return date; }
}

Load Template from Remote Source

You can load templates from URLs or remote sources:

import com.groupdocs.assembly.*;
import java.io.*;
import java.net.URI;

public static void loadTemplateFromRemote() throws Exception
{
    // Download template from remote URL
    try (InputStream templateStream = URI.create(
             "https://example.com/templates/ReportTemplate.docx").toURL().openStream();
         OutputStream outputStream = new FileOutputStream("Output.docx"))
    {
        CompanyData data = new CompanyData("My Company", 2024);

        DocumentAssembler assembler = new DocumentAssembler();
        assembler.assembleDocument(templateStream, outputStream,
            new DataSourceInfo(data, "company"));

        System.out.println("Document assembled from remote template.");
    }
}

// POJO for the data source
public class CompanyData
{
    private final String company;
    private final int year;

    public CompanyData(String company, int year)
    {
        this.company = company;
        this.year = year;
    }

    public String getCompany() { return company; }
    public int getYear() { return year; }
}

Mixed File Path and Stream Usage

You can mix files and streams as needed. DocumentAssembler accepts either two paths or two streams, so open the template file as a stream when the output should go to a stream:

import com.groupdocs.assembly.*;
import java.io.*;
import java.nio.file.*;

public static void mixedFileAndStream() throws Exception
{
    // Load template from file path
    String templatePath = "Template.docx";

    // Save output to stream
    try (InputStream templateStream = new FileInputStream(templatePath);
         ByteArrayOutputStream outputStream = new ByteArrayOutputStream())
    {
        ItemData data = new ItemData("Test", 100);

        DocumentAssembler assembler = new DocumentAssembler();
        assembler.assembleDocument(templateStream, outputStream,
            new DataSourceInfo(data, "item"));

        // Use the output stream (e.g., send via HTTP, save to database)
        byte[] outputBytes = outputStream.toByteArray();
        Files.write(Paths.get("Output.docx"), outputBytes);

        System.out.println("Document assembled with mixed file/stream usage.");
    }
}

// POJO for the data source
public class ItemData
{
    private final String name;
    private final int value;

    public ItemData(String name, int value)
    {
        this.name = name;
        this.value = value;
    }

    public String getName() { return name; }
    public int getValue() { return value; }
}
Warning
When using streams for output, read the assembled content (for example, via ByteArrayOutputStream.toByteArray()) only after assembleDocument returns. The output stream is neither closed nor reset by the assembler, so close it yourself when you are done.

Advanced Usage Topics

To learn more about working with different template formats, custom document loading, and advanced stream handling, 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.