GroupDocs.Assembly for .NET supports both file paths and streams for template loading and document saving. Understanding when to use each approach helps you build efficient, scalable applications. File paths are simpler for local file system operations, while streams are essential for memory-based processing, remote templates, and web applications.
The main class involved is:
DocumentAssembler - provides overloads for both file paths and streams
Here are the considerations for choosing between streams and file paths:
Use file paths for simple local file system operations
Use streams for memory-based processing, remote templates, or web applications
Mix file paths and streams as needed for your scenario
Always properly dispose of streams to avoid resource leaks
Note
DocumentAssembler provides multiple overloads supporting various combinations of file paths and streams for both input templates and output documents.
When to Use File Paths
File paths are ideal for:
Simple local file system operations
Templates stored on disk
Output documents saved to disk
Desktop applications with direct file access
usingGroupDocs.Assembly;publicstaticvoidUseFilePaths(){// Simple file path approachstringtemplatePath=@"C:\Templates\InvoiceTemplate.docx";stringoutputPath=@"C:\Output\Invoice.docx";vardata=new{CustomerName="ABC Corp",Amount=1500.00m};DocumentAssemblerassembler=newDocumentAssembler();assembler.AssembleDocument(templatePath,outputPath,newDataSourceInfo(data,"invoice"));Console.WriteLine("Document assembled using file paths.");}
When to Use Streams
Streams are essential for:
Memory-based processing (no disk I/O)
Templates loaded from databases or remote sources
Web applications serving documents directly
Processing templates in memory for performance
usingGroupDocs.Assembly;usingSystem.IO;publicstaticvoidUseStreams(){// Load template into memorybyte[]templateBytes=File.ReadAllBytes("Template.docx");using(MemoryStreamtemplateStream=newMemoryStream(templateBytes))using(MemoryStreamoutputStream=newMemoryStream()){vardata=new{Title="Report",Date=DateTime.Now};DocumentAssemblerassembler=newDocumentAssembler();assembler.AssembleDocument(templateStream,outputStream,newDataSourceInfo(data,"report"));// Use output stream (e.g., send via HTTP, save to database)byte[]outputBytes=outputStream.ToArray();File.WriteAllBytes("Output.docx",outputBytes);Console.WriteLine("Document assembled using streams.");}}
Mixed Approach: File Path Input, Stream Output
Load template from file, save to stream:
usingGroupDocs.Assembly;usingSystem.IO;publicstaticvoidFilePathInputStreamOutput(){// Template from file pathstringtemplatePath="Template.docx";// Output to memory streamusing(MemoryStreamoutputStream=newMemoryStream()){vardata=new{Name="Test",Value=100};DocumentAssemblerassembler=newDocumentAssembler();assembler.AssembleDocument(templatePath,outputStream,newDataSourceInfo(data,"item"));// Use output stream (e.g., send via HTTP response)byte[]outputBytes=outputStream.ToArray();Console.WriteLine($"Document assembled, output size: {outputBytes.Length} bytes");}}
Mixed Approach: Stream Input, File Path Output
Load template from stream, save to file:
usingGroupDocs.Assembly;usingSystem.IO;publicstaticvoidStreamInputFilePathOutput(){// Template from memory streambyte[]templateBytes=File.ReadAllBytes("Template.docx");using(MemoryStreamtemplateStream=newMemoryStream(templateBytes)){// Output to file pathstringoutputPath="Output.docx";vardata=new{Title="Report",Date=DateTime.Now};DocumentAssemblerassembler=newDocumentAssembler();assembler.AssembleDocument(templateStream,outputPath,newDataSourceInfo(data,"report"));Console.WriteLine("Document assembled from stream to file.");}}
Web Application Example
In web applications, streams are typically preferred:
usingGroupDocs.Assembly;usingSystem.IO;publicstaticvoidProperStreamDisposal(){// Good: Using statement ensures disposalusing(FileStreamtemplateStream=newFileStream("Template.docx",FileMode.Open))using(FileStreamoutputStream=newFileStream("Output.docx",FileMode.Create)){vardata=new{Name="Test"};DocumentAssemblerassembler=newDocumentAssembler();assembler.AssembleDocument(templateStream,outputStream,newDataSourceInfo(data,"item"));}// Streams are automatically disposed hereConsole.WriteLine("Streams properly disposed.");}
Warning
DocumentAssembler does not automatically dispose of input streams. Always use using statements or manually dispose streams to avoid resource leaks. Output streams are managed by the assembler.
Performance Considerations
File Paths: Simpler but require disk I/O
Streams: More flexible, can be faster for memory operations
Memory Streams: Best for small to medium documents
File Streams: Better for large documents to avoid memory pressure
Advanced Usage Topics
To learn more about advanced stream handling, custom stream implementations, 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: