How to Add Page Numbers in a PDF

Page numbers are sequential numbers assigned to each individual page within a PDF document. Page numbers are one of the most important components in a document for navigation. They help readers locate specific pages and determine what page they are reading from. Additionally, page numbers enable easy citation and referencing of content within the document. With IronPDF, adding page numbers to your PDF is a simple process.

Start numbering your PDF in one line!

new IronPdf.ChromePdfRenderer { RenderingOptions = { HtmlFooter = new IronPdf.HtmlHeaderFooter { HtmlFragment = "<center>{page} of {total‑pages}</center>", MaxHeight = 15, DrawDividerLine = true } } }.RenderHtmlAsPdf("<h1>Page‑1</h1><div style='page-break-after: always;'></div><h1>Page‑2</h1>").SaveAs("numbered.pdf");
Install with NuGet
green arrow pointer

PM >  Install-Package IronPdf

Get started with IronPDF

Start using IronPDF in your project today with a free trial.

First Step:
green arrow pointer



Add Page Numbers Example

Using placeholder strings {page} and {total-pages} with either the TextHeaderFooter or the HtmlHeaderFooter class, you can add the current page number and the total number of pages.

:path=/static-assets/pdf/content-code-examples/how-to/page-numbers-basic.cs
using IronPdf;

// Create text header
TextHeaderFooter textHeader = new TextHeaderFooter()
{
    CenterText = "{page} of {total-pages}"
};

// Create html footer
HtmlHeaderFooter htmlFooter = new HtmlHeaderFooter()
{
    HtmlFragment = "<center><i>{page} of {total-pages}<i></center>"
};

// Render a new PDF
ChromePdfRenderer renderer = new ChromePdfRenderer();
PdfDocument pdf = renderer.RenderHtmlAsPdf("<h1>Hello World!</h1>");

// Add header and footer
pdf.AddTextHeaders(textHeader);
pdf.AddHtmlFooters(htmlFooter);

pdf.SaveAs("pdfWithPageNumber.pdf");
Imports IronPdf

' Create text header
Private textHeader As New TextHeaderFooter() With {.CenterText = "{page} of {total-pages}"}

' Create html footer
Private htmlFooter As New HtmlHeaderFooter() With {.HtmlFragment = "<center><i>{page} of {total-pages}<i></center>"}

' Render a new PDF
Private renderer As New ChromePdfRenderer()
Private pdf As PdfDocument = renderer.RenderHtmlAsPdf("<h1>Hello World!</h1>")

' Add header and footer
pdf.AddTextHeaders(textHeader)
pdf.AddHtmlFooters(htmlFooter)

pdf.SaveAs("pdfWithPageNumber.pdf")
$vbLabelText   $csharpLabel

The output PDF from the code above is shown below:

You can also directly add the headers and footers with the page number placeholder strings into the rendering options of the ChromePdfRenderer.

:path=/static-assets/pdf/content-code-examples/how-to/page-numbers-chromerenderer.cs
using IronPdf;

// Add header and footer to rendering options
ChromePdfRenderer renderer = new ChromePdfRenderer();
renderer.RenderingOptions.TextHeader = new TextHeaderFooter()
{
    CenterText = "{page} of {total-pages}"
};
renderer.RenderingOptions.HtmlFooter = new HtmlHeaderFooter()
{
    HtmlFragment = "<center><i>{page} of {total-pages}<i></center>"
};

string html = @"
    <h1>Hello World!</h1>
<div style='page-break-after: always;'/>
    <h1>2nd Page!</h1>";

// Render new PDF
PdfDocument pdf = renderer.RenderHtmlAsPdf(html);

pdf.SaveAs("applyPageNumberWithRenderingOptions.pdf");
Imports IronPdf

' Add header and footer to rendering options
Private renderer As New ChromePdfRenderer()
renderer.RenderingOptions.TextHeader = New TextHeaderFooter() With {.CenterText = "{page} of {total-pages}"}
renderer.RenderingOptions.HtmlFooter = New HtmlHeaderFooter() With {.HtmlFragment = "<center><i>{page} of {total-pages}<i></center>"}

Dim html As String = "
    <h1>Hello World!</h1>
<div style='page-break-after: always;'/>
    <h1>2nd Page!</h1>"

' Render new PDF
Dim pdf As PdfDocument = renderer.RenderHtmlAsPdf(html)

pdf.SaveAs("applyPageNumberWithRenderingOptions.pdf")
$vbLabelText   $csharpLabel

Add Page Numbers to Specific Pages Example

With IronPDF, you can decide where to add page numbers. You can make them start on a certain page or on specific groups of pages such as on the even-indexed pages.

Let's prepare our PDF document to apply the page numbers.

