How to Add Page Numbers to PDFs Using C#

How to Add Page Numbers to a PDF Using C#

IronPDF enables you to add page numbers to PDFs in C# using placeholder strings {page} and {total-pages} in headers/footers, with options to apply them to specific pages or sections for enhanced document navigation.

Page numbers are sequential numbers assigned to each page within a PDF document. They are essential components for navigation, helping readers locate specific pages and track their position. Page numbers also enable easy citation and referencing of content. With IronPDF, adding page numbers to your PDF is straightforward.

Quickstart: Add Page Numbers to PDFs with Ease

Add page numbers to PDF documents using IronPDF. With minimal C# code, you can insert dynamic page numbers into headers or footers, customize their placement, and specify which pages they appear on. Whether handling new PDFs or existing ones, IronPDF provides a flexible solution to enhance document navigation and organization.

Nuget IconGet started making PDFs with NuGet now:

  1. Install IronPDF with NuGet Package Manager

    PM > Install-Package IronPdf

  2. Copy and run this code snippet.

    new IronPdf.ChromePdfRenderer { RenderingOptions = { HtmlFooter = new IronPdf.HtmlHeaderFooter { HtmlFragment = "<center>{page}of{total-pages}</center>", DrawDividerLine = true } } }
         .RenderHtmlAsPdf("<h1>My multi-page document</h1><div style='page-break-after:always;'></div><h1>Page 2</h1>")
         .SaveAs("numbered-pages.pdf");
  3. Deploy to test on your live environment

    Start using IronPDF in your project today with a free trial
    arrow pointer


How Do I Add Basic Page Numbers to a PDF?

Using placeholder strings {page} and {total-pages} with either the TextHeaderFooter or the HtmlHeaderFooter class, you can add the current page number and total page count. This functionality is essential when creating new PDFs or converting HTML to PDF documents that require professional formatting.

: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");
$vbLabelText   $csharpLabel

The output PDF from the code above is shown below:

You can also add headers and footers with page number placeholder strings directly to the rendering options of the ChromePdfRenderer. This approach is useful when working with rendering options to customize your PDF output.

: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");
$vbLabelText   $csharpLabel

When working with complex documents, consider page breaks and custom margins to ensure page numbers display correctly.

How Can I Add Page Numbers to Specific Pages Only?

With IronPDF, you can control where page numbers appear. Make them start on a specific page or apply them to specific groups like even-indexed pages. This flexibility is valuable when generating PDF reports or working with documents containing different sections.

First, prepare the PDF document to apply 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);
$vbLabelText   $csharpLabel

How Do I Number Even Pages Only?

Building on the previous example, this code applies page numbers exclusively to even page indexes. Since filtering is for even page indexes, the resulting PDF shows numbers on odd page numbers. Page indexes begin at zero, while page numbers start from one. This technique suits documents following traditional book formatting or when working with merged PDF documents.

: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");
$vbLabelText   $csharpLabel

How Do I Number Odd Pages Only?

Add page numbers to pages with odd index numbers. This approach is common in double-sided printing scenarios where page numbers should appear on only one side of the printed document.

: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");
$vbLabelText   $csharpLabel

How Do I Add a Page Number to the Last Page Only?

Add a page number to the last page only. This is useful for summary pages or when indicating the document's end.

: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");
$vbLabelText   $csharpLabel

How Do I Add a Page Number to the First Page Only?

Add a page number to the first page only. This works well for cover pages or documents where only the first page needs identification.

: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");
$vbLabelText   $csharpLabel

How Do I Skip the First Page When Adding Page Numbers?

Skip the first page when applying the header. This common requirement applies to documents with cover or title pages that shouldn't display page numbers. When working with PDF forms or documents including a cover letter, this technique ensures professional formatting.

: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");
$vbLabelText   $csharpLabel

How Do I Skip the First Page and Start Numbering from 1 on the Second Page?

Skip the first page and begin numbering from the second page, considering it as page 1. This approach suits documents where the cover page should not count in the page numbering sequence, such as academic papers or formal reports.

: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");
$vbLabelText   $csharpLabel

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

Advanced Page Numbering Techniques

Beyond basic page numbering, IronPDF supports advanced scenarios including:

  • Section-based numbering: Different numbering schemes for different document sections
  • Roman numerals: Custom formatting for preface or appendix pages
  • Multiple numbering systems: Combining different numbering styles in a single document
  • Dynamic page counts: Updating total page counts when adding or copying pages

These techniques are valuable when creating complex documents like technical manuals, academic papers, or legal documents requiring sophisticated page numbering schemes.

Best Practices for Page Numbers

When implementing page numbers in your PDFs, consider these practices:

  1. Consistency: Maintain consistent positioning and formatting throughout your document
  2. Readability: Ensure page numbers are clearly visible and don't overlap with content
  3. Accessibility: Use appropriate font sizes and contrast for easy reading
  4. Professional appearance: Match the page number style with your document's overall design

Following these guidelines and leveraging IronPDF's flexible page numbering capabilities enables you to create professional, well-organized PDF documents meeting your specific requirements.

Frequently Asked Questions

How do I add basic page numbers to a PDF in C#?

With IronPDF, you can add page numbers using placeholder strings {page} and {total-pages} in either the TextHeaderFooter or HtmlHeaderFooter class. Simply include these placeholders in your header or footer HTML fragment, and IronPDF will automatically replace them with the current page number and total page count when rendering the PDF.

Can I add page numbers directly through rendering options?

Yes, you can add page numbers directly to the rendering options of IronPDF's ChromePdfRenderer. This approach allows you to set headers and footers with page number placeholder strings as part of the rendering configuration, which is particularly useful when customizing PDF output through rendering options.

What placeholder strings are available for page numbering?

IronPDF provides two main placeholder strings for page numbering: {page} displays the current page number, and {total-pages} shows the total number of pages in the document. These placeholders are automatically replaced with actual values when the PDF is rendered.

Can I apply page numbers to specific pages only?

Yes, IronPDF allows you to control where page numbers appear in your document. You can specify which pages should display page numbers, enabling you to exclude them from cover pages or apply them only to certain sections of your PDF.

Is it possible to customize the placement and styling of page numbers?

Absolutely. With IronPDF's HtmlHeaderFooter class, you can customize the placement, styling, and formatting of page numbers using HTML and CSS. You can control their position, font, size, color, and even add divider lines or other decorative elements.

Can I add page numbers to existing PDF documents?

Yes, IronPDF supports adding page numbers to both new PDFs and existing PDF documents. You can load an existing PDF document and apply headers or footers with page numbers without recreating the entire document from scratch.

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 ...
Read More
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
Ready to Get Started?
Nuget Downloads 16,901,161 | Version: 2025.12 just released