Test in a live environment
Test in production without watermarks.
Works wherever you need it to.
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.
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.
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:
Visual Studio opening ui
Create a new project in Visual Studio
Configure your new project
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.
There are many ways to install the IronPDF library:
Using the Package Manager Console is the recommended method. Follow the following steps to install IronPDF:
Install-Package IronPdf
The progress of installing the IronPdf package in the Package Manager Console tab
Follow the steps below to install the IronPDF library using NuGet Package Manager:
Navigate to 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.
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.
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
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")
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
Page numbers will be shown like this on the edited document.
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.
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.
9 .NET API products for your office documents