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 a text header for the PDF with the page numbering format
TextHeaderFooter textHeader = new TextHeaderFooter
{
    // Display current page of total pages in the center of the header
    CenterText = "{page} of {total-pages}"
};

// Create an HTML footer for the PDF with the page numbering format
HtmlHeaderFooter htmlFooter = new HtmlHeaderFooter
{
    // HTML containing italicized page numbers in the footer
    HtmlFragment = "<center><i>{page} of {total-pages}</i></center>"
};

// Initialize a new ChromePdfRenderer instance to render the PDF from HTML
ChromePdfRenderer renderer = new ChromePdfRenderer();

// Render a new PDF document from the specified HTML content
PdfDocument pdf = renderer.RenderHtmlAsPdf("<h1>Hello World!</h1>");

// Add the text header to the PDF document
pdf.AddTextHeader(textHeader);

// Add the HTML footer to the PDF document
pdf.AddHtmlFooter(htmlFooter);

// Save the rendered PDF document with headers and footers to a file
pdf.SaveAs("pdfWithPageNumber.pdf");
Imports IronPdf



' Create a text header for the PDF with the page numbering format

Private textHeader As New TextHeaderFooter With {.CenterText = "{page} of {total-pages}"}



' Create an HTML footer for the PDF with the page numbering format

Private htmlFooter As New HtmlHeaderFooter With {.HtmlFragment = "<center><i>{page} of {total-pages}</i></center>"}



' Initialize a new ChromePdfRenderer instance to render the PDF from HTML

Private renderer As New ChromePdfRenderer()



' Render a new PDF document from the specified HTML content

Private pdf As PdfDocument = renderer.RenderHtmlAsPdf("<h1>Hello World!</h1>")



' Add the text header to the PDF document

pdf.AddTextHeader(textHeader)



' Add the HTML footer to the PDF document

pdf.AddHtmlFooter(htmlFooter)



' Save the rendered PDF document with headers and footers to a file

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;

// Create a new instance of ChromePdfRenderer
ChromePdfRenderer renderer = new ChromePdfRenderer();

// Set rendering options for a text header with page numbers
renderer.RenderingOptions.TextHeader = new TextHeaderFooter()
{
    CenterText = "{page} of {total-pages}" // Header text format
};

// Set rendering options for an HTML footer with page numbers
renderer.RenderingOptions.HtmlFooter = new HtmlHeaderFooter()
{
    HtmlFragment = "<center><i>{page} of {total-pages}</i></center>" // Footer text format
};

// Define the HTML content to be converted into the PDF
string html = @"
    <h1>Hello World!</h1>
    <div style='page-break-after: always;'></div>
    <h1>2nd Page!</h1>";

// Render the HTML content to a PDF document
PdfDocument pdf = renderer.RenderHtmlAsPdf(html);

// Save the generated PDF to a file
pdf.SaveAs("applyPageNumberWithRenderingOptions.pdf");
Imports IronPdf



' Create a new instance of ChromePdfRenderer

Private renderer As New ChromePdfRenderer()



' Set rendering options for a text header with page numbers

renderer.RenderingOptions.TextHeader = New TextHeaderFooter() With {.CenterText = "{page} of {total-pages}"}



' Set rendering options for an HTML footer with page numbers

renderer.RenderingOptions.HtmlFooter = New HtmlHeaderFooter() With {.HtmlFragment = "<center><i>{page} of {total-pages}</i></center>"}



' Define the HTML content to be converted into the PDF

Dim html As String = "

    <h1>Hello World!</h1>

    <div style='page-break-after: always;'></div>

    <h1>2nd Page!</h1>"



' Render the HTML content to a PDF document

Dim pdf As PdfDocument = renderer.RenderHtmlAsPdf(html)



' Save the generated PDF to a file

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;

