Add Page Numbers to PDF Using Python

Alexander Stock
6 min readMar 7, 2024

--

Adding page numbers to a PDF document is a common requirement in various scenarios, such as creating professional reports, generating academic papers, or organizing large documents. Page numbers provide navigation and reference points for readers, making it easier to locate specific content within the document. By adding page numbers, you can enhance the overall readability and usability of your PDF files.

In this article, I’ll walk you through the process of adding page numbers to a PDF document in Python using the Spire.PDF for Python library.

Python PDF Library for Adding Page Numbers

Spire.PDF for Python is a robust library designed for PDF document manipulation in Python applications. It offers user-friendly classes like PdfPageNumberField and PdfPageCountField, which allow users to retrieve the current page number and the total page count. To generate a “Page X of Y” format, you can utilize a PdfCompositeField object to combine multiple automatic fields into a single field.

After the PdfCompositeField object is created, you can set its position on the page using the Location property and then add it to the page using the Draw() method. The positioning of the page number is determined by specific coordinates, so it’s advisable to familiarize yourself with the coordinate system as well.

Coordinate System on a PDF Page

When working with Spire.PDF for Python to modify a PDF document, the coordinate system’s origin is located at the top left corner of the page. The x-axis extends to the right, and the y-axis extends downward.

Typically, page numbers are placed in the header or footer area. Therefore, it’s necessary to take into account the page size and margins when determining the position for placing the page numbers.

PDF coordinate system.
Figure 1. PDF Coordinate System

Install Library and Get a Free Trial License

The library can be easily installed from PyPI using the following pip command.

pip install Spire.PDF

It is worth mentioning that Spire.PDF for Python is a commercial library and has some limitations when dealing with PDF documents longer than 10 pages. To get rid of the limitations, you can request a 30-day free trial license here.

Add Left-Aligned Page Numbers to a PDF Document’s Footer Using Python

This example illustrates how to add page numbers to the left side of a PDF document’s footer, as well as adding a horizontal line across the footer section.

from spire.pdf.common import *
from spire.pdf import *

# Apply your license key
License.SetLicenseKey("your license key")

# Create a PdfDocument object
doc = PdfDocument()

# Load a PDF file
doc.LoadFromFile("C:\\Users\\Administrator\\Desktop\\Input.pdf")

# Create font, brush and pen, which determine the appearance of the page numbers to be added
font = PdfTrueTypeFont("Times New Roman", 12.0, PdfFontStyle.Regular, True)
brush = PdfBrushes.get_Black()
pen = PdfPen(brush, 1.0)

# Create a PdfPageNumberField object and a PdfPageCountField object
pageNumberField = PdfPageNumberField()
pageCountField = PdfPageCountField()

# Create a PdfCompositeField object to combine page count field and page number field in a single field
compositeField = PdfCompositeField(font, brush, "Page {0} of {1}", [pageNumberField, pageCountField])

# Get the page size
pageSize = doc.Pages[0].Size

# Set the location of the composite field
compositeField.Location = PointF(72.0, pageSize.Height - 45.0)

# Iterate through the pages in the document
for i in range(doc.Pages.Count):

# Get a specific page
page = doc.Pages[i]

# Draw a line at the specified position
page.Canvas.DrawLine(pen, 72.0, pageSize.Height - 50.0, pageSize.Width - 72.0, pageSize.Height - 50.0)

# Draw the composite field on the page
compositeField.Draw(page.Canvas, 0.0, 0.0)

# Save to a different PDF file
doc.SaveToFile("Output/AddPageNumbersToLeft.pdf")

# Dispose resources
doc.Dispose()
A PDF page contain a page number at the left side of footer.
Figure 2. Left-Aligned Page Number

Add Center-Aligned Page Numbers to a PDF Document’s Footer Using Python

This example demonstrates how to add page numbers to the center of a PDF document’s footer and include a horizontal line across the footer section.

from spire.pdf.common import *
from spire.pdf import *

# Apply your license key
License.SetLicenseKey("your license key")

# Create a PdfDocument object
doc = PdfDocument()

# Load a PDF file
doc.LoadFromFile("C:\\Users\\Administrator\\Desktop\\Input.pdf")

