USING IRONPDF

How to Add Page Numbers in PDF using C#

Updated June 23, 2022
Share:

Introduction

Page numbers in PDFs inform readers of the document index so they can locate a specific page. They also assist readers in organizing, filling out forms, or preparing for meetings or other interactions. The IronPDF C# library provides editing functionality for PDFs programmatically. In this article, IronPDF will be used to add page numbers to PDF documents.

IronPDF: C# Library

The IronPDF C# library is a whole suite of custom PDF-specific functionality wrapped up into a single, streamlined API. With its set of ready-to-use effects, layouts, and more, it's time for .NET developers to breathe new life into their PDF creations. The IronPDF C# library promises to change that by making PDF creation fast and easy while saving time and money.

Let's take a look at how to add page numbers to a PDF document using the IronPDF C# library.

Create or Open a C# Project

This tutorial will use the latest version of Visual Studio. You must have the .NET Framework installed on your machine because it is needed to run the IronPDF library. Follow the given steps to create the C# project:

  • Open Visual Studio

How to Add Page Numbers in PDF using C#, Figure 1: Visual Studio opening ui Visual Studio opening ui

  • Click on the "Create a New Project" button.

How to Add Page Numbers in PDF using C#, Figure 2: Create a new project in Visual Studio Create a new project in Visual Studio

  • Select C# Console Application from project templates and click on the Next button. Now give a name to the project and click on the Next button.

How to Add Page Numbers in PDF using C#, Figure 3: Configure your new project Configure your new project

  • After that, select the .NET Framework. The latest version is recommended. After that, click the Create button. It will create the C# project.

How to Add Page Numbers in PDF using C#, Figure 4: The created project with an empty Program.cs file The created project with an empty Program.cs file

The C# project has now been created. The IronPDF library can also be added as a reference to any existing C# project. Just open the project and install the IronPDF library. The next section will show how to install the IronPDF library into .NET project.

Install the IronPDF Library

There are many ways to install the IronPDF library:

  • Using Package Manager Console
  • Using NuGet Package Manager
  • By DLL file

Using Package Manager Console

Using the Package Manager Console is the recommended method. Follow the following steps to install IronPDF:

  • Open the Package Manager Console (usually located at the bottom of Visual Studio).
  • Write the following command in the console.
Install-Package IronPdf
  • It will start installing the library. You will see the progress in the console.
  • After installation, you will be able to use the IronPDF library in your project.

How to Add Page Numbers in PDF using C#, Figure 5: The progress of installing the IronPdf package in the Package Manager Console tab The progress of installing the IronPdf package in the Package Manager Console tab

Using NuGet Package Manager

Follow the steps below to install the IronPDF library using NuGet Package Manager:

  • Open the C# project and go to Tools > NuGet Package Manager > Manage NuGet Packages for Solution.

How to Add Page Numbers in PDF using C#, Figure 6: Navigate to NuGet Package Manager Navigate to NuGet Package Manager

  • It will open the NuGet Package Manager Window. Click on the Browse menu. After that, write IronPDF in the search bar. Select the first one.

How to Add Page Numbers in PDF using C#, Figure 7: Install the IronPdf package using NuGet Package Manager Install the IronPdf package using NuGet Package Manager

Click the Install button. It will install the IronPDF library and the project will be ready to use with the IronPDF library.

By DLL file

Alternatively, you can directly download the DLL. After downloading the file, add a reference to the project. Visit our official documentation page for a more detailed installation tutorial.

The installation is now complete. It's time to write the code to add page numbers to the PDF file. This works for both new and existing PDF documents. Let's see how it's done.

Add Page Numbers in the PDF document

It is now time to add the page numbers to a new document. Firstly, the IronPdf namespace should be added. Add the following code at the top of the code file:

using IronPdf;
using IronPdf;
Imports IronPdf
VB   C#

Now add the following code snippet in the main method of the project:

var html = @"
<p> Hello Iron</p>
<p> This is 1st Page </p>
<div style = 'page-break-after: always;' ></div>
<p> This is 2nd Page</p>
<div style = 'page-break-after: always;' ></div>
<p> This is 3rd Page</p>";

var Renderer = new ChromePdfRenderer();

Renderer.RenderingOptions.HtmlFooter = new HtmlHeaderFooter()
{

    MaxHeight = 15, //millimeters
    HtmlFragment = "<center><i>{page} of {total-pages}<i></center>",
    DrawDividerLine = true
};

