Skip to Content
NexPDF

Templates

Generate PDFs from Word, HTML and PDF templates filled with JSON data in a .NET MAUI app, and show them in the PdfView.

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, described in full in the .NET templates guide. Filling and converting runs on the device, with no network.

Word templates

Ship the template in Resources/Raw, fill it on a background thread and show the result:

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

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

byte[] template;
await using (var s = await FileSystem.OpenAppPackageFileAsync("invoice.docx"))
using (var ms = new MemoryStream())
{
    await s.CopyToAsync(ms);
    template = ms.ToArray();
}

var pdf = await Task.Run(() =>
{
    using var doc = NexPdfSdk.TemplateDocxToPdf(template, data, """{"locale":"pt-PT"}""");
    return doc.SaveBytes();
});
await view.LoadAsync(pdf);

NexPdfSdk.TemplateDocxFill(template, data) returns the filled .docx instead, for example to share it with Share.Default. Placeholders work even when Word splits them over several runs; a table row with an {{#each items}} tag is repeated once per item. NexPdfSdk.TemplateDocxFillWith(template, data, optionsJson) returns the filled .docx with the strict and locale options.

HTML templates

var html = """
    <h1>Invoice {{number}}</h1>
    <p>{{customer.name}}</p>
    <table>{{#each items}}<tr><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}}
    """;

var pdf = await Task.Run(() =>
{
    using var doc = NexPdfSdk.TemplateHtmlToPdf(html, data);
    return doc.SaveBytes();
});
await view.LoadAsync(pdf);

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

PDF templates

TemplateFillPdf(dataJson, optionsJson) fills the open document 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.

var filled = view.Document!.TemplateFillPdf(data, """{"flatten":true}""");
await view.ReloadAsync();

When filled text no longer fits, the rows below break onto new pages. header_height and footer_height (in points) mark the rows at the top and bottom edges that repeat in place on every page. By default there is no header and a 72 pt footer: a text row that starts in the bottom 72 pt (one inch) of the page is treated as a footer and repeated on every page instead of flowing with the content. "footer_height":0 turns the footer zone off, so flowing rows may run to the bottom edge; a small value such as 36 keeps a margin without catching rows.

Characters the page font cannot show use a fallback font, shaped and in bidi order; the font option chooses it ("bundled", the default, "keep", a .ttf path, {"path"} or {"data":"<base64>"}). See the .NET templates guide.

Syntax

Tags: {{customer.name}}, {{total | currency:"EUR"}}, {{#each items}}…{{/each}}, {{#if paid}}…{{else}}…{{/if}}, {{#unless paid}}…{{/unless}}, {{% logo}} for an image and {{! note}} for a comment. Formatters include upper, lower, currency, number, percent, round, date, default, 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}. The full table is in the .NET templates guide.

Options

strict (a field missing from the data fails the call), locale (default en), and for PDF templates flatten, fields, text, header_height, footer_height (default 72) and font. Syntax errors always fail with a NexPdfException that names the problem and where the tag is:

try
{
    using var doc = NexPdfSdk.TemplateHtmlToPdf("<p>{{missing}}</p>", "{}", """{"strict":true}""");
}
catch (NexPdfException e)
{
    await DisplayAlert("Template", e.Message, "OK");
}

On this page