/// <summary>
/// This script generates a multi-page PDF document using the IronPdf library.
/// Each page contains different content, and explicit page breaks ensure proper pagination.
/// A header can optionally be added to each page to display page numbers.
/// </summary>
class PdfDocumentCreator
{
    // Define the multi-page HTML content with explicit page breaks for separating pages in the PDF.
    string multiPageHtml = @"
        <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 a header with page number placeholders to be included in the PDF.
    HtmlHeaderFooter header = new HtmlHeaderFooter
    {
        HtmlFragment = "<center><i>{page} of {total-pages}</i></center>"
    };

    // Method to generate and save the PDF document
    public void GeneratePdf()
    {
        // Create a new instance of ChromePdfRenderer which will be used to convert HTML to PDF.
        ChromePdfRenderer renderer = new ChromePdfRenderer();

        // Render the HTML to a PDF document.
        // Optionally, include the header to each page by uncommenting the header addition.
        PdfDocument pdf = renderer.RenderHtmlAsPdf(multiPageHtml);
        // pdf.AddHeader(header); // Uncomment this line if the header needs to be added to each page.

        // Generate a list of all page indices present in the document, useful for further custom operations.
        var allPageIndices = Enumerable.Range(0, pdf.PageCount);

        // Add logic here to save or manipulate the PDF as needed.
        // For example, saving the generated PDF to a file:
        pdf.SaveAs("output.pdf");
    }
}

// This creates an instance of PdfDocumentCreator and generates the PDF.
PdfDocumentCreator creator = new PdfDocumentCreator();
creator.GeneratePdf();
Imports IronPdf

Imports System.Linq



''' <summary>

''' This script generates a multi-page PDF document using the IronPdf library.

''' Each page contains different content, and explicit page breaks ensure proper pagination.

''' A header can optionally be added to each page to display page numbers.

