PDF TOOLS

How to Add Page Numbers to a PDF File

Updated August 13, 2024
Share:

How to Add Page Numbers to a PDF File

PDFs are everywhere - and for good reason. They’re secure, easy to share, and work on just about any machine or browser in the world. However, despite millions of people using PDF files every day, they can still be tricky to properly use.

One of the best features of PDF documents is that they’re hard to tamper with, but that gets in the way when you’re looking to make some changes of your own. In this guide, we’ll show how easy it is to add page numbers to a PDF file using our feature-rich C# library, IronPDF, as well as recommend a couple of alternative online tools which offer basic functionality.

IronPDF: The Premier C# PDF Library

IronPDF is a lightweight .NET PDF library designed specifically with web developers in mind. PDF conversion is just one of many uses - it makes reading, writing, and manipulating PDFs a breeze, and you can use it in your .NET projects for both desktop and web. The best part - it’s free to try out in a development environment.

One of the main advantages of IronPDF is its flexibility. It can be used as a standalone desktop application or integrated into your web application using the IronPDF API. Additionally, IronPDF is compatible with Windows, Mac, and Linux operating systems, making it accessible to a wide range of users.

Adding Page Numbers to a PDF File - Getting Started

Create Your Project in Visual Studio

First of all, open Visual Studio and go to File -> New Project -> Console Application. Enter your project name, choose the location you want to save it to, and hit the Next button. Select the latest .NET Framework and then Create. Once your project is up and running, it’s time to add our library.

Install the IronPDF Library

IronPDF is easy to use but it’s even easier to install. There are a couple of ways you can do it:

Method 1: NuGet Package Manager Console

In Visual Studio, in Solution Explorer, right-click References, and then click Manage NuGet Packages. Hit browse and search ‘IronPDF, and install the latest version. If you see this, it’s working:

How to Add Page Numbers to a PDF File: Figure 1

You can also go to Tools -> NuGet Package Manager -> Packet Manager Console, and enter the following line in the Package Manager Tab:

Install-Package IronPdf

Finally, you can get IronPDF directly from NuGet’s official website. Select the Download Package option from the menu on the right of the page, double-click your download to install it automatically, and reload the Solution to start using it in your project.

Didn’t work? You can find platform-specific help on our advanced NuGet installation page.

Method 2: Using a DLL file

You can also get the IronPDF DLL file straight from us and add it to Visual Studio manually. For full instructions and links to the Windows, MacOS, and Linux DLL packages, check out our dedicated installation page.

Add the IronPDF Namespace

Always remember to kick off your code with the IronPDF namespace, like this:

using IronPdf;
using IronPdf;
Imports IronPdf
VB   C#

How to Add Page Numbers to a PDF File using IronPDF

We kick things off by importing the IronPDF package and then creating an HTML string which will be converted to PDF with page numbers. Here’s the code example:

using IronPdf;

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>";
using IronPdf;

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>";
IRON VB CONVERTER ERROR developers@ironsoftware.com
VB   C#

Next, we’ll instantiate ChromePDFRender with specific HTML footer settings. This allows us to define the styling and layout of the page numbers in our PDF. Here’s the code:

// Instantiate Renderer
var renderer = new ChromePdfRenderer
{
   RenderingOptions =
   {
       HtmlFooter = new HtmlHeaderFooter()
       {

           MaxHeight = 15, // millimeters
           HtmlFragment = "<center><i>{page} of {total-pages}<i></center>",
           DrawDividerLine = true
       }
   }
};
// Instantiate Renderer
var renderer = new ChromePdfRenderer
{
   RenderingOptions =
   {
       HtmlFooter = new HtmlHeaderFooter()
       {

           MaxHeight = 15, // millimeters
           HtmlFragment = "<center><i>{page} of {total-pages}<i></center>",
           DrawDividerLine = true
       }
   }
};
IRON VB CONVERTER ERROR developers@ironsoftware.com
VB   C#

Then, we’ll render the HTML as a PDF. Simply call the RenderHtmlAsPdf method to convert it to PDF and IronPDF will automatically add numbers to each page.

var pdf = renderer.RenderHtmlAsPdf(html);
var pdf = renderer.RenderHtmlAsPdf(html);
Dim pdf = renderer.RenderHtmlAsPdf(html)
VB   C#

And finally, all we need to do now is save the newly numbered PDF to our system. It’s as simple as this:

pdf.SaveAs("pageNumber.pdf");
pdf.SaveAs("pageNumber.pdf");
pdf.SaveAs("pageNumber.pdf")
VB   C#

As you saw in the code before, we customized our page number format to be centered in the page, to show the current and total number of pages, and add a divider line. Here’s what the final product looks like:

How to Add Page Numbers to a PDF File: Figure 2

Alternative PDF Page Numbering Tools

There are many free, if limited, tools which allow you to add page numbers to PDFs. Here are some alternative PDF page numbering options.

Sejda - Online PDF Tool

Sejda is an online tool which offers basic PDF editing tools such as merging pages, compressing the file size, creating forms, and more. While it is free to use, it comes with page and time restrictions before you have to subscribe to one of the paid plans.

How to Add Page Numbers to a PDF Using Sejda Online PDF Tool

  • Visit the Sejda website and find 'Page Numbers' in the list of tools - click it
  • Upload the PDF file you want to add page numbers to
  • Once it loads, browse the customization options you want to apply
  • When you’re happy, click Add Header/Footer to process the file
  • After the page numbers have been added, click Download to save your file

Smallpdf

SmallPDF is a popular online tool that offers a range of PDF editing features, including the ability to add page numbers. One of the main advantages of SmallPDF is its convenience. Because it is a web-based tool, you can access it from any device with an internet connection, without needing to download or install any software.

Add Page Numbers to a PDF Using Smallpdf

  • Go to SmallPDF, open the Tools meny at the top, and click Number Pages
  • When you upload your file, a new menu will appear asking you to choose the position of your page numbers and which number to start from
  • SmallPDF does not have any other customization or personalization options
  • Click Number Pages and, once it’s processed, download the PDF

Conclusion

Adding page numbers to PDF documents is a straightforward way to improve readability and organization of your document. It’s a simple task, but one which different tools approach in various ways and with a range of features and options.

Free online tools can be quick and convenient, until you hit the limit and need to subscribe to a plan, but they can lack the in-depth control which a dedicated PDF library - like IronPDF - provides.

And PDF page numbers are just the start - with PDF formatting tools, built-in security and compliance features, and more, IronPDF is the number one tool for all of your PDF document needs.

Ready to get your hands on IronPDF? You can start with our 30-day free trial. It’s also completely free to use for development purposes so you can really get to see what it’s made of. And if you like what you see, IronPDF starts as low as $749. For even bigger savings, check out the Iron Suite where you can get all nine Iron Software tools for the price of two. Happy coding!

How to Add Page Numbers to a PDF File: Figure 3

< PREVIOUS
How To Convert PDF to PDFA in C++
NEXT >
How To Reduce PDF File Size Without Losing Quality (Online Tools)

Ready to get started? Version: 2024.10 just released

Free NuGet Download Total downloads: 11,308,499 View Licenses >