Load Template Documents from File or Stream

Overview

GroupDocs.Assembly for .NET 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 string path parameter
  • Load template from a stream using Stream parameter
  • Choose the appropriate method based on your use case (file system vs memory/remote)
  • Save assembled document to file path or stream accordingly
Note
When using streams, ensure proper disposal of stream resources. The DocumentAssembler does not automatically dispose of input streams, so you should manage stream lifecycle yourself.

Load Template from File Path

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

using GroupDocs.Assembly;

public static void LoadTemplateFromFile()
{
    // Template and output file paths
    string templatePath = @"C:\Templates\InvoiceTemplate.docx";
    string outputPath = @"C:\Output\Invoice.docx";
    
    // Sample data
    var invoiceData = new { CustomerName = "ABC Corp", Amount = 1500.00 };
    
    // Assemble document from file path
    DocumentAssembler assembler = new DocumentAssembler();
    assembler.AssembleDocument(templatePath, outputPath, 
        new DataSourceInfo(invoiceData, "invoice"));
    
    Console.WriteLine("Document assembled successfully.");
}

Load Template from Stream

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

using GroupDocs.Assembly;
using System.IO;

public static void LoadTemplateFromStream()
{
    // Load template into memory stream
    byte[] templateBytes = File.ReadAllBytes("Template.docx");
    using (MemoryStream templateStream = new MemoryStream(templateBytes))
    {
        // Create output stream
        using (FileStream outputStream = new FileStream("Output.docx", FileMode.Create))
        {
            var data = new { Title = "Report", Date = DateTime.Now };
            
            DocumentAssembler assembler = new DocumentAssembler();
            assembler.AssembleDocument(templateStream, outputStream, 
                new DataSourceInfo(data, "report"));
            
            Console.WriteLine("Document assembled from stream successfully.");
        }
    }
}

Load Template from Remote Source

You can load templates from URLs or remote sources:

using GroupDocs.Assembly;
using System.IO;
using System.Net.Http;

public static async void LoadTemplateFromRemote()
{
    // Download template from remote URL
    using (HttpClient client = new HttpClient())
    {
        byte[] templateBytes = await client.GetByteArrayAsync(
            "https://example.com/templates/ReportTemplate.docx");
        
        using (MemoryStream templateStream = new MemoryStream(templateBytes))
        using (FileStream outputStream = new FileStream("Output.docx", FileMode.Create))
        {
            var data = new { Company = "My Company", Year = 2024 };
            
            DocumentAssembler assembler = new DocumentAssembler();
            assembler.AssembleDocument(templateStream, outputStream, 
                new DataSourceInfo(data, "company"));
            
            Console.WriteLine("Document assembled from remote template.");
        }
    }
}

Mixed File Path and Stream Usage

You can mix file paths and streams as needed:

using GroupDocs.Assembly;
using System.IO;

public static void MixedFileAndStream()
{
    // Load template from file path
    string templatePath = "Template.docx";
    
    // Save output to stream
    using (MemoryStream outputStream = new MemoryStream())
    {
        var data = new { Name = "Test", Value = 100 };
        
        DocumentAssembler assembler = new DocumentAssembler();
        assembler.AssembleDocument(templatePath, outputStream, 
            new DataSourceInfo(data, "item"));
        
        // Use the output stream (e.g., send via HTTP, save to database)
        byte[] outputBytes = outputStream.ToArray();
        File.WriteAllBytes("Output.docx", outputBytes);
        
        Console.WriteLine("Document assembled with mixed file/stream usage.");
    }
}
Warning
When using streams for output, the stream position is reset after assembly. Make sure to read from the stream after assembly completes, and handle the stream position appropriately.

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 .NET 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.