GroupDocs.Assembly for .NET allows you to generate output documents in a different format than the template. For example, you can create a DOCX template but output a PDF, or create an XLSX template but output HTML. The output format is determined by the file extension of the output path.
The main class involved is:
DocumentAssembler - assembles documents and converts to output format
Here are the steps to generate output in a different format:
Create a template document in one format (e.g., DOCX)
Specify an output path with a different file extension (e.g., .pdf, .html)
Assemble the document using DocumentAssembler
The output will be automatically converted to the specified format
Note
Format conversion is automatic based on the output file extension. Supported conversions depend on the template format - Word templates can convert to PDF, HTML, and more, while spreadsheet templates have more limited conversion options.
Convert DOCX Template to PDF
Generate a PDF from a Word template:
usingGroupDocs.Assembly;publicstaticvoidConvertDocxToPdf(){// DOCX template, PDF outputstringtemplatePath="InvoiceTemplate.docx";stringoutputPath="Invoice.pdf";varinvoiceData=new{CustomerName="ABC Corp",Amount=1500.00m,Date=DateTime.Now};DocumentAssemblerassembler=newDocumentAssembler();assembler.AssembleDocument(templatePath,outputPath,newDataSourceInfo(invoiceData,"invoice"));Console.WriteLine("PDF generated from DOCX template.");}
Convert DOCX Template to HTML
Generate an HTML file from a Word template:
usingGroupDocs.Assembly;publicstaticvoidConvertDocxToHtml(){// DOCX template, HTML outputstringtemplatePath="ReportTemplate.docx";stringoutputPath="Report.html";varreportData=new{Title="Monthly Report",Month="January 2024"};DocumentAssemblerassembler=newDocumentAssembler();assembler.AssembleDocument(templatePath,outputPath,newDataSourceInfo(reportData,"report"));Console.WriteLine("HTML generated from DOCX template.");}
Not all format combinations are supported. Word processing templates have the most conversion options, while spreadsheet and presentation templates have more limited output formats.
Advanced Usage Topics
To learn more about format-specific options, conversion settings, and advanced format 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: