Skip to Content
NexPDF

Templates

Generate PDFs from Word, HTML and PDF templates filled with JSON data, with loops, conditions, formatters and images, with NexPDF for .NET.

English

License module

Templates need the conversion module (Startup tier and above).

A template is a Word document, an HTML page or a PDF with placeholders such as {{customer.name}}. NexPDF fills it from a JSON object and gives you a PDF (or, for Word, a filled .docx). The three kinds share one syntax.

Word templates

NexPdfSdk.TemplateDocxToPdf(templateBytes, dataJson, optionsJson) fills a Word template and converts it to a new document; NexPdfSdk.TemplateDocxFill(templateBytes, dataJson) returns the filled .docx instead.

using System.Text.Json;
using Nexdevit.NexPdf;

var data = JsonSerializer.Serialize(new
{
    number = "2026-114",
    customer = new { name = "Ana Silva" },
    due = "2026-12-31",
    items = new[]
    {
        new { description = "Consulting", qty = 4, price = 120.0 },
        new { description = "Travel", qty = 1, price = 85.5 },
    },
    total = 565.5,
    paid = false,
});

var template = File.ReadAllBytes("invoice.docx");

using (var doc = NexPdfSdk.TemplateDocxToPdf(template, data, """{"locale":"pt-PT"}"""))
    doc.Save("invoice-2026-114.pdf");

var filledDocx = NexPdfSdk.TemplateDocxFill(template, data);   // keep it as Word
File.WriteAllBytes("invoice-2026-114.docx", filledDocx);

NexPdfSdk.TemplateDocxFillWith(templateBytes, dataJson, optionsJson) returns the filled .docx with the strict and locale options.

Placeholders work even when Word splits them over several runs. A table row that holds an {{#each items}} tag is repeated once per item, and a paragraph that holds only a block tag disappears from the output. Headers, footers, footnotes and endnotes are filled too.

HTML templates

var html = """
    <h1>Invoice {{number}}</h1>
    <p>{{customer.name}}, due {{due | date:"dd/MM/yyyy"}}</p>
    <table>
      {{#each items}}<tr><td>{{@number}}</td><td>{{description}}</td><td>{{price | currency:"EUR"}}</td></tr>{{/each}}
    </table>
    {{#if paid}}<p>Paid</p>{{else}}<p>Total due: {{total | currency:"EUR"}}</p>{{/if}}
    """;

using var doc = NexPdfSdk.TemplateHtmlToPdf(html, data);
doc.Save("invoice.pdf");

Values are HTML-escaped; {{{raw}}} (three braces) inserts HTML as it is.

PDF templates

TemplateFillPdf(dataJson, optionsJson) fills an existing PDF in place: AcroForm fields by name (dotted names such as customer.name follow the data tree) and placeholders in the page's text lines. It returns the number of fields and text lines filled.

using var doc = NexPdfDocument.Open("form-template.pdf");
var filled = doc.TemplateFillPdf(data, """{"flatten":true}""");
doc.Save("filled.pdf");

Rows that do not fit

When filled text no longer fits, the rows below it break onto new pages. Text rows and graphics at the top and bottom edges of the page can repeat in place on every page, as a header and a footer:

OptionDefaultMeaning
header_height0 (no header)Height in points, from the top edge, of the rows that repeat on every page
footer_height72Height in points, from the bottom edge, of the rows that repeat on every page

By default, a text row that starts in the bottom 72 pt (one inch) of the page is treated as a footer: it is repeated in place on every page instead of flowing with the content. Set "footer_height":0 to turn the footer zone off; no row is then a footer, and flowing rows may run to the bottom edge. A small value, such as 36, keeps a margin without catching your rows.

doc.TemplateFillPdf(data, """{"header_height":90,"footer_height":0}""");

Without footer_height the flow keeps a bottom margin of 36 to 72 pt. A row taller than a page is cut between lines, and graphics after the last row move with the end of the content (onto a page of their own when they do not fit).

Fonts

Characters the page font cannot show are drawn in a fallback font, shaped with OpenType and in bidi order. The font option chooses it: "bundled" (the default), "keep" (the page font only), a path to a .ttf file, {"path": "..."} or {"data": "<base64>"}. Right-to-left text uses the fallback font even when the page font has codes for it, unless font is "keep".

Syntax

TagMeaning
{{customer.name}}, {{items.0.price}}A value from the data
{{total | currency:"EUR"}}A value through a formatter
{{#each items}}…{{/each}}Repeats for each item; inside, this, @index, @number, @first, @last, @key, ../name and @root.name
{{#if paid}}…{{else}}…{{/if}}Condition; comparisons such as {{#if total > 1000}} work
{{#unless paid}}…{{/unless}}Inverse condition
{{% logo}}An image from the data
{{! note}}A comment, not printed

Formatters: upper, lower, capitalize, trim, currency:"EUR" (optionally with a locale, currency:"EUR","de"), number[:decimals[,"locale"]], percent[:decimals], round[:decimals], date:"yyyy-MM-dd" (ISO dates or Unix seconds), default:"text", join[:", "] and length.

An image value is base64 text or a data: URL of a PNG, JPEG or GIF, or an object {"data": "...", "width": 120, "height": 40} with a size in points:

using System.Text.Json.Nodes;

var root = JsonNode.Parse(data)!;
root["logo"] = new JsonObject
{
    ["data"] = Convert.ToBase64String(File.ReadAllBytes("logo.png")),
    ["width"] = 120,
};
var withLogo = root.ToJsonString();

Options

OptionDefaultMeaning
strictfalseA field missing from the data fails the call, naming the field and where the tag is. Otherwise it renders empty
localeenDefault locale of currency and number (en, de, fr, pt-BR, …)
flattenfalsePDF templates: flatten the form after filling
fieldstruePDF templates: fill AcroForm fields
texttruePDF templates: replace placeholders in the page text
header_height, footer_height0, 72PDF templates: the repeated header and footer zones (see above)
fontbundledPDF templates: the fallback font (see above)

Syntax errors (an unclosed block, an unknown formatter) always fail. The error is a NexPdfException with the engine's message:

try
{
    using var doc = NexPdfSdk.TemplateHtmlToPdf("<p>{{missing}}</p>", "{}", """{"strict":true}""");
}
catch (NexPdfException e)
{
    Console.WriteLine(e.Message);   // names the field and the tag location
}

On this page