# Create font, brush and pen
font = PdfTrueTypeFont("Times New Roman", 12.0, PdfFontStyle.Regular, True)
brush = PdfBrushes.get_Black()
pen = PdfPen(brush, 1.0)

# Create a PdfPageNumberField object and a PdfPageCountField object
pageNumberField = PdfPageNumberField()
pageCountField = PdfPageCountField()

# Create a PdfCompositeField object to combine page count field and page number field in a single field
compositeField = PdfCompositeField(font, brush, "Page {0} of {1}", [pageNumberField, pageCountField])

# Iterate through the pages in the document
for i in range(doc.Pages.Count):

# Get a specific page
page = doc.Pages[i]

# Get the page size
pageSize = doc.Pages[i].Size

# Draw a line at the specified position
page.Canvas.DrawLine(pen, 72.0, pageSize.Height - 50.0, pageSize.Width - 72.0, pageSize.Height - 50.0)

# Measure the size the "Page X of Y"
pageNumberSize = font.MeasureString("Page {} of {}".format(i + 1, doc.Pages.Count))

# Set the location of the composite field
compositeField.Location = PointF((pageSize.Width - pageNumberSize.Width)/2, pageSize.Height - 45.0)

# Draw the composite field on the page
compositeField.Draw(page.Canvas, 0.0, 0.0)

# Save to a different PDF file
doc.SaveToFile("Output/AddPageNumbersToCenter.pdf")

# Dispose resources
doc.Dispose()
A PDF page contain a page number at the center of footer.
Figure 3. Center-Aligned Page Number

Add Right-Aligned Page Numbers to a PDF Document’s Footer Using Python

This example shows you how to add page numbers to the right side of a PDF document’s footer, as well as adding a horizontal line across the footer section.

from spire.pdf.common import *
from spire.pdf import *

# Apply your license key
License.SetLicenseKey("your license key")

# Create a PdfDocument object
doc = PdfDocument()

# Load a PDF file
doc.LoadFromFile("C:\\Users\\Administrator\\Desktop\\Input.pdf")

# Create font, brush and pen
font = PdfTrueTypeFont("Times New Roman", 12.0, PdfFontStyle.Regular, True)
brush = PdfBrushes.get_Black()
pen = PdfPen(brush, 1.0)

# Create a PdfPageNumberField object and a PdfPageCountField object
pageNumberField = PdfPageNumberField()
pageCountField = PdfPageCountField()

# Create a PdfCompositeField object to combine page count field and page number field in a single field
compositeField = PdfCompositeField(font, brush, "Page {0} of {1}", [pageNumberField, pageCountField])

# Iterate through the pages in the document
for i in range(doc.Pages.Count):

# Get a specific page
page = doc.Pages[i]

# Get the page size
pageSize = doc.Pages[i].Size

# Draw a line at the specified position
page.Canvas.DrawLine(pen, 72.0, pageSize.Height - 50.0, pageSize.Width - 72.0, pageSize.Height - 50.0)

# Measure the size the "Page X of Y"
pageNumberSize = font.MeasureString("Page {} of {}".format(i + 1, doc.Pages.Count))

# Set the location of the composite field
compositeField.Location = PointF(pageSize.Width - pageNumberSize.Width - 72.0, pageSize.Height - 45.0)

# Draw the composite field on the page
compositeField.Draw(page.Canvas, 0.0, 0.0)

# Save to a different PDF file
doc.SaveToFile("Output/AddPageNumbersToRight.pdf")

# Dispose resources
doc.Dispose()
Figure 4. Right-Aligned Page Number

Conclusion

In this blog post, we’ve explored how to add “Page X of Y” to a PDF document at the left footer, center footer, and right footer using Python. Additionally, we have discussed the PDF coordinate system, which enables you to position the page number at any desired location on a page. I hope you find this article to be informative and valuable.

See Also

Add Watermarks to PDF in Python

Extract Images from PDF in Python

Extract Text from PDF in Python

--

--

Alexander Stock

I'm Alexander Stock, a software development consultant and blogger with 10+ years' experience. Specializing in office document tools and knowledge introduction.