Skip to main content

Add page numbers

Add page numbers

Page numbers help readers track their place in longer documents. Create two three-page PDFs with automatic page numbering. This example follows AddPageNumbersToPdf.cs, placing uppercase Roman numerals in a header and Arabic numerals in a footer.

Fluent recipe / 12Add page numbersCreate PDFs with Roman header numbers and Arabic footer numbers.

What you’ll create​

  • Three Letter-sized pages in each PDF, with 50-point margins.
  • Centered Roman header labels: I of III, II of III, and III of III.
  • Centered Arabic footer labels: 1 of 3, 2 of 3, and 3 of 3.
  • Page labels in 18-point Helvetica within a 24-point header or footer.
  • Two files in your chosen output directory: AddPageNumbersToPdf_roman_output.pdf and AddPageNumbersToPdf_arabic_output.pdf.

This example creates new PDFs. It does not add numbering to an existing PDF or restart numbering in sections.

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 creates both numbered documents and saves them to an existing output directory. No resource files are required.

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

namespace FluentExamples.Examples;

internal static class AddPageNumbersToPdf
{
public static void Run(string outputRoot)
{
Roman(outputRoot);
Arabic(outputRoot);
}

public static void Roman(string outputRoot)
{
var path = Path.Combine(
outputRoot, "AddPageNumbersToPdf_roman_output.pdf");
var style = PageNumberingStyle.RomanUpperCase;

DocumentBuilder.Create()
.Title("Roman page numbers")
.Pages(3, (page, index) => page
.Size(PageSize.Letter)
.Margins(50, LengthUnit.Point)
.Header(header => header
.Height(24, LengthUnit.Point)
.Center(
header.CurrentPage(style)
+ "\u00A0of\u00A0"
+ header.TotalPages(style),
text => text
.FontFamily("Helvetica")
.FontSize(18, LengthUnit.Point)))
.Text("Roman page numbering in the header."))
.Save(path);

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

public static void Arabic(string outputRoot)
{
var path = Path.Combine(
outputRoot, "AddPageNumbersToPdf_arabic_output.pdf");
var style = PageNumberingStyle.Numeric;

DocumentBuilder.Create()
.Title("Arabic page numbers")
.Pages(3, (page, index) => page
.Size(PageSize.Letter)
.Margins(50, LengthUnit.Point)
.Footer(footer => footer
.Height(24, LengthUnit.Point)
.Center(
footer.CurrentPage(style) + " of "
+ footer.TotalPages(style),
text => text
.FontFamily("Helvetica")
.FontSize(18, LengthUnit.Point)))
.Text("Arabic page numbering in the footer."))
.Save(path);

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

Friend Module AddPageNumbersToPdf
Public Sub Run(outputRoot As String)
Roman(outputRoot)
Arabic(outputRoot)
End Sub

Public Sub Roman(outputRoot As String)
Dim outputPath = Path.Combine(
outputRoot, "AddPageNumbersToPdf_roman_output.pdf")
Dim style = PageNumberingStyle.RomanUpperCase

DocumentBuilder.Create().
Title("Roman page numbers").
Pages(3, Sub(page, index) page.
Size(PageSize.Letter).
Margins(50, unit:=LengthUnit.Point).
Header(Sub(header) header.
Height(24, LengthUnit.Point).
Center(
header.CurrentPage(style) + ChrW(160) &
"of" & ChrW(160) &
header.TotalPages(style),
Sub(text) text.
FontFamily("Helvetica").
FontSize(18, LengthUnit.Point))).
Text("Roman page numbering in the header.")).
Save(outputPath)

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

Public Sub Arabic(outputRoot As String)
Dim outputPath = Path.Combine(
outputRoot, "AddPageNumbersToPdf_arabic_output.pdf")
Dim style = PageNumberingStyle.Numeric

DocumentBuilder.Create().
Title("Arabic page numbers").
Pages(3, Sub(page, index) page.
Size(PageSize.Letter).
Margins(50, unit:=LengthUnit.Point).
Footer(Sub(footer) footer.
Height(24, LengthUnit.Point).
Center(
footer.CurrentPage(style) + " of " &
footer.TotalPages(style),
Sub(text) text.
FontFamily("Helvetica").
FontSize(18, LengthUnit.Point))).
Text("Arabic page numbering in the footer.")).
Save(outputPath)

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

How it works​

1. Choose the output paths and numbering styles​

Run calls Roman and then Arabic. Each method uses Path.Combine to create a separate output path under outputRoot. Make sure that directory exists before calling Run.

PageNumberingStyle.RomanUpperCase formats numbers as uppercase Roman numerals. PageNumberingStyle.Numeric formats them as Arabic numerals.

2. Create three pages​

Each method starts with DocumentBuilder.Create() and sets the document title before adding pages. Pages(3, ...) configures three Letter-sized pages with 50-point margins. The callback applies the same layout to every page.

3. Add Roman page numbers to the header​

Header creates a 24-point header. header.CurrentPage(style) and header.TotalPages(style) supply automatic page-number tokens, joined with nonbreaking spaces around “of” to keep labels such as I of III on one line.

Center centers the label in the header, and its text callback selects 18-point Helvetica. Text adds the sentence “Roman page numbering in the header.” to each page’s body.

Footer creates a 24-point footer on each page of the second document. footer.CurrentPage(style) and footer.TotalPages(style) use the numeric style to produce 1 of 3, 2 of 3, and 3 of 3.

The footer labels use the same centered alignment and 18-point Helvetica. Each page’s body contains “Arabic page numbering in the footer.”

5. Save both PDFs​

Each .Save(path) writes its document directly through the Flow builder. The different filenames keep both outputs. Saving overwrites an existing file with the same name, and Console.WriteLine prints each output path.

Next steps​

Change the page count or choose another PageNumberingStyle to adjust the labels. Explore bookmarks and outlines to add navigation, or browse all examples.