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 EaseAdd 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.
-
1Install IronPDF with NuGet Package Manager
-
2Copy 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");C# -
3Deploy to test on your live environment
Start using IronPDF in your project today with a free trial
Minimal Workflow (5 steps)
- Download the C# PDF Library from NuGet
- Load an existing PDF document or render a new one
- Use placeholder strings
{page}and{total-pages}in the Header and Footer to insert page numbers - Apply page numbers to specific pages or sections
- Review the resulting PDF document
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.
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")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.
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")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:
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)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.
// 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")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.
// 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")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.
// 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")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.
// 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")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.
// 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")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.
// 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")To explore all metadata options, visit the IronPDF Headers and Footers Guide.
The IronSuite play a crucial role in our operations. These are tools that increase efficiencies across the business including creating floor plans and improving inventory management.
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:
- Consistency: Maintain consistent positioning and formatting throughout your document
- Readability: Ensure page numbers are clearly visible and don't overlap with content
- Accessibility: Use appropriate font sizes and contrast for easy reading
- 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 can I add page numbers to a PDF using IronPDF?
IronPDF allows you to add page numbers to a PDF using placeholder strings like `{page}` and `{total-pages}` directly in the header or footer sections. This can be done using either the `TextHeaderFooter` or `HtmlHeaderFooter` classes.
Can I customize the placement of page numbers in a PDF with IronPDF?
Yes, IronPDF lets you customize page number placement by specifying whether they appear in headers or footers, as well as defining their alignment within the document.
Is it possible to add page numbers to specific pages only with IronPDF?
With IronPDF, you can apply page numbers to specific pages or sections, allowing you to start numbering from a specific page or apply numbers to even or odd pages only.
Can I start numbering from the second page with IronPDF?
Yes, you can skip the first page and start numbering from the second page using IronPDF, treating it as page 1, which is useful for documents with a cover page.
What advanced page numbering techniques does IronPDF support?
IronPDF supports advanced techniques such as section-based numbering, using Roman numerals, multiple numbering systems within the same document, and dynamic page counts when adding or copying pages.
How do I add page numbers using minimal C# code in IronPDF?
You can add page numbers with minimal C# code by setting the `HtmlFooter` with page placeholders like `{page} of {total-pages}` in `ChromePdfRenderer` and rendering your HTML as a PDF.
Can I add page numbers to only even or odd pages in a PDF with IronPDF?
Yes, IronPDF enables you to filter and apply page numbers exclusively to even or odd page indexes, which is ideal for documents needing traditional book formatting.
How can I skip the first page when adding page numbers with IronPDF?
To skip the first page, you can simply adjust the page range indices and apply page numbers starting from the second page onward using IronPDF.
Can IronPDF handle custom page numbering formats like Roman numerals?
Yes, IronPDF can handle various custom formatting options, including Roman numerals, which can be useful for sections like prefaces or appendices.
What are the best practices for implementing page numbers in PDFs using IronPDF?
Ensure consistency in positioning and formatting, maintain readability with clear visibility, use appropriate font sizes for accessibility, and match the style with the document's design for a professional look.

Curtis Chau holds a Bachelor’s degree in Computer Science (Carleton University) and specializes in front-end development with expertise in Node.js, TypeScript, JavaScript, and React. Passionate about crafting intuitive and aesthetically pleasing user interfaces, Curtis enjoys working with modern frameworks and creating well-structured, visually appealing manuals.