''' </summary>

Friend Class PdfDocumentCreator

	' Define the multi-page HTML content with explicit page breaks for separating pages in the PDF.

	Private multiPageHtml 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 a header with page number placeholders to be included in the PDF.

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



	' Method to generate and save the PDF document

	Public Sub GeneratePdf()

		' Create a new instance of ChromePdfRenderer which will be used to convert HTML to PDF.

		Dim renderer As New ChromePdfRenderer()



		' Render the HTML to a PDF document.

		' Optionally, include the header to each page by uncommenting the header addition.

		Dim pdf As PdfDocument = renderer.RenderHtmlAsPdf(multiPageHtml)

		' pdf.AddHeader(header); // Uncomment this line if the header needs to be added to each page.



		' Generate a list of all page indices present in the document, useful for further custom operations.

		Dim allPageIndices = Enumerable.Range(0, pdf.PageCount)



		' Add logic here to save or manipulate the PDF as needed.

		' For example, saving the generated PDF to a file:

		pdf.SaveAs("output.pdf")

	End Sub

End Class



' This creates an instance of PdfDocumentCreator and generates the PDF.

Private creator As New PdfDocumentCreator()

creator.GeneratePdf()
$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).
// This code filters out even indices which typically correlate with odd-numbered pages 
// due to zero-based indexing.

using System;
using System.Collections.Generic;
using System.Linq;

// Assuming 'allPageIndices' is a List<int> containing all available page indices.
// Note: In a real-world application, you need to ensure this list is initialized correctly.
List<int> allPageIndices = new List<int> { 0, 1, 2, 3, 4, 5 }; // Example initialization

// Retrieve the collection of even indices among all page indices using a lambda expression
// and the IEnumerable<T>.Where extension method.
var evenPageIndices = allPageIndices.Where(i => i % 2 == 0).ToList();

// Assuming 'pdf' is an object of a class that represents a PDF document and has the methods 
// 'AddHtmlHeaders' and 'SaveAs'. 
// Note: You need to initialize 'pdf' with your specific PDF handling class instance.
var pdf = new PdfDocument(); // Example placeholder for actual PDF document instance

// Add HTML headers to each of the pages identified by evenPageIndices.
// This process typically adds a specific header format to every odd page in the document, 
// given the previous step's output.
string header = "<h1>This is a header</h1>"; // Example header content
pdf.AddHtmlHeaders(header, 1, evenPageIndices);

// Save the modified PDF with changes applied only to the specified pages.
// The file is saved to disk with the name "EvenPages.pdf".
pdf.SaveAs("EvenPages.pdf");

// Definition of the placeholder PdfDocument class for demonstration purposes.
public class PdfDocument
{
    public void AddHtmlHeaders(string htmlHeader, int someOtherParameter, IEnumerable<int> pages)
    {
        // Logic to add HTML headers to specified pages.
        Console.WriteLine($"Adding headers to pages: {string.Join(", ", pages)}");
    }

    public void SaveAs(string fileName)
    {
        // Logic to save the PDF document.
        Console.WriteLine($"PDF saved as {fileName}");
    }
}
' Get even page indexes (resulting in odd page numbers).

' This code filters out even indices which typically correlate with odd-numbered pages 

' due to zero-based indexing.



Imports System

Imports System.Collections.Generic

Imports System.Linq



' Assuming 'allPageIndices' is a List<int> containing all available page indices.

' Note: In a real-world application, you need to ensure this list is initialized correctly.

Private allPageIndices As New List(Of Integer) From {0, 1, 2, 3, 4, 5} ' Example initialization



' Retrieve the collection of even indices among all page indices using a lambda expression

' and the IEnumerable<T>.Where extension method.

Private evenPageIndices = allPageIndices.Where(Function(i) i Mod 2 = 0).ToList()



' Assuming 'pdf' is an object of a class that represents a PDF document and has the methods 

' 'AddHtmlHeaders' and 'SaveAs'. 

' Note: You need to initialize 'pdf' with your specific PDF handling class instance.

Private pdf = New PdfDocument() ' Example placeholder for actual PDF document instance



' Add HTML headers to each of the pages identified by evenPageIndices.

' This process typically adds a specific header format to every odd page in the document, 

' given the previous step's output.

Private header As String = "<h1>This is a header</h1>" ' Example header content

pdf.AddHtmlHeaders(header, 1, evenPageIndices)



' Save the modified PDF with changes applied only to the specified pages.

' The file is saved to disk with the name "EvenPages.pdf".

pdf.SaveAs("EvenPages.pdf")



' Definition of the placeholder PdfDocument class for demonstration purposes.

'INSTANT VB TODO TASK: Local functions are not converted by Instant VB:

'public class PdfDocument

'{

'	public void AddHtmlHeaders(string htmlHeader, int someOtherParameter, IEnumerable<int> pages)

'	{

'		' Logic to add HTML headers to specified pages.

'		Console.WriteLine(string.Format("Adding headers to pages: {0}", string.Join(", ", pages)));

'	}

'

'	public void SaveAs(string fileName)

'	{

'		' Logic to save the PDF document.

'		Console.WriteLine(string.Format("PDF saved as {0}", fileName));

'	}

'}
$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
// Using LINQ to filter a list of page indexes to obtain odd index values,
// which correspond to even page numbers when dealing with zero-based index systems.

using System.Linq;
using SomePdfLibrary; // Assuming there's a library that includes pdf.AddHtmlHeaders and pdf.SaveAs methods

// Assume 'allPageIndices' is a predefined list or array of integers that represents page indexes.
// In C#, list indexing is zero-based meaning index 0 is the first element in the list.

// Get odd page indexes (resulting in even page numbers)
// In zero-based indexing, an odd index i (i % 2 != 0) actually refers to an even page number.
var allPageIndices = new List<int> { 0, 1, 2, 3, 4, 5 }; // Example initialization for demonstration
var oddPageIndexes = allPageIndices.Where(i => i % 2 != 0);

// Assume 'pdf' is an instance of a PDF document object with methods implemented to modify/save PDF documents.
var pdf = new PdfDocument(); // Example initialization for demonstration

// Adding HTML headers to the PDF on all odd page indexes (even page numbers).
// The parameters for 'AddHtmlHeaders' are assumed to be 'header' template, alignment (1), and the odd indexes.
string header = "<h1>Header</h1>"; // Example header
pdf.AddHtmlHeaders(header, 1, oddPageIndexes.ToList());

// Save the modified PDF document as "OddPages.pdf".
pdf.SaveAs("OddPages.pdf");
' Using LINQ to filter a list of page indexes to obtain odd index values,

' which correspond to even page numbers when dealing with zero-based index systems.



Imports System.Linq

Imports SomePdfLibrary ' Assuming there's a library that includes pdf.AddHtmlHeaders and pdf.SaveAs methods



' Assume 'allPageIndices' is a predefined list or array of integers that represents page indexes.

' In C#, list indexing is zero-based meaning index 0 is the first element in the list.



' Get odd page indexes (resulting in even page numbers)

' In zero-based indexing, an odd index i (i % 2 != 0) actually refers to an even page number.

Private allPageIndices = New List(Of Integer) From {0, 1, 2, 3, 4, 5} ' Example initialization for demonstration

Private oddPageIndexes = allPageIndices.Where(Function(i) i Mod 2 <> 0)



' Assume 'pdf' is an instance of a PDF document object with methods implemented to modify/save PDF documents.

Private pdf = New PdfDocument() ' Example initialization for demonstration



' Adding HTML headers to the PDF on all odd page indexes (even page numbers).

' The parameters for 'AddHtmlHeaders' are assumed to be 'header' template, alignment (1), and the odd indexes.

Private header As String = "<h1>Header</h1>" ' Example header

pdf.AddHtmlHeaders(header, 1, oddPageIndexes.ToList())



' Save the modified PDF document as "OddPages.pdf".

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
// Import necessary libraries
using System.Collections.Generic;

// Assuming there is a PDF document handling class with an instance 'pdf'.
// The 'pdf' object is expected to already be defined in the surrounding context.
// For example: PDFDocument pdf = new PDFDocument();

// Note: Ensure that 'pdf' has been properly initialized before executing this code.

// Define a list containing the index of the last page.
// This assumes that the PDF document class has a property 'PageCount' which gives the total number of pages,
// and the page index is zero-based.
var lastPageIndex = new List<int> { pdf.PageCount - 1 };

// Add HTML headers to the last page.
// This assumes that there is a method 'AddHtmlHeaders' that adds HTML headers to specified pages.
// Parameters:
// 'header': The HTML header content to be added. Make sure 'header' is defined in the context.
// '1': Priority or tag of the header, likely predefined or necessary for method overload.
// 'lastPageIndex': The list of page indexes to which the header will be added.
pdf.AddHtmlHeaders(header, 1, lastPageIndex);

// Save the changes in the document as "LastPageOnly.pdf".
// This assumes that there is a method 'SaveAs' in the PDF class that saves the document with the given filename.
pdf.SaveAs("LastPageOnly.pdf");
' Import necessary libraries

Imports System.Collections.Generic



' Assuming there is a PDF document handling class with an instance 'pdf'.

' The 'pdf' object is expected to already be defined in the surrounding context.

' For example: PDFDocument pdf = new PDFDocument();



' Note: Ensure that 'pdf' has been properly initialized before executing this code.



' Define a list containing the index of the last page.

' This assumes that the PDF document class has a property 'PageCount' which gives the total number of pages,

' and the page index is zero-based.

Private lastPageIndex = New List(Of Integer) From {pdf.PageCount - 1}



' Add HTML headers to the last page.

' This assumes that there is a method 'AddHtmlHeaders' that adds HTML headers to specified pages.

' Parameters:

' 'header': The HTML header content to be added. Make sure 'header' is defined in the context.

' '1': Priority or tag of the header, likely predefined or necessary for method overload.

' 'lastPageIndex': The list of page indexes to which the header will be added.

pdf.AddHtmlHeaders(header, 1, lastPageIndex)



' Save the changes in the document as "LastPageOnly.pdf".

' This assumes that there is a method 'SaveAs' in the PDF class that saves the document with the given filename.

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
// Import necessary namespaces
using System.Collections.Generic;

// The following code assumes 'pdf' is an instance of a PDF document handling class.
// The variables and methods 'AddHtmlHeaders' and 'header' are assumed to be defined elsewhere
// in the application.

// List containing the index of the first page.
// In this context, the index 0 corresponds to the first page of the PDF document.
var firstPageIndex = new List<int> { 0 };

// Add HTML headers to only the first page of the PDF document.
// AddHtmlHeaders() is assumed to be a method that adds HTML headers to specified pages in the PDF.
// Parameters:
// - 'header': string containing the HTML header content.
// - '1': integer representing the HTML header level to be added (e.g., <h1>).
// - 'firstPageIndex': list containing the page indices where the header should be added.
pdf.AddHtmlHeaders(header, 1, firstPageIndex);

// Save the modified PDF document with the specified file name.
// The modified document will be saved as "FirstPageOnly.pdf".
pdf.SaveAs("FirstPageOnly.pdf");
' Import necessary namespaces

Imports System.Collections.Generic



' The following code assumes 'pdf' is an instance of a PDF document handling class.

' The variables and methods 'AddHtmlHeaders' and 'header' are assumed to be defined elsewhere

' in the application.



' List containing the index of the first page.

' In this context, the index 0 corresponds to the first page of the PDF document.

Private firstPageIndex = New List(Of Integer) From {0}



' Add HTML headers to only the first page of the PDF document.

' AddHtmlHeaders() is assumed to be a method that adds HTML headers to specified pages in the PDF.

' Parameters:

' - 'header': string containing the HTML header content.

' - '1': integer representing the HTML header level to be added (e.g., <h1>).

' - 'firstPageIndex': list containing the page indices where the header should be added.

pdf.AddHtmlHeaders(header, 1, firstPageIndex)



' Save the modified PDF document with the specified file name.

' The modified document will be saved as "FirstPageOnly.pdf".

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
// Assuming allPageIndices is a collection of page indices, with the first page being at index 0.
// This collection should be initialized with appropriate values before this code snippet.

using System.Collections.Generic;
using System.Linq;

public class PdfDocument
{
    // Method to add HTML headers to specific pages of the PDF
    public void AddHtmlHeaders(string header, int startingPageIndex, IEnumerable<int> pageIndices)
    {
        foreach (var pageIndex in pageIndices)
        {
            // Logic to add headers to the specified pages.
            // Ensure that the page index is valid within the context of the PDF document.
            AddHeaderToPage(header, pageIndex);
        }
    }

    // Method to save the PDF document with a specified filename
    public void SaveAs(string filename)
    {
        // Logic to save the document as a file with the given filename.
        // Ensure the file is saved in the desired location with correct permissions.
    }

    // Placeholder method to simulate adding a header to a specific page.
    private void AddHeaderToPage(string header, int pageIndex)
    {
        // Implementation for adding a header to a page.
        // This would normally include the logic to modify the PDF content.
    }
}

// Main code logic
// Instantiate a PdfDocument object.
PdfDocument pdf = new PdfDocument();

// Define a collection of integers representing all page indices.
List<int> allPageIndices = new List<int> { 0, 1, 2, 3, 4 }; // These should be valid page indices.

// Use the Skip method to skip the first page.
// Creates a sequence excluding the first item, which corresponds to page index 0.
IEnumerable<int> skipFirstPage = allPageIndices.Skip(1);

// Specify the header to be added.
string header = "Header Content";

// Add headers to all pages except the first one (page index 0).
pdf.AddHtmlHeaders(header, 1, skipFirstPage);

// Save the resulting PDF to a file.
pdf.SaveAs("SkipFirstPage.pdf");
' Assuming allPageIndices is a collection of page indices, with the first page being at index 0.

' This collection should be initialized with appropriate values before this code snippet.



Imports System.Collections.Generic

Imports System.Linq



Public Class PdfDocument

	' Method to add HTML headers to specific pages of the PDF

	Public Sub AddHtmlHeaders(ByVal header As String, ByVal startingPageIndex As Integer, ByVal pageIndices As IEnumerable(Of Integer))

		For Each pageIndex In pageIndices

			' Logic to add headers to the specified pages.

			' Ensure that the page index is valid within the context of the PDF document.

			AddHeaderToPage(header, pageIndex)

		Next pageIndex

	End Sub



	' Method to save the PDF document with a specified filename

	Public Sub SaveAs(ByVal filename As String)

		' Logic to save the document as a file with the given filename.

		' Ensure the file is saved in the desired location with correct permissions.

	End Sub



	' Placeholder method to simulate adding a header to a specific page.

	Private Sub AddHeaderToPage(ByVal header As String, ByVal pageIndex As Integer)

		' Implementation for adding a header to a page.

		' This would normally include the logic to modify the PDF content.

	End Sub

End Class



' Main code logic

' Instantiate a PdfDocument object.

Private pdf As New PdfDocument()



' Define a collection of integers representing all page indices.

Private allPageIndices As New List(Of Integer) From {0, 1, 2, 3, 4} ' These should be valid page indices.



' Use the Skip method to skip the first page.

' Creates a sequence excluding the first item, which corresponds to page index 0.

Private skipFirstPage As IEnumerable(Of Integer) = allPageIndices.Skip(1)



' Specify the header to be added.

Private header As String = "Header Content"



' Add headers to all pages except the first one (page index 0).

pdf.AddHtmlHeaders(header, 1, skipFirstPage)



' Save the resulting PDF to a file.

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
// The following code demonstrates how to skip the first page of a PDF document's list
// of page indices, then add HTML headers to the remaining pages, starting the numbering
// from page 1. Finally, it saves the modified PDF to a specified file.

using System.Collections.Generic;
using System.Linq; // Required for the Skip method

// Example list simulating all page indices of a PDF document.
List<int> allPageIndices = new List<int> { 0, 1, 2, 3 };

// Skip the first page (page index 0) and start numbering from the second page (index 1).
// Use the Skip method from LINQ to create a list of page indices except the first one.
IEnumerable<int> skipFirstPageAndDontCountIt = allPageIndices.Skip(1);

// Assume 'pdf' is an instance of a PDF handling class with a method 'AddHtmlHeaders'.
// This method applies headers to pages, starting numbering at the specified start index.
// It uses the provided page indices, here represented by 'skipFirstPageAndDontCountIt'. 
// Replace 'pdf' with your actual PDF handling object and ensure you have this method.
pdf.AddHtmlHeaders(header: "Example Header", startPageNumber: 1, pageIndices: skipFirstPageAndDontCountIt);

// Save the modified document to a file named "SkipFirstPageAndDontCountIt.pdf".
// Ensure that your PDF handling object has a method SaveAs that performs this action.
pdf.SaveAs("SkipFirstPageAndDontCountIt.pdf");
' The following code demonstrates how to skip the first page of a PDF document's list

' of page indices, then add HTML headers to the remaining pages, starting the numbering

' from page 1. Finally, it saves the modified PDF to a specified file.



Imports System.Collections.Generic

Imports System.Linq ' Required for the Skip method



' Example list simulating all page indices of a PDF document.

Private allPageIndices As New List(Of Integer) From {0, 1, 2, 3}



' Skip the first page (page index 0) and start numbering from the second page (index 1).

' Use the Skip method from LINQ to create a list of page indices except the first one.

Private skipFirstPageAndDontCountIt As IEnumerable(Of Integer) = allPageIndices.Skip(1)



' Assume 'pdf' is an instance of a PDF handling class with a method 'AddHtmlHeaders'.

' This method applies headers to pages, starting numbering at the specified start index.

' It uses the provided page indices, here represented by 'skipFirstPageAndDontCountIt'. 

' Replace 'pdf' with your actual PDF handling object and ensure you have this method.

pdf.AddHtmlHeaders(header:= "Example Header", startPageNumber:= 1, pageIndices:= skipFirstPageAndDontCountIt)



' Save the modified document to a file named "SkipFirstPageAndDontCountIt.pdf".

' Ensure that your PDF handling object has a method SaveAs that performs this action.

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.
Talk to an Expert Five Star Trust Score Rating

Ready to Get Started?

Nuget Passed