IRONSOFTWAREHOME
USING IRONPDF FOR PYTHON

How to Add Page Numbers to PDF in Python

Curtis Chau
Curtis Chau
Updated: June 20, 2026

In the ever-evolving landscape of digital documentation, PDFs have become a cornerstone format for sharing data, storing, and presenting written information. However, as the reliance on PDFs continues to grow, developers often encounter several scenarios where programmatically manipulating these files becomes a necessity. One such common requirement is the addition of the page number on all the pages, a crucial element for file navigation and organization. On the first page of this comprehensive guide, we will delve into the intricacies of adding page numbers to PDFs using IronPDF.

Understanding IronPDF

IronPDF stands out as an example of a powerful Python library that simplifies the complexities associated with working with PDF files programmatically. Its extensive feature set makes it a versatile tool for developers seeking to manipulate Python PDFs seamlessly. In this tutorial, our primary focus will be harnessing IronPDF's capabilities to enhance existing PDFs with page numbers, font, and images.

For more information and documentation, please refer to the Python IronPDF page.

Prerequisites

Before embarking on the journey of adding page numbers to your existing PDFs and files, it is imperative to ensure that you have the IronPDF library installed. This can be achieved by executing the following command line below.

> pip install ironpdf

Instantiating the Renderer

The ChromePdfRenderer class is a crucial component of IronPDF that facilitates the rendering of HTML and CSS to output a PDF file. In our script, we instantiate this new PDF renderer function to prepare for the subsequent steps.

# Import IronPDF module
from ironpdf import ChromePdfRenderer

# Instantiate the PDF renderer
renderer = ChromePdfRenderer()
Python

Loading the PDF

The PdfDocument.FromFile method is a function employed to load the existing PDF document file format that we write and intend to enhance with page numbers.

# Import PdfDocument from IronPDF
from ironpdf import PdfDocument

# Load the existing PDF document
document = PdfDocument.FromFile("example.pdf")
Python

IronPDF allows for the creation of selected pages with a range number of pages and custom HTML headers and footers, providing a flexible way to make pages that include dynamic content. In our case, we create pages with an HTML footer that includes page numbers.

# Import HtmlHeaderFooter from IronPDF
from ironpdf import HtmlHeaderFooter

# Create an HTML footer for page numbers
footer = HtmlHeaderFooter()
footer.MaxHeight = 15  # Set the maximum height of the footer
footer.DrawDividerLine = True  # Draw a dividing line in the footer
footer.HtmlFragment = "<center><i>{page}</i></center>"  # HTML for page number
Python

Adding Page Numbers to PDF

The AddHtmlFooters method is a pivotal step in the process. It integrates the reader to write a custom HTML footer into the PDF document output file, effectively adding page numbers to each output PDF page.

# Add the HTML footers to the PDF document
document.AddHtmlFooters(footer)
Python

Saving the Modified PDF

Finally, the SaveAs method is employed to save the modified output PDF file, complete with all the newly created and added page numbers.

# Save the modified PDF document with page numbers
document.SaveAs("Modified_Output.pdf")
Python

Overall Implementation

So far we have discussed the functionalities that we have used to add page numbers to create and import the PDF in the above PDF file format. Have a look at the overall Python implementation of the code below in which the page number is added to the footer of the page at the center of it.

from ironpdf import ChromePdfRenderer, PdfDocument, HtmlHeaderFooter

# Instantiate Renderer
renderer = ChromePdfRenderer()

# Load PDF document
document = PdfDocument.FromFile("example.pdf")
print(f"Number of pages in the document: {document.PageCount}")

# Create HTML footer for page numbers
footer = HtmlHeaderFooter()
footer.MaxHeight = 15
footer.DrawDividerLine = True
footer.HtmlFragment = "<center><i>{page} of {total-pages}</i></center>"

# Add page numbers to the PDF
document.AddHtmlFooters(footer)

# Save the modified PDF
document.SaveAs("Modified.pdf")
Python

The following image is the output file that will center the page number in the PDF.

How to Add Page Numbers to PDF in Python: Figure 1 - Image with page number at center

Page Number at the Right

To make the page number appear on the right side of the footer, use the following code:

from ironpdf import ChromePdfRenderer, PdfDocument, HtmlHeaderFooter

# Instantiate Renderer
renderer = ChromePdfRenderer()

# Load PDF document
document = PdfDocument.FromFile("example.pdf")
print(f"Number of pages in the document: {document.PageCount}")

# Create HTML footer for page numbers
footer = HtmlHeaderFooter()
footer.MaxHeight = 15
footer.DrawDividerLine = True
footer.HtmlFragment = "<div style='text-align: right;'><i>{page} of {total-pages}</i></div>"

