Generate documents from templates with GroupDocs.Assembly
Leave feedback
On this page
GroupDocs.Assembly generates documents from a template and a data source. Unlike the rest of the suite it needs an input you have to author yourself: a template carrying <<[...]>> tags that say where the data goes.
Every example below uses invoice-template.docx, which contains:
Invoice <<[invoice.Number]>>
Bill to: <<[invoice.CustomerName]>>
Date: <<[invoice.Date]:"dd MMMM yyyy">>
Items
<<foreach [item in invoice.Items]>><<[item.Description]>> - qty <<[item.Quantity]>> - <<[item.Price]>> USD<</foreach>>
Total: <<[invoice.Items.Sum(i => i.Quantity * i.Price)]>> USD
The tag syntax is the LINQ Reporting Engine: <<[expr]>> inserts a value, <<foreach [x in xs]>>…<</foreach>> repeats a block, and expressions can compute — note the Sum in the total.
Generate a document from .NET objects
Pass any object as a data source and name it. The name is what the template’s tags refer to — here invoice.
usingSystem;usingSystem.Collections.Generic;usingGroupDocs.Assembly;publicclassInvoiceItem{publicstringDescription{get;set;}="";publicintQuantity{get;set;}publicdecimalPrice{get;set;}}publicclassInvoice{publicstringNumber{get;set;}="";publicstringCustomerName{get;set;}="";publicDateTimeDate{get;set;}publicList<InvoiceItem>Items{get;set;}=new();}varinvoice=newInvoice{Number="INV-2026-0042",CustomerName="Northwind Trading Ltd",Date=newDateTime(2026,7,16),Items=newList<InvoiceItem>{newInvoiceItem{Description="Document processing licence",Quantity=3,Price=1200m},newInvoiceItem{Description="Priority support",Quantity=1,Price=800m},newInvoiceItem{Description="Onboarding workshop",Quantity=2,Price=450m}}};DocumentAssemblerassembler=newDocumentAssembler();// "invoice" must match the name the template's tags useassembler.AssembleDocument("invoice-template.docx","assembly-from-objects.docx",newDataSourceInfo(invoice,"invoice"));Console.WriteLine("Generated assembly-from-objects.docx");
invoice-template.docx is the template used in this example. Click here to download it.
The output format follows the output file’s extension. The same template and the same data produce a PDF instead of a Word file with no other change.
usingSystem;usingSystem.Collections.Generic;usingGroupDocs.Assembly;// Data classes must be public — see the note belowpublicclassLineItem{publicstringDescription{get;set;}="";publicintQuantity{get;set;}publicdecimalPrice{get;set;}}publicclassOrder{publicstringNumber{get;set;}="";publicstringCustomerName{get;set;}="";publicDateTimeDate{get;set;}publicList<LineItem>Items{get;set;}=new();}varinvoice=newOrder{Number="INV-2026-0043",CustomerName="Contoso Manufacturing",Date=newDateTime(2026,7,16),Items=newList<LineItem>{newLineItem{Description="Annual subscription",Quantity=1,Price=9600m},newLineItem{Description="Additional seats",Quantity=5,Price=240m}}};DocumentAssemblerassembler=newDocumentAssembler();assembler.AssembleDocument("invoice-template.docx","assembly-to-pdf.pdf",newDataSourceInfo(invoice,"invoice"));Console.WriteLine("Generated assembly-to-pdf.pdf");
invoice-template.docx is the template used in this example. Click here to download it.
Data source classes must be public. The engine reflects over the object at generation time, so an anonymous type or an internal class fails at run time rather than compile time:
System.InvalidOperationException: A data source object must be of a visible type
or implement 'System.Collections.IEnumerable'. (Parameter 'dataSource')
The same applies to the item type inside a collection — List<LineItem> needs LineItem public too.
Learn more
GroupDocs.Assembly also reads JSON, XML, CSV, DataTable and database data sources, generates tables and charts from collections, supports conditional blocks, master-detail reports, hyperlinks and images in templates, and can produce output in any format the suite supports.