To generate a Word document from a template in C#, put <<[...]>> tags in a .docx, then call DocumentAssembler.AssembleDocument with the template, an output path and your data — a .NET object, a DataTable or a JSON file. One call, no Microsoft Office on the server, and DOCX or PDF out of the same line.

Every application that sends invoices, confirmations or statements ends up with the same question: who owns the layout? If it lives in your code, the next wording change is a deployment. If it lives in a .docx that the people who write the wording can edit, it is an afternoon.

This is the short version of the second approach. The full sample is on GitHub — word-report-from-template-net.

What does a Word template look like?

A tag is <<[expression]>>, evaluated against a data source you name yourself:

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 doing real work there:

  • foreach repeats the text between its tags once per element.
  • LINQ in the expressions — Sum, Where, GroupBy, OrderBy. The total is part of the template, which means the person who decides what the total should include can change it without you.
  • A format after the bracket, not inside it: <<[order.Placed]:"dd MMMM yyyy">>. This catches everybody once.

The same tags work in a .docx, where they sit in paragraphs, table cells and headers of a document somebody styled in Word.

How do you fill a Word template from C#?

Order order = new Order
{
    Number = 4117,
    Customer = "Quayside Bistro",
    Placed = new DateTime(2026, 11, 3),
    Lines = new List<OrderLine>
    {
        new OrderLine { Product = "Espresso beans, 1 kg", Quantity = 6, Price = 18.40m },
        new OrderLine { Product = "Oat milk, 1 l", Quantity = 24, Price = 1.95m }
    }
};

new DocumentAssembler().AssembleDocument(
    "templates/order-confirmation.txt",
    "output/order-confirmation.txt",
    new DataSourceInfo(order, "order"));

and the output:

Order 4117 for Quayside Bistro
Placed on 03 November 2026

Espresso beans, 1 kg x6 - 110.40 EUR
Oat milk, 1 l x24 - 46.80 EUR

Total: 157.20 EUR

"order" is the name the template uses. Any public property is reachable, including a computed one such as Total => Quantity * Price — so the arithmetic can live in your domain model where it is tested, not in the document.

One rule to know up front: a data source object must be of a visible type — a public class. Anonymous types have no stable member names at runtime and are rejected.

Can the data come from JSON instead of an object?

new DocumentAssembler().AssembleDocument(
    "templates/Customer orders.docx",
    "output/customer-orders.docx",
    new DataSourceInfo(new JsonDataSource("data/Customers.json"), "customers"));

Look at what did not change: the template. Since 26.11 a template written against typed objects works over JSON, XML and CSV too — members of dates and numbers that may be missing in a file, Booleans usable as conditions and in predicates, $id/$ref references, grouping over nested objects.

That removes the usual second template. The batch job that has the data as objects and the web endpoint that receives it as JSON can now share one file.

How do you get a PDF out of the same template?

new DocumentAssembler().AssembleDocument(
    "templates/Customer orders.docx",
    "output/customer-orders.pdf",
    new LoadSaveOptions(FileFormat.Pdf),
    new DataSourceInfo(new JsonDataSource("data/Customers.json"), "customers"));

The output format is a load-save option, not a different API. DOCX, PDF, ODT, HTML, Markdown and plain text all come out of the same call, and no Microsoft Office or Adobe Acrobat is involved on the server.

Is this mail merge?

It replaces it, and does more than it. Classic mail merge fills MERGEFIELD placeholders from a flat row of data: one record, one set of fields, no logic. A GroupDocs.Assembly template has <<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 discount block is one template, not one template plus code.

If you are here because you need mail merge in C# without Microsoft Word installed, this is that, with the limits removed.

Questions people ask

What is the best way to generate Word documents from templates in C#? Keep the layout in a .docx that the people who own the wording can edit, keep the data in your code, and let a template engine put them together. Writing Open XML by hand couples the document’s appearance to your source code, and every change to the wording becomes a deployment.

Do I need Microsoft Word or Office on the server? No. GroupDocs.Assembly reads and writes the formats itself and runs on Windows and Linux.

Can I generate a PDF directly, without saving a DOCX first? Yes — new LoadSaveOptions(FileFormat.Pdf) on the same AssembleDocument call.

What template formats are supported? DOCX, DOC, DOCM, DOTX, RTF, ODT and plain text for word processing, plus spreadsheet, presentation and e-mail templates with the same tag syntax.

What happens if a field is missing from the data? By default the call throws. See Templates that survive incomplete data for the three ways to handle it.

Try it

git clone https://github.com/groupdocs-assembly/word-report-from-template-net.git
cd word-report-from-template-net
dotnet run --project src/WordReports -- all

The reference walk-through is in the documentation: Generate a Word report from a template.

From here, three directions people usually take: a workbook, a deck, or serving this from ASP.NET Core.