:path=/static-assets/pdf/content-code-examples/how-to/page-numbers-preparation.cs
using IronPdf;
using System.Linq;
using System.Collections.Generic;

string multi_page_html = @"
    <p>This is the 1st Page</p>
<div style = 'page-break-after: always;' ></div>
    <p>This is the 2nd Page</p>
<div style = 'page-break-after: always;' ></div>
    <p>This is the 3rd Page</p>
<div style = 'page-break-after: always;' ></div>
    <p>This is the 4th Page</p>
<div style = 'page-break-after: always;' ></div>
    <p>This is the 5th Page</p>
<div style = 'page-break-after: always;' ></div>
    <p>This is the 6th Page</p>
<div style = 'page-break-after: always;' ></div>
    <p>This is the 7th Page</p>";

// Create header
HtmlHeaderFooter header = new HtmlHeaderFooter()
{
    HtmlFragment = "<center><i>{page} of {total-pages}<i></center>"
};

// Render PDF
ChromePdfRenderer renderer = new ChromePdfRenderer();
PdfDocument pdf = renderer.RenderHtmlAsPdf(multi_page_html);

// Create a Page Range 0..6
var allPageIndices = Enumerable.Range(0, pdf.PageCount);
Imports IronPdf
Imports System.Linq
Imports System.Collections.Generic

Private multi_page_html As String = "
    <p>This is the 1st Page</p>
<div style = 'page-break-after: always;' ></div>
    <p>This is the 2nd Page</p>
<div style = 'page-break-after: always;' ></div>
    <p>This is the 3rd Page</p>
<div style = 'page-break-after: always;' ></div>
    <p>This is the 4th Page</p>
<div style = 'page-break-after: always;' ></div>
    <p>This is the 5th Page</p>
<div style = 'page-break-after: always;' ></div>
    <p>This is the 6th Page</p>
<div style = 'page-break-after: always;' ></div>
    <p>This is the 7th Page</p>"

' Create header
Private header As New HtmlHeaderFooter() With {.HtmlFragment = "<center><i>{page} of {total-pages}<i></center>"}

' Render PDF
Private renderer As New ChromePdfRenderer()
Private pdf As PdfDocument = renderer.RenderHtmlAsPdf(multi_page_html)

' Create a Page Range 0..6
Private allPageIndices = Enumerable.Range(0, pdf.PageCount)
$vbLabelText   $csharpLabel

Even Pages Indexes

Building upon the previous code example, the following code will apply page numbers exclusively to even page indexes. Since we are filtering for even page indexes, the resulting PDF will apply numbers on only odd page numbers. Page indexes begin at zero, while page numbers start from one.

:path=/static-assets/pdf/content-code-examples/how-to/page-numbers-even-pages.cs
// Get even page indexes (resulting in odd page numbers)
var evenPageIndices = allPageIndices.Where(i => i % 2 == 0);

pdf.AddHtmlHeaders(header, 1, evenPageIndices);
pdf.SaveAs("EvenPages.pdf");
' Get even page indexes (resulting in odd page numbers)
Dim evenPageIndices = allPageIndices.Where(Function(i) i Mod 2 = 0)

pdf.AddHtmlHeaders(header, 1, evenPageIndices)
pdf.SaveAs("EvenPages.pdf")
$vbLabelText   $csharpLabel

Odd Pages Indexes

Add page numbers specifically to pages with odd index numbers.

:path=/static-assets/pdf/content-code-examples/how-to/page-numbers-odd-pages.cs
// Get odd page indexes (resulting in even page numbers)
var oddPageIndexes = allPageIndices.Where(i => i % 2 != 0);

pdf.AddHtmlHeaders(header, 1, oddPageIndexes);
pdf.SaveAs("OddPages.pdf");
' Get odd page indexes (resulting in even page numbers)
Dim oddPageIndexes = allPageIndices.Where(Function(i) i Mod 2 <> 0)

pdf.AddHtmlHeaders(header, 1, oddPageIndexes)
pdf.SaveAs("OddPages.pdf")
$vbLabelText   $csharpLabel

Last Page Only

Add a page number to the last page only.

:path=/static-assets/pdf/content-code-examples/how-to/page-numbers-last-page-only.cs
// Last page only
var lastPageIndex = new List<int>() { pdf.PageCount - 1 };

pdf.AddHtmlHeaders(header, 1, lastPageIndex);
pdf.SaveAs("LastPageOnly.pdf");
' Last page only
Dim lastPageIndex = New List(Of Integer)() From {pdf.PageCount - 1}

pdf.AddHtmlHeaders(header, 1, lastPageIndex)
pdf.SaveAs("LastPageOnly.pdf")
$vbLabelText   $csharpLabel