# Add page numbers to the PDF
document.AddHtmlFooters(footer)

# Save the modified PDF
document.SaveAs("Modified.pdf")
Python

The image below shows the output result of placing the page number on the right side of the footer.

How to Add Page Numbers to PDF in Python: Figure 2 - Image with page number at the right side

Page Number at the Left

Similarly, to place the page number on the left side of the footer, use the following code:

from ironpdf import ChromePdfRenderer, PdfDocument, HtmlHeaderFooter

# Instantiate Renderer
renderer = ChromePdfRenderer()

# Load PDF document
document = PdfDocument.FromFile("example.pdf")
print(f"Number of pages in the document: {document.PageCount}")

# Create HTML footer for page numbers
footer = HtmlHeaderFooter()
footer.MaxHeight = 15
footer.DrawDividerLine = True
footer.HtmlFragment = "<div style='text-align: left;'><i>{page} of {total-pages}</i></div>"

# Add page numbers to the PDF
document.AddHtmlFooters(footer)

# Save the modified PDF
document.SaveAs("Modified.pdf")
Python

The image below shows the output result of placing the page number on the left side of the footer.

How to Add Page Numbers to PDF in Python: Figure 3 - Image with page number on the left side

Real-World Use Cases

Let us explore the many real-world examples and use cases where adding page numbers programmatically becomes invaluable.

Generating Reports

In business applications, automated report file generation is a common requirement. Adding page numbers ensures that reports are well-organized and professional.

Academic Documents

For academic institutions or researchers, for example, dynamically adding page numbers to research papers or academic documents streamlines the reading and referencing process.

In the legal domain, maintaining version control and ensuring proper file pagination and file naming is crucial. Automating the addition of page numbers simplifies the process.

E-books and Publications

When dealing with digital publications or e-books, providing a consistent reading experience is paramount. Page numbers and fonts all contribute to the overall readability and navigation of the document's pages.

Conclusion

In this detailed exploration article, we have delved into the intricate process of enhancing PDF document output by seamlessly incorporating page numbers using IronPDF in Python. The provided code takes a systematic approach to output, ensuring clarity of path, output, and ease of understanding.

It is important to note that this guide serves as a foundation, and developers are encouraged to tailor the provided code to specific requirements. Additionally, IronPDF offers a free trial for development purposes to evaluate before making a purchase. For more information on licensing details, advanced features, and support, visit the official IronPDF website.

As the landscape of PDF file manipulation tools continues to evolve, IronPDF stands out as a valuable asset for developers seeking to automate and enhance their PDF file and image-related workflows. If you want to explore more about IronPDF in Python, you can follow the link here for licensing details and more information.

If you are looking to add page numbers in the HTML after a page break, you can find an example here.

Happy PDF file coding!

Curtis Chau
Technical Writer

Curtis Chau holds a Bachelor’s degree in Computer Science (Carleton University) and specializes in front-end development with expertise in Node.js, TypeScript, JavaScript, and React. Passionate about crafting intuitive and aesthetically pleasing user interfaces, Curtis enjoys working with modern frameworks and creating well-structured, visually appealing manuals.

...
Read More

Related Articles

Key in blue circle

Get your free 30-day Trial Key instantly.

Your trial license will be sent to your email address

No limitations. 100% unlocked. No credit card.

OR
bullet_checkedNo credit card or account creation requiredNo limitations. 100% unlocked. No credit card.
  • Logo Aetna
  • Logo NASA
  • Logo GE
  • Logo Porsche
  • Logo USDA
  • Logo Qatar
Join Millions of Engineers who’ve tried Iron Suite
Book your free Live Demo
Booking Badge

Trusted by Millions of Engineers Worldwide

Iron Software's customer logos
Get Your No-Obligation Consult
Complete the form below or email sales@ironsoftware.com
Your details will always be kept confidential.
Trusted by Millions of Engineers Worldwide
Iron Software's customer logos
Get your free 30-day Trial Key instantly.
No credit card or account creation required
C# NuGet Library for PDF
Install with NuGet

Version: 2026.9

PM > Install-Package IronPdf
nuget.org/packages/IronPdf/
  1. In Solution Explorer, right-click References, Manage NuGet Packages
  2. Select Browse and search "IronPdf"
  3. Select the package and install
C# PDF DLL
Download DLL

Version: 2026.9

or download Windows Installer here.

  1. Download and unzip IronPDF to a location such as ~/Libs within your Solution directory
  2. In Visual Studio Solution Explorer, right click References. Select Browse, "IronPdf.dll"

Licenses from $999