Skip to main content

Add headers and footers

Add headers and footers

Use page headers and footers to place text at the top and bottom of a PDF page. This example creates three pages with different headers and footers, then saves a separate PDF with a full-width header and a logo.

Fluent recipe / 03Add headers and footersAdd styled page headers and footers, including a full-width header with a logo.

What you’ll create​

  • AddHeadersAndFooters_output.pdf, a three-page PDF with A5, Letter, and Legal pages. Each page has a left-aligned header and a centered footer in the same color.
  • AddHeadersAndFooters_full_width_output.pdf, a Letter PDF with a light-blue header band, a logo on the left, centered header text, and a centered footer.
A5, Letter, and Legal pages with different colored headers and matching centered footers.

Example code​

Add the following class to a console project that references the DynamicPDF Flow API. See the quick start for an overview. Ensure DPDFLogo.png is in the Resources folder. The example saves both PDFs to an existing output directory.

using System;
using System.IO;
using DynamicPDF.Flow;
using DynamicPDF.Flow.Merger;

namespace FluentExamples.Examples;

internal static class AddHeadersAndFooters
{
public static void Run(string outputRoot)
{
SimpleHeaderFooter(outputRoot);
FullWidthHeaderFooter(outputRoot);
}

private static void SimpleHeaderFooter(string outputRoot)
{
var path = Path.Combine(
outputRoot, "AddHeadersAndFooters_output.pdf");

DocumentBuilder.Create()
.Pages(page => page
.Size(PageSize.A5)
.Background(background => background
.Color("#eff6ff"))
.Header(header => header
.Height(24, LengthUnit.Point)
.Left("MASTER SERVICE AGREEMENT",
text => text
.FontFamily("Helvetica")
.FontSize(18, LengthUnit.Point)
.Color("navy")))
.Footer(footer => footer
.Height(24, LengthUnit.Point)
.Center("Contract Administration",
text => text
.FontFamily("Helvetica")
.FontSize(12, LengthUnit.Point)
.Color("navy"))))
.Pages(page => page
.Size(PageSize.Letter)
.Background(background => background
.Color("#eef2ff"))
.Header(header => header
.Height(24, LengthUnit.Point)
.Left("PURCHASE ORDER FORM",
text => text
.FontFamily("Helvetica")
.FontSize(18, LengthUnit.Point)
.Color("darkgreen")))
.Footer(footer => footer
.Height(24, LengthUnit.Point)
.Center("Purchasing Department",
text => text
.FontFamily("Helvetica")
.FontSize(12, LengthUnit.Point)
.Color("darkgreen"))))
.Pages(page => page
.Size(PageSize.Legal)
.Background(background => background
.Color("#f5f3ff"))
.Header(header => header
.Height(24, LengthUnit.Point)
.Left("LEGAL ADDENDUM",
text => text
.FontFamily("Helvetica")
.FontSize(18, LengthUnit.Point)
.Color("maroon")))
.Footer(footer => footer
.Height(24, LengthUnit.Point)
.Center("Legal Department",
text => text
.FontFamily("Helvetica")
.FontSize(12, LengthUnit.Point)
.Color("maroon"))))
.Save(path);

Console.WriteLine(
$"Created {Path.GetFullPath(path)}");
}

private static void FullWidthHeaderFooter(string outputRoot)
{
var resources = Path.Combine(
AppContext.BaseDirectory, "Resources");
var logo = Path.Combine(resources, "DPDFLogo.png");


var background = PdfSource.FromFluent(
DocumentBuilder.Create()
.Pages(page => page
.Size(PageSize.Letter)
.Margins(0, LengthUnit.Point)
.Rectangle(rectangle => rectangle
.Width(612, LengthUnit.Point)
.Height(0.5, LengthUnit.Inch)
.BackgroundColor("lightblue"))));

var path = Path.Combine(
outputRoot, "AddHeadersAndFooters_" +
"full_width_output.pdf");

DocumentBuilder.Create()
.Pages(page => page
.Size(PageSize.Letter)
.Margins(6, 0, 6, 0, LengthUnit.Point)
.PdfPage(background, 1, pdf => pdf.Natural())
.Header(header => header
.Height(0.5, LengthUnit.Inch)
.VerticalAlign(VerticalAlign.Top)
.Left(left => left.Image(logo, image => image
.Width(22 * 262.0 / 250,
LengthUnit.Point)
.Height(22, LengthUnit.Point)
.Margin(0, 0, 0, 12, LengthUnit.Point)))
.Center("Example Header", text => text
.FontFamily("Helvetica", "Arial")
.FontSize(22, LengthUnit.Point)
.Color("red")))
.Footer(footer => footer
.Height(36, LengthUnit.Point)
.Center("<i>This is an example footer.<i>",
text => text
.FontFamily("Helvetica")
.FontWeight(FontWeight.Bold)
.FontSize(12, LengthUnit.Point))))
.Save(path);

Console.WriteLine(
$"Created {Path.GetFullPath(path)}");
}
}
Imports System
Imports System.IO
Imports DynamicPDF.Flow
Imports DynamicPDF.Flow.Merger