First Page Only

Add a page number to the first page only.

:path=/static-assets/pdf/content-code-examples/how-to/page-numbers-first-page-only.cs
// First page only
var firstPageIndex = new List<int>() { 0 };

pdf.AddHtmlHeaders(header, 1, firstPageIndex);
pdf.SaveAs("FirstPageOnly.pdf");
' First page only
Dim firstPageIndex = New List(Of Integer)() From {0}

pdf.AddHtmlHeaders(header, 1, firstPageIndex)
pdf.SaveAs("FirstPageOnly.pdf")
$vbLabelText   $csharpLabel

Skip First Page

Skip the first page when applying the header.

:path=/static-assets/pdf/content-code-examples/how-to/page-numbers-skip-first-page.cs
// Skip the first page
var skipFirstPage = allPageIndices.Skip(1);

pdf.AddHtmlHeaders(header, 1, skipFirstPage);
pdf.SaveAs("SkipFirstPage.pdf");
' Skip the first page
Dim skipFirstPage = allPageIndices.Skip(1)

pdf.AddHtmlHeaders(header, 1, skipFirstPage)
pdf.SaveAs("SkipFirstPage.pdf")
$vbLabelText   $csharpLabel

Skip First Page And Don't Count it

Skip the first page and begin numbering from the second page, considering it as page 1.

:path=/static-assets/pdf/content-code-examples/how-to/page-numbers-skip-first-page-and-dont-count-it.cs
// Skip the first page and start numbering the second page as page 1
var skipFirstPageAndDontCountIt = allPageIndices.Skip(1);

pdf.AddHtmlHeaders(header, 0, skipFirstPageAndDontCountIt);
pdf.SaveAs("SkipFirstPageAndDontCountIt.pdf");
' Skip the first page and start numbering the second page as page 1
Dim skipFirstPageAndDontCountIt = allPageIndices.Skip(1)

pdf.AddHtmlHeaders(header, 0, skipFirstPageAndDontCountIt)
pdf.SaveAs("SkipFirstPageAndDontCountIt.pdf")
$vbLabelText   $csharpLabel

To explore all metadata options, please visit the IronPDF Headers and Footers Guide.

Frequently Asked Questions

What are page numbers in a PDF?

Page numbers are sequential numbers assigned to each individual page within a PDF document that help readers navigate, cite, and reference content within the document.

How do I add page numbers to a PDF?

To add page numbers, you can use a library like IronPDF. Download the C# PDF Library from NuGet, load or create a PDF, use placeholder strings {page} and {total-pages} in the header or footer, and save the resulting PDF.

Can I add page numbers to specific pages?

Yes, using IronPDF, you can add page numbers to specific pages or sections by applying placeholders in headers or footers to selected pages.

How can I apply page numbers only to even-indexed pages?

You can apply page numbers to even-indexed pages by using IronPDF to filter for even indexes and setting the footer with placeholders, then saving the PDF.

Is it possible to add page numbers to only the last page?

Yes, you can add page numbers to the last page by using IronPDF to set the footer with placeholders on the last page only.

Can I skip numbering the first page of a PDF?

Yes, you can skip numbering the first page by using IronPDF to start applying headers or footers from the second page onward.

How do I skip the first page and renumber starting from the second page?

To skip the first page and start numbering from the second page as page 1, you can use IronPDF to iterate over the pages starting from the second and increment the page number manually.

What is the benefit of using placeholder strings like {page} and {total-pages}?

Placeholder strings like {page} and {total-pages} allow dynamic insertion of the current page number and total number of pages, facilitating automatic updates when the document structure changes. IronPDF makes use of these placeholders to easily manage page numbering.

Jordi Bardia
Software Engineer
Jordi is most proficient in Python, C# and C++, when he isn’t leveraging his skills at Iron Software; he’s game programming. Sharing responsibilities for product testing, product development and research, Jordi adds immense value to continual product improvement. The varied experience keeps him challenged and engaged, and he says it’s one of his favorite aspects of working with Iron Software. Jordi grew up in Miami, Florida and studied Computer Science and Statistics at University of Florida.
Reviewed by
Jeff Fritz
Jeffrey T. Fritz
Principal Program Manager - .NET Community Team
Jeff is also a Principal Program Manager for the .NET and Visual Studio teams. He is the executive producer of the .NET Conf virtual conference series and hosts 'Fritz and Friends' a live stream for developers that airs twice weekly where he talks tech and writes code together with viewers. Jeff writes workshops, presentations, and plans content for the largest Microsoft developer events including Microsoft Build, Microsoft Ignite, .NET Conf, and the Microsoft MVP Summit