Generate Output in Same Format as Template

View as MarkdownMarkdown

Set up your AI agent

Give your coding agent live access to the GroupDocs documentation and API reference through the GroupDocs Docs MCP server — no install, no API key. Learn more

claude mcp add --transport http groupdocs-docs https://docs.groupdocs.com/mcp

Also process documents locally

The GroupDocs.Assembly MCP server lets your agent work with files on your machine; they are never uploaded. It needs the .NET 10 SDK or Docker.

claude mcp add groupdocs-assembly -- dnx GroupDocs.Assembly.Mcp --yes
Folders, licensing and more →

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:

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:

using GroupDocs.Assembly;

public static void GenerateDocxFromDocx()
{
    // Template and output are both DOCX
    string templatePath = "InvoiceTemplate.docx";
    string outputPath = "Invoice.docx";
    
    var invoiceData = new { 
        CustomerName = "ABC Corp", 
        Amount = 1500.00m,
        Date = DateTime.Now
    };
    
    DocumentAssembler assembler = new DocumentAssembler();
    assembler.AssembleDocument(templatePath, outputPath, 
        new DataSourceInfo(invoiceData, "invoice"));
    
    Console.WriteLine("DOCX document generated from DOCX template.");
}

Generate XLSX from XLSX Template

Generate an Excel spreadsheet from an Excel template:

using GroupDocs.Assembly;

public static void GenerateXlsxFromXlsx()
{
    // Template and output are both XLSX
    string templatePath = "ReportTemplate.xlsx";
    string outputPath = "Report.xlsx";
    
    var reportData = new { 
        Title = "Monthly Sales",
        Month = "January 2024"
    };
    
    DocumentAssembler assembler = new DocumentAssembler();
    assembler.AssembleDocument(templatePath, outputPath, 
        new DataSourceInfo(reportData, "report"));
    
    Console.WriteLine("XLSX document generated from XLSX template.");
}

Generate PPTX from PPTX Template

Generate a PowerPoint presentation from a PowerPoint template:

using GroupDocs.Assembly;

public static void GeneratePptxFromPptx()
{
    // Template and output are both PPTX
    string templatePath = "PresentationTemplate.pptx";
    string outputPath = "Presentation.pptx";
    
    var presentationData = new { 
        Title = "Quarterly Review",
        Quarter = "Q1 2024"
    };
    
    DocumentAssembler assembler = new DocumentAssembler();
    assembler.AssembleDocument(templatePath, outputPath, 
        new DataSourceInfo(presentationData, "presentation"));
    
    Console.WriteLine("PPTX document generated from PPTX template.");
}

Generate MSG from MSG Template

Generate an email message from an email template:

using GroupDocs.Assembly;

public static void GenerateMsgFromMsg()
{
    // Template and output are both MSG
    string templatePath = "EmailTemplate.msg";
    string outputPath = "Email.msg";
    
    var emailData = new { 
        Recipient = "customer@example.com",
        Subject = "Invoice",
        Body = "Please find attached invoice."
    };
    
    DocumentAssembler assembler = new DocumentAssembler();
    assembler.AssembleDocument(templatePath, outputPath, 
        new DataSourceInfo(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:

using GroupDocs.Assembly;
using System.IO;

public static void GenerateSameFormatFromStream()
{
    // Load DOCX template from stream
    byte[] templateBytes = File.ReadAllBytes("Template.docx");
    
    using (MemoryStream templateStream = new MemoryStream(templateBytes))
    using (FileStream outputStream = new FileStream("Output.docx", FileMode.Create))
    {
        var data = new { Name = "Test", Value = 100 };
        
        DocumentAssembler assembler = new DocumentAssembler();
        assembler.AssembleDocument(templateStream, outputStream, 
            new DataSourceInfo(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:

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.