The shortest complete path from data to a document: a template somebody edits in Word, a data
source of your choosing, and one call. No Microsoft Office on the server, no Open XML by hand, and
the layout stays with the people who own the report.
A tag is <<[expression]>>. The expression is evaluated against a data source that you name
yourself, and a format may follow the closing bracket:
Order <<[order.Number]>> for <<[order.Customer]>>
Placed on <<[order.Placed]:"dd MMMM yyyy">>
<<foreach [line in order.Lines]>><<[line.Product]>> x<<[line.Quantity]>> - <<[line.Total]:"0.00">> EUR
<</foreach>>
Total: <<[order.Lines.Sum(l => l.Total)]:"0.00">> EUR
Three things are worth noticing. foreach repeats the text between its tags once per element. LINQ
is available inside the expressions — Sum, Where, GroupBy, OrderBy — so a total is part of
the template rather than of your code. And a format after the bracket, not inside it:
<<[order.Placed]:"dd MMMM yyyy">>.
The same tags work in a .docx, where they sit in the paragraphs, tables and headers of a document
your colleagues style in Word. See
template syntax
for the full set.
"order" is the name the template uses. Any public property of the object — and any computed
property, such as Total => Quantity * Price — is reachable from an expression.
Warning
A data source object must be of a visible type: a public class, not an anonymous type or a
private nested class. Anonymous types have no stable member names at runtime and are rejected.
The template did not change when the data moved from objects to a file. Since 26.11 a template
written for typed objects works over JSON, XML and CSV as well: members of dates and numbers that
may be missing, Booleans usable as conditions and in predicates, $id/$ref references, and
grouping over nested objects.
Other data sources follow the same shape — see
a database,
XML and
JSON.
The output format is a load-save option, not a different API. DOCX, PDF, ODT, HTML, Markdown and
plain text come out of the same call; the list is on
Supported document formats.
It replaces it. Classic mail merge fills MERGEFIELD placeholders from one flat row of data: one
record, a fixed set of fields, no logic. A GroupDocs.Assembly template adds <<foreach>> for
repeating sections, <<if>> for conditions, expressions such as Sum, GroupBy and OrderBy,
and nested objects — so an invoice with a variable number of lines and a conditional block is a
single template rather than a template plus code.
Frequently asked questions
How do you generate a Word document from a template in C#?
Put <<[...]>> tags in a .docx and call DocumentAssembler.AssembleDocument with the template,
an output path and a DataSourceInfo holding your data.
Is Microsoft Word or Office needed on the server?
No. GroupDocs.Assembly reads and writes the formats itself, on Windows and on Linux.
Can the data be JSON instead of a .NET object?
Yes. JsonDataSource takes a file, a stream or a string, and the template does not change.
Can the same template produce a PDF?
Yes — add new LoadSaveOptions(FileFormat.Pdf) to the call.
Which template formats are supported?
DOCX, DOC, DOCM, DOTX, RTF, ODT and plain text, plus spreadsheet, presentation and e-mail
templates with the same tag syntax.