How to Add Page Numbers in PDF using C#

Introduction

The PDF file has grown enormously in importance because it enables users to complete many tasks over the Internet without converting files or running components. The PDF format is now almost universally supported and is the standard for document sharing. You can also create “read-only” PDF files so that you need not be concerned about them being altered while they are on someone else’s computer.

Page numbers in PDFs tell readers the document index so they can find a specific page. They also help readers organize, fill-out forms, or prepare for meetings or other interactions. Page numbering is one of the most important features that enable easier navigation within a document and keeps them more logically ordered. If we want to edit PDFs programmatically, then we can use the IronPDF C# library. In this article, we will learn about IronPDF and look at how we can add page numbers in PDF documents using IronPDF.

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.

PDFs have become ubiquitous in today’s world of work as the go-to document format for many businesses. They carry all the information you could want and allow instant distribution across all modern platforms. The convenience can come at a cost, however — when you or your developers spend precious hours creating and maintaining PDF documents manually, it can lead to costs that mount over time, not just in terms of money, but also in terms of opportunity. 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 we can add page numbers to a PDF document using the IronPDF C# library.

You can download the software product from this link.

Create or Open a C# Project

In this tutorial, I will use Visual Studio 2019. You can choose any Visual Studio version, but the latest version is recommended. 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 2019
  • Click on the "Create a New Project" button.
  • 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.
  • After that, select the .NET framework. The latest version is recommended. After that, click the "Create" button. It will create the C# project.

The C# project has now been created and we can use the IronPDF library in it. We can also use the existing C# project. Just open the project and install the IronPDF library. In the next section, we will look at how we can install the IronPDF library in our project.

Install the IronPDF Library

There are many ways to install the IronPDF library. We can install it in the following ways:

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

Using Package Manager Console

Using the package manager console, we can install the IronPDF library very easily. 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
Install-Package IronPDF
'INSTANT VB TODO TASK: The following line uses invalid syntax:
'Install-Package IronPDF
VB   C#
  • 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.

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 > NuGet Package Manager for Solution.
  • 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.

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. For a more detailed installation tutorial, visit this link.

The installation has now been completed. It is time to write the code to add the page numbers to the PDF file. We can add page numbers in a new PDF document or we can add page numbers in an existing PDF document. Let's see how this is done.

Add Page Numbers in the PDF document

It is now time to add the page numbers to a new document. When we create a new PDF document, we have to add the page breaks to move to the new page. First of all, we have to add the IronPDF namespace in our code file so that we will be able to use IronPDF. 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 IronPdf.ChromePdfRenderer();

Renderer.RenderingOptions.HtmlFooter = new IronPdf.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 IronPdf.ChromePdfRenderer();

Renderer.RenderingOptions.HtmlFooter = new IronPdf.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 IronPdf.ChromePdfRenderer()

Renderer.RenderingOptions.HtmlFooter = New IronPdf.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 us to merge 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)
{
var Renderer = new IronPdf.ChromePdfRenderer();
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)
{
var Renderer = new IronPdf.ChromePdfRenderer();
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 Renderer = New IronPdf.ChromePdfRenderer()
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.

Conclusion

In this tutorial, we have seen how IronPDF helps developers to customize PDFs very easily with simple and short functions. We can add page numbers in 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 30-day 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.