using var pdfdoc = Renderer.RenderHtmlAsPdf(html);
pdfdoc.SaveAs("pageNumber.pdf");
var html = @"
<p> Hello Iron</p>
<p> This is 1st Page </p>
<div style = 'page-break-after: always;' ></div>
<p> This is 2nd Page</p>
<div style = 'page-break-after: always;' ></div>
<p> This is 3rd Page</p>";

var Renderer = new ChromePdfRenderer();

Renderer.RenderingOptions.HtmlFooter = new HtmlHeaderFooter()
{

    MaxHeight = 15, //millimeters
    HtmlFragment = "<center><i>{page} of {total-pages}<i></center>",
    DrawDividerLine = true
};

using var pdfdoc = Renderer.RenderHtmlAsPdf(html);
pdfdoc.SaveAs("pageNumber.pdf");
Dim html = "
<p> Hello Iron</p>
<p> This is 1st Page </p>
<div style = 'page-break-after: always;' ></div>
<p> This is 2nd Page</p>
<div style = 'page-break-after: always;' ></div>
<p> This is 3rd Page</p>"

Dim Renderer = New ChromePdfRenderer()

Renderer.RenderingOptions.HtmlFooter = New HtmlHeaderFooter() With {
	.MaxHeight = 15,
	.HtmlFragment = "<center><i>{page} of {total-pages}<i></center>",
	.DrawDividerLine = True
}

Dim pdfdoc = Renderer.RenderHtmlAsPdf(html)
pdfdoc.SaveAs("pageNumber.pdf")
VB   C#

The following code sample added the page numbers in a newly created PDF document. IronPDF's "Headers and Footers" functionality allows merging information about page numbering. This works in HtmlHeaderFooters and TextHeaderFooters alike: e.g. {page} of {total-pages}.

If you wish to add a page number to an existing PDF file, then follow the sample code below:

static void Main(string [] args)
{
    PdfDocument document = new PdfDocument("C:\\Users\\tayyabali.ashraf\\Downloads\\Xeem_App.pdf");
    var footer = new HtmlHeaderFooter();
    footer.MaxHeight = 15;
    footer.DrawDividerLine = true;
    footer.HtmlFragment = "<center><i>{page} of {total-pages}<i></center>";
    var allPageIndexes = Enumerable.Range(0, document.PageCount);
    document.AddHtmlFooters(footer, 1, allPageIndexes);
    document.SaveAs("Modified.pdf");
}
static void Main(string [] args)
{
    PdfDocument document = new PdfDocument("C:\\Users\\tayyabali.ashraf\\Downloads\\Xeem_App.pdf");
    var footer = new HtmlHeaderFooter();
    footer.MaxHeight = 15;
    footer.DrawDividerLine = true;
    footer.HtmlFragment = "<center><i>{page} of {total-pages}<i></center>";
    var allPageIndexes = Enumerable.Range(0, document.PageCount);
    document.AddHtmlFooters(footer, 1, allPageIndexes);
    document.SaveAs("Modified.pdf");
}
Shared Sub Main(ByVal args() As String)
	Dim document As New PdfDocument("C:\Users\tayyabali.ashraf\Downloads\Xeem_App.pdf")
	Dim footer = New HtmlHeaderFooter()
	footer.MaxHeight = 15
	footer.DrawDividerLine = True
	footer.HtmlFragment = "<center><i>{page} of {total-pages}<i></center>"
	Dim allPageIndexes = Enumerable.Range(0, document.PageCount)
	document.AddHtmlFooters(footer, 1, allPageIndexes)
	document.SaveAs("Modified.pdf")
End Sub
VB   C#

Output

Page numbers will be shown like this on the edited document.

How to Add Page Numbers in PDF using C#, Figure 8: Page number in the modified PDF file Page number in the modified PDF file

Aside from this, IronPDF offers a lot more advanced features to interact with PDF files including extracting text and content from a PDF, rendering charts in PDFs, and even handling PDF forms programmatically. All of this can easily be implemented via the code samples page.

Conclusion

This tutorial demonstrated how IronPDF helps developers to customize PDFs very easily with simple and short functions, targeting new PDF documents as well as existing PDF documents, there are no limitations. IronPDF is absolutely free for development purposes. You can see for yourself by activating the free trial without any payment information. Moreover, IronPDF currently has an offer — you can purchase five Iron Software packages for the price of just two! More information about the various licenses can be found at the following link.

< PREVIOUS
How to Extract Data from PDF in C#
NEXT >
How to Add Images in PDF using C#

Ready to get started? Version: 2024.7 just released

Free NuGet Download Total downloads: 9,848,088 View Licenses >
123