Use a Template
A PDF template lets you reuse branded artwork across documents that have different content. Use an existing PDF letterhead as a template and add a formatted letter over it. This example follows TemplateExample.cs and saves the result as TemplateExample_output.pdf.
What you’ll create
- A letter using the first page of
letterhead-template.pdf. - The current date, recipient John Doe, and the closing “Sincerely, Jane Doe.”
- HTML content styled with 14-point Helvetica, an Arial fallback, paragraph spacing, and bold names.
- Page margins that keep the letter clear of the letterhead artwork.
- A file named
TemplateExample_output.pdfin your chosen output directory.

Example code
Add the following class to a console project that references the DynamicPDF Flow API. See the quick start for an overview. The class adds a letter to a PDF template and saves the document to an existing output directory.
Ensure letterhead-template.pdf is in the Resources folder.
using System;
using System.Globalization;
using System.IO;
using DynamicPDF.Flow;
namespace FluentExamples.Examples;
internal static class TemplateExample
{
public static void Run(string outputRoot)
{
LetterWithTemplate(outputRoot);
}
private static void LetterWithTemplate(string outputRoot)
{
var template = Path.Combine(
AppContext.BaseDirectory,
"Resources", "letterhead-template.pdf");
var date = DateTime.Today.ToString(
"MMMM d, yyyy", CultureInfo.InvariantCulture);
var letter = $"""
<div style="font-family: Helvetica, Arial;
font-size: 14pt; line-height: 1.5;
color: #243247;">
<p style="margin: 0 0 24pt; color: #526174;">
{date}
</p>
<p style="margin: 0 0 20pt;">
<strong>John Doe</strong><br />
Dear John Doe,
</p>
<p style="margin: 0 0 16pt;">
Thank you for your interest in our services.
We look forward to working with you and
helping you achieve your goals.
</p>
<p style="margin: 0 0 24pt;">
Please contact us if you have any questions
or would like to discuss the next steps.
</p>
<p style="margin: 0;">
Sincerely,<br /><br />
<strong>Jane Doe</strong>
</p>
</div>
""";
var path = Path.Combine(
outputRoot, "TemplateExample_output.pdf");
DocumentBuilder.Create()
.Title("Letter using a letterhead template")
.Pages(page => page
.PdfTemplate(template)
.Margins(108, 54, 90, 54, LengthUnit.Point)
.Text(letter))
.Save(path);
Console.WriteLine(
$"Created {Path.GetFullPath(path)}");
}
}
Imports System
Imports System.Globalization
Imports System.IO
Imports DynamicPDF.Flow
Friend Module TemplateExample
Public Sub Run(outputRoot As String)
LetterWithTemplate(outputRoot)
End Sub
Private Sub LetterWithTemplate(outputRoot As String)
Dim template = Path.Combine(
AppContext.BaseDirectory,
"Resources", "letterhead-template.pdf")
Dim letterDate = DateTime.Today.ToString(
"MMMM d, yyyy", CultureInfo.InvariantCulture)
Dim letter = $"<div style=""font-family: Helvetica, Arial;
font-size: 14pt; line-height: 1.5;
color: #243247;"">
<p style=""margin: 0 0 24pt; color: #526174;"">
{letterDate}
</p>
<p style=""margin: 0 0 20pt;"">
<strong>John Doe</strong><br />
Dear John Doe,
</p>
<p style=""margin: 0 0 16pt;"">
Thank you for your interest in our services.
We look forward to working with you and
helping you achieve your goals.
</p>
<p style=""margin: 0 0 24pt;"">
Please contact us if you have any questions
or would like to discuss the next steps.
</p>
<p style=""margin: 0;"">
Sincerely,<br /><br />
<strong>Jane Doe</strong>
</p>
</div>"
Dim outputPath = Path.Combine(
outputRoot, "TemplateExample_output.pdf")
DocumentBuilder.Create().
Title("Letter using a letterhead template").
Pages(Sub(page) page.
PdfTemplate(template).
Margins(108, 54, 90, 54, LengthUnit.Point).
Text(letter)).
Save(outputPath)
Console.WriteLine(
$"Created {Path.GetFullPath(outputPath)}")
End Sub
End Module
How it works
1. Locate the template and prepare the date
Run calls LetterWithTemplate. The method locates letterhead-template.pdf in the Resources folder and formats today's date as a month name, day, and year using CultureInfo.InvariantCulture.
2. Write the letter as HTML
The interpolated raw string contains the letter's HTML, including the date. Paragraphs separate the recipient, message, and closing. Explicit <br /> tags break the recipient and greeting onto separate lines and leave space before Jane Doe's name.
The surrounding div sets 14-point Helvetica with Arial as a fallback, a line height of 1.5, and a dark text color. Paragraph margins control spacing, the date uses a muted color, and strong makes the names bold.
3. Apply the PDF template
DocumentBuilder.Create() starts the document, and .Title(...) sets its metadata title before adding pages. .PdfTemplate(template) uses the first source page by default and adopts its dimensions. The source page supplies the letterhead artwork beneath the added content.
4. Position and add the letter
.Margins(108, 54, 90, 54, LengthUnit.Point) sets the top, right, bottom, and left margins in points. The top and bottom margins reserve space for the letterhead artwork. .Text(letter) renders the HTML and its CSS inside the available content area.
5. Save the completed letter
.Save(...) writes TemplateExample_output.pdf under outputRoot. Make sure the output directory exists. Saving overwrites an existing file with the same name; the template is read from its separate resource path.
After saving, Console.WriteLine prints the full output path.
Next steps
Replace the letter text or use your own letterhead PDF. To select another source page, supply a one-based page number, such as .PdfTemplate(template, pageNumber: 2). Explore Styling text using CSS for more formatting examples, or browse all examples.