USING IRONPDF FOR PYTHON

How to Add Page Numbers to PDF in Python

Published January 27, 2024
Share:

Introduction

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.

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.

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.

footer = HtmlHeaderFooter()
footer.MaxHeight = 15
footer.DrawDivideLine = True
footer.HtmlFragment = "<center><i>{page}</i></center>"
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.

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.

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 *

# Instantiate Renderer
renderer = ChromePdfRenderer()

# Load PDF document
document = PdfDocument.FromFile("example.pdf")
print(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 *

# Instantiate Renderer
renderer = ChromePdfRenderer()

# Load PDF document
document = PdfDocument.FromFile("example.pdf")
print(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 *

# Instantiate Renderer
renderer = ChromePdfRenderer()

# Load PDF document
document = PdfDocument.FromFile("example.pdf")
print(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!

< PREVIOUS
How to Create PDF Files with Text and Images in Python
NEXT >
How to Read Scanned PDFs in Python

Ready to get started? Version: 2024.11.1 just released

Free pip Install View Licenses >