VOOZH about

URL: https://ironpdf.com/python/blog/using-ironpdf-for-python/python-add-page-numbers-to-pdf-tutorial/

⇱ Python Add Page numbers To PDF (Developer Tutorial)


Skip to footer content
  1. IronPDF for Python
  2. IronPDF for Python Blog
  3. Using IronPDF For Python
  4. Python Add Page numbers To PDF
USING IRONPDF FOR PYTHON

How to Add Page Numbers to PDF in Python

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()
# 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")
# Import PdfDocument from IronPDF
from ironpdf import PdfDocument

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

Creating an HTML Footer to Add Page Number

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
# 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)
# 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")
# 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")
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")
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")
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.

Legal Documents

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!

Frequently Asked Questions

How do I add page numbers to a PDF using Python?

You can add page numbers to a PDF in Python using IronPDF by creating an HTML footer with page number placeholders and applying it to your document with the AddHtmlFooters method.

What method can I use to render HTML and CSS to a PDF?

The ChromePdfRenderer class in IronPDF can be used to render HTML and CSS into a PDF, which is particularly useful for creating dynamic content within the PDF.

Can I customize the appearance of page numbers in a PDF footer?

Yes, using IronPDF, you can customize the appearance of page numbers in a PDF footer by styling them with HTML and CSS to align them as desired, such as center, left, or right.

How can I save a PDF with added page numbers in Python?

After adding page numbers using IronPDF, you can save the modified PDF by calling the SaveAs method and specifying your desired output file name.

What are some practical applications of adding page numbers to PDFs?

Adding page numbers to PDFs is useful for generating organized reports, academic papers, legal documents, and e-books, making it easier for readers to navigate and reference specific sections.

Is there a trial available for testing the PDF library's capabilities?

Yes, IronPDF offers a free trial for development purposes, allowing you to test its PDF manipulation capabilities before purchasing a license.

What should I do if I need to add page numbers after a page break?

You can find examples and documentation on adding page numbers after a page break using IronPDF's resources, which will guide you in implementing this feature.

How do I install IronPDF for use with Python?

IronPDF can be installed for Python by running the command pip install ironpdf in your command line interface.

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

Updated

Scrapy in Python (How It Works For Developers)

Here comes Scrapy, a web scraping framework in Python, and IronPDF, two formidable libraries that work together to optimize the extraction of online data and the creation of dynamic PDFs.

Read More

Updated

How to Use Python to Add Text to PDF file

This is where IronPDF for Python comes into play, providing strong tools to add text, annotations, and other components to PDF documents dynamically using programming

Read More

Install with pip
Version: 2026.6
 pip install ironpdf
  1. Download and install Python 3.7+.
  2. Install pip from pypi.org if it isn't installed already.
  3. Execute the above command in the terminal.
Download Module
Version: 2026.6
Download Now
Manually install into your project
  1. Download the package
  2. Run this command from the terminal
    pip install ironpdf-2026.6-py37-none-win_amd64.whi
Licenses from $749

Have a question? Get in touch with our development team.

Now you've installed with PyPi
Your browser is now downloading IronPDF

Next step: Start free 30-day Trial

No credit card required

  • Test in a live environment
  • Fully-functional product
  • 24/5 technical support

Thank You

Your trial key should be in the email.
If it is not, please contact
support@ironsoftware.com
Get your free 30-day Trial Key instantly.
Thank you.
If you'd like to speak to our licensing team:
πŸ‘ badge_greencheck_in_yellowcircle
The trial form was submitted
successfully.

Your trial key should be in the email.
If it is not, please contact
support@ironsoftware.com

Have a question? Get in touch with our development team.
No credit card or account creation required
Now you've installed with PyPi
Your browser is now downloading IronPDF

Next step: Start free 30-day Trial

No credit card required

  • Test in a live environment
  • Fully-functional product
  • 24/5 technical support
Thank you.
View your license options:
Thank you.
If you'd like to speak to our licensing team:
Have a question? Get in touch with our development team.
Have a question? Get in touch with our development team.
Talk to Sales Team

Book a No-obligation Consult

How we can help:
  • Consult on your workflow & pain points
  • See how other companies solve their .NET document needs
  • All your questions answered to make sure you have all the information you need. (No commitment whatsoever.)
  • Get a tailored quote for your project's needs
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
Book Free Live Demo

Book a 30-minute, personal demo.

No contract, no card details, no commitments.

Here's what to expect:
  • A live demo of our product and its key features
  • Get project specific feature recommendations
  • All your questions are answered to make sure you have all the information you need.
    (No commitment whatsoever.)
CHOOSE TIME
YOUR INFO
Book your free Live Demo

Trusted by Millions of Engineers Worldwide

Iron Support Team

We're online 24 hours, 5 days a week.
Chat
Email
Call Me