Load Template Documents from File or Stream
Leave feedback
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:
DocumentAssembler - provides overloads for both file paths and streams
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:
usingGroupDocs.Assembly;publicstaticvoidLoadTemplateFromFile(){// Template and output file pathsstringtemplatePath=@"C:\Templates\InvoiceTemplate.docx";stringoutputPath=@"C:\Output\Invoice.docx";// Sample datavarinvoiceData=new{CustomerName="ABC Corp",Amount=1500.00};// Assemble document from file pathDocumentAssemblerassembler=newDocumentAssembler();assembler.AssembleDocument(templatePath,outputPath,newDataSourceInfo(invoiceData,"invoice"));Console.WriteLine("Document assembled successfully.");}
Load Template from Stream
When working with templates in memory or from remote sources, use streams:
usingGroupDocs.Assembly;usingSystem.IO;publicstaticvoidLoadTemplateFromStream(){// Load template into memory streambyte[]templateBytes=File.ReadAllBytes("Template.docx");using(MemoryStreamtemplateStream=newMemoryStream(templateBytes)){// Create output streamusing(FileStreamoutputStream=newFileStream("Output.docx",FileMode.Create)){vardata=new{Title="Report",Date=DateTime.Now};DocumentAssemblerassembler=newDocumentAssembler();assembler.AssembleDocument(templateStream,outputStream,newDataSourceInfo(data,"report"));Console.WriteLine("Document assembled from stream successfully.");}}}
Load Template from Remote Source
You can load templates from URLs or remote sources:
usingGroupDocs.Assembly;usingSystem.IO;usingSystem.Net.Http;publicstaticasyncvoidLoadTemplateFromRemote(){// Download template from remote URLusing(HttpClientclient=newHttpClient()){byte[]templateBytes=awaitclient.GetByteArrayAsync("https://example.com/templates/ReportTemplate.docx");using(MemoryStreamtemplateStream=newMemoryStream(templateBytes))using(FileStreamoutputStream=newFileStream("Output.docx",FileMode.Create)){vardata=new{Company="My Company",Year=2024};DocumentAssemblerassembler=newDocumentAssembler();assembler.AssembleDocument(templateStream,outputStream,newDataSourceInfo(data,"company"));Console.WriteLine("Document assembled from remote template.");}}}
Mixed File Path and Stream Usage
You can mix file paths and streams as needed:
usingGroupDocs.Assembly;usingSystem.IO;publicstaticvoidMixedFileAndStream(){// Load template from file pathstringtemplatePath="Template.docx";// Save output to streamusing(MemoryStreamoutputStream=newMemoryStream()){vardata=new{Name="Test",Value=100};DocumentAssemblerassembler=newDocumentAssembler();assembler.AssembleDocument(templatePath,outputStream,newDataSourceInfo(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: