Generate Output in Same Format as Template
Leave feedback
Overview
By default, GroupDocs.Assembly for .NET saves assembled documents in the same format as the template. This means a DOCX template produces a DOCX output, an XLSX template produces an XLSX output, and so on. The output format is determined by the file extension of the output path.
The main class involved is:
DocumentAssembler - assembles documents and determines output format
Here are the steps to generate output in the same format:
Create a template document in your desired format (DOCX, XLSX, PPTX, etc.)
Specify an output path with the same file extension as the template
Assemble the document using DocumentAssembler
The output will automatically be in the same format as the template
Note
The output format is automatically determined by the file extension of the output path. No additional configuration is needed to preserve the template format.
Generate DOCX from DOCX Template
Generate a Word document from a Word template:
usingGroupDocs.Assembly;publicstaticvoidGenerateDocxFromDocx(){// Template and output are both DOCXstringtemplatePath="InvoiceTemplate.docx";stringoutputPath="Invoice.docx";varinvoiceData=new{CustomerName="ABC Corp",Amount=1500.00m,Date=DateTime.Now};DocumentAssemblerassembler=newDocumentAssembler();assembler.AssembleDocument(templatePath,outputPath,newDataSourceInfo(invoiceData,"invoice"));Console.WriteLine("DOCX document generated from DOCX template.");}
Generate XLSX from XLSX Template
Generate an Excel spreadsheet from an Excel template:
usingGroupDocs.Assembly;publicstaticvoidGenerateXlsxFromXlsx(){// Template and output are both XLSXstringtemplatePath="ReportTemplate.xlsx";stringoutputPath="Report.xlsx";varreportData=new{Title="Monthly Sales",Month="January 2024"};DocumentAssemblerassembler=newDocumentAssembler();assembler.AssembleDocument(templatePath,outputPath,newDataSourceInfo(reportData,"report"));Console.WriteLine("XLSX document generated from XLSX template.");}
Generate PPTX from PPTX Template
Generate a PowerPoint presentation from a PowerPoint template:
usingGroupDocs.Assembly;publicstaticvoidGeneratePptxFromPptx(){// Template and output are both PPTXstringtemplatePath="PresentationTemplate.pptx";stringoutputPath="Presentation.pptx";varpresentationData=new{Title="Quarterly Review",Quarter="Q1 2024"};DocumentAssemblerassembler=newDocumentAssembler();assembler.AssembleDocument(templatePath,outputPath,newDataSourceInfo(presentationData,"presentation"));Console.WriteLine("PPTX document generated from PPTX template.");}
Generate MSG from MSG Template
Generate an email message from an email template:
usingGroupDocs.Assembly;publicstaticvoidGenerateMsgFromMsg(){// Template and output are both MSGstringtemplatePath="EmailTemplate.msg";stringoutputPath="Email.msg";varemailData=new{Recipient="customer@example.com",Subject="Invoice",Body="Please find attached invoice."};DocumentAssemblerassembler=newDocumentAssembler();assembler.AssembleDocument(templatePath,outputPath,newDataSourceInfo(emailData,"email"));Console.WriteLine("MSG document generated from MSG template.");}
Using Streams with Same Format
When using streams, the format is still preserved based on the template format:
usingGroupDocs.Assembly;usingSystem.IO;publicstaticvoidGenerateSameFormatFromStream(){// Load DOCX template from streambyte[]templateBytes=File.ReadAllBytes("Template.docx");using(MemoryStreamtemplateStream=newMemoryStream(templateBytes))using(FileStreamoutputStream=newFileStream("Output.docx",FileMode.Create)){vardata=new{Name="Test",Value=100};DocumentAssemblerassembler=newDocumentAssembler();assembler.AssembleDocument(templateStream,outputStream,newDataSourceInfo(data,"item"));Console.WriteLine("Document generated from stream in same format.");}}
Format Compatibility
The following formats support same-format output:
Word Processing: DOCX, DOC, RTF, ODT, OTT
Spreadsheet: XLSX, XLS, XLSM, ODS
Presentation: PPTX, PPT, PPTM, ODP
Email: MSG, EML, EMLX
Other: HTML, TXT, XML
Warning
Some older formats (like DOC, XLS, PPT) may have limitations compared to their newer counterparts (DOCX, XLSX, PPTX). For best results, use the newer Office Open XML formats.
Advanced Usage Topics
To learn more about format conversion, saving to different formats, and format-specific options, 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: