Skip to footer content
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
pip install ironpdf
SHELL

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

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.

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

What is a tool that helps in PDF manipulation with Python?

IronPDF is a powerful Python library that simplifies working with PDF files programmatically. It allows developers to easily manipulate PDFs, adding features such as page numbers, fonts, and images.

How do I install a library for PDF manipulation in Python?

You can install IronPDF by executing the command: `pip install ironpdf`.

How can I add page numbers to a PDF using a Python library?

To add page numbers to a PDF using IronPDF, you create an HTML footer with page number placeholders and use the `AddHtmlFooters` method to integrate it into your PDF document.

What is the purpose of a class that facilitates rendering HTML and CSS to PDF in a Python PDF library?

The `ChromePdfRenderer` class in IronPDF facilitates the rendering of HTML and CSS to output a PDF file, serving as a crucial component for preparing PDFs for further manipulation.

Can I customize the position of page numbers in a PDF footer using a Python library?

Yes, you can customize the position of page numbers in the PDF footer using HTML and CSS. For instance, you can align page numbers to the center, right, or left using appropriate HTML tags and styles.

What are some real-world use cases for adding page numbers to PDFs programmatically?

Real-world use cases include generating reports, academic documents, legal documents, and e-books where adding page numbers ensures organization, professionalism, and ease of navigation.

How do I save a modified PDF with added page numbers using a Python library?

You can save a modified PDF with added page numbers using the `SaveAs` method, specifying the output file name.

Is there a way to test a Python PDF library before purchasing?

Yes, IronPDF offers a free trial for development purposes, allowing you to evaluate its features before making a purchase.

What should I do if I want to add page numbers in HTML after a page break?

You can find an example of adding page numbers in HTML after a page break on the IronPDF website under their examples section.

Chaknith Bin
Software Engineer
Chaknith works on IronXL and IronBarcode. He has deep expertise in C# and .NET, helping improve the software and support customers. His insights from user interactions contribute to better products, documentation, and overall experience.
Talk to an Expert Five Star Trust Score Rating

Ready to Get Started?