Friend Module AddHeadersAndFooters
Public Sub Run(outputRoot As String)
SimpleHeaderFooter(outputRoot)
FullWidthHeaderFooter(outputRoot)
End Sub

Private Sub SimpleHeaderFooter(outputRoot As String)
Dim outputPath = Path.Combine(
outputRoot, "AddHeadersAndFooters_output.pdf")

DocumentBuilder.Create().
Pages(Sub(page) page.
Size(PageSize.A5).
Background(Sub(background) background.
Color("#eff6ff")).
Header(Sub(header) header.
Height(24, LengthUnit.Point).
Left("MASTER SERVICE AGREEMENT",
Sub(text) text.
FontFamily("Helvetica").
FontSize(18, LengthUnit.Point).
Color("navy"))).
Footer(Sub(footer) footer.
Height(24, LengthUnit.Point).
Center("Contract Administration", Sub(text) text.
FontFamily("Helvetica").
FontSize(12, LengthUnit.Point).
Color("navy")))).
Pages(Sub(page) page.
Size(PageSize.Letter).
Background(Sub(background) background.
Color("#eef2ff")).
Header(Sub(header) header.
Height(24, LengthUnit.Point).
Left("PURCHASE ORDER FORM",
Sub(text) text.
FontFamily("Helvetica").
FontSize(18, LengthUnit.Point).
Color("darkgreen"))).
Footer(Sub(footer) footer.
Height(24, LengthUnit.Point).
Center("Purchasing Department", Sub(text) text.
FontFamily("Helvetica").
FontSize(12, LengthUnit.Point).
Color("darkgreen")))).
Pages(Sub(page) page.
Size(PageSize.Legal).
Background(Sub(background) background.
Color("#f5f3ff")).
Header(Sub(header) header.
Height(24, LengthUnit.Point).
Left("LEGAL ADDENDUM",
Sub(text) text.
FontFamily("Helvetica").
FontSize(18, LengthUnit.Point).
Color("maroon"))).
Footer(Sub(footer) footer.
Height(24, LengthUnit.Point).
Center("Legal Department", Sub(text) text.
FontFamily("Helvetica").
FontSize(12, LengthUnit.Point).
Color("maroon")))).
Save(outputPath)

Console.WriteLine(
$"Created {Path.GetFullPath(outputPath)}")
End Sub

Private Sub FullWidthHeaderFooter(outputRoot As String)
Dim resources = Path.Combine(
AppContext.BaseDirectory, "Resources")
Dim logo = Path.Combine(resources, "DPDFLogo.png")


Dim background = PdfSource.FromFluent(
DocumentBuilder.Create().
Pages(Sub(page) page.
Size(PageSize.Letter).
Margins(0, unit:=LengthUnit.Point).
Rectangle(Sub(rectangle) rectangle.
Width(612, LengthUnit.Point).
Height(0.5, LengthUnit.Inch).
BackgroundColor("lightblue"))))

Dim outputPath = Path.Combine(
outputRoot, "AddHeadersAndFooters_" +
"full_width_output.pdf")

DocumentBuilder.Create().
Pages(Sub(page) page.
Size(PageSize.Letter).
Margins(6, 0, 6, 0, LengthUnit.Point).
PdfPage(background, 1, Sub(pdf) pdf.Natural()).
Header(Sub(header) header.
Height(0.5, LengthUnit.Inch).
VerticalAlign(DynamicPDF.Flow.VerticalAlign.Top).
Left(Sub(left) left.Image(logo, Sub(image) image.
Width(22 * 262.0 / 250,
LengthUnit.Point).
Height(22, LengthUnit.Point).
Margin(0, 0, 0, 12, LengthUnit.Point))).
Center("Example Header", Sub(text) text.
FontFamily("Helvetica", "Arial").
FontSize(22, LengthUnit.Point).
Color("red"))).
Footer(Sub(footer) footer.
Height(36, LengthUnit.Point).
Center("<i>This is an example footer.<i>",
Sub(text) text.
FontFamily("Helvetica").
FontWeight(FontWeight.Bold).
FontSize(12, LengthUnit.Point)))).
Save(outputPath)

Console.WriteLine(
$"Created {Path.GetFullPath(outputPath)}")
End Sub
End Module

How it works​

1. Set up the three pages​

Run calls SimpleHeaderFooter first. Each .Pages(...) call sets a page size and a background color. .Header(...) and .Footer(...) each set aside a 24-point area for their text. .Left(...) places the header title on the left, and .Center(...) centers the footer. Each footer uses the same text color as its header.

2. Build the full-width header background​

FullWidthHeaderFooter starts with a separate Letter page with zero margins and a light-blue rectangle 612 points wide and half an inch high. PdfSource.FromFluent(...) turns that page into a PDF source. The output page uses .PdfPage(...) with .Natural() to place the background at its original size.

.VerticalAlign(VerticalAlign.Top) aligns the full-width header contents at the top of the header area. The left slot contains DPDFLogo.png, and the center slot contains the red title. A separate 36-point footer area contains the centered footer text.

4. Save both PDFs​

Each .Save(path) writes its PDF to outputRoot and overwrites an existing file with the same name. The example prints both paths.

Next steps​

See Add page numbers for page numbering in headers and footers, or Add an image for placing images in page content.