Adding Page Number to PDF with Python

All the documents must have page numbers in it. The page number makes it easier for the reader to locate different parts of the document.

Aspose.PDF for Python via .NET allows you to add page numbers with PageNumberStamp.

Adding Page Number Stamp to a PDF

Add dynamic page number stamps to a PDF document using Aspose.PDF for Python. The PageNumberStamp object allows you to automatically display the current page number along with the total number of pages. The example shows how to create a page number stamp, customize its appearance (font, size, style, color, alignment, and margins), and apply it to a specific page in the PDF. This functionality is useful for generating professional, numbered documents and automating pagination in PDF workflows.

  1. Open the PDF document.
  2. Create a page number stamp.
  3. Set stamp properties.
  4. Customize text style.
  5. Apply the stamp to a page.
  6. Save the modified PDF.

import os
import aspose.pdf as ap

# Global configuration
DATA_DIR = "your path here"

def add_page_num_stamp(input_file_name, output_file_name):
    # Open document
    document = ap.Document(input_file_name)

    # Create page number stamp
    page_number_stamp = ap.PageNumberStamp()
    # Whether the stamp is background
    page_number_stamp.background = False
    page_number_stamp.format = "Page # of " + str(len(document.pages))
    page_number_stamp.bottom_margin = 10
    page_number_stamp.horizontal_alignment = ap.HorizontalAlignment.CENTER
    page_number_stamp.starting_number = 1
    # Set text properties
    page_number_stamp.text_state.font = ap.text.FontRepository.find_font("Arial")
    page_number_stamp.text_state.font_size = 14.0
    page_number_stamp.text_state.font_style = ap.text.FontStyles.BOLD
    page_number_stamp.text_state.font_style = ap.text.FontStyles.ITALIC
    page_number_stamp.text_state.foreground_color = ap.Color.blue_violet

    # Add stamp to particular page
    document.pages[1].add_stamp(page_number_stamp)

    # Save output document
    document.save(output_file_name)

Adding Roman Numeral Page Numbers to a PDF

Add page numbers in Roman numeral format to all pages of a PDF document. The page numbers are added as stamps, with customizable font, size, style, color, and alignment. The numbering can also start from any specified value.

  1. Open the PDF document.
  2. Create a page number stamp.
  3. Configure stamp properties.
  4. Set text appearance.
  5. Apply the stamp to all pages.
  6. Save the modified PDF.

import os
import aspose.pdf as ap

# Global configuration
DATA_DIR = "your path here"

def add_page_num_stamp_roman(input_file_name, output_file_name):
    # Open document
    document = ap.Document(input_file_name)

    # Create page number stamp
    page_number_stamp = ap.PageNumberStamp()
    # Whether the stamp is background
    page_number_stamp.background = False
    page_number_stamp.bottom_margin = 10
    page_number_stamp.horizontal_alignment = ap.HorizontalAlignment.CENTER
    page_number_stamp.starting_number = 42
    page_number_stamp.numbering_style = ap.NumberingStyle.NUMERALS_ROMAN_UPPERCASE

    # Set text properties
    page_number_stamp.text_state.font = ap.text.FontRepository.find_font("Arial")
    page_number_stamp.text_state.font_size = 14.0
    page_number_stamp.text_state.font_style = ap.text.FontStyles.BOLD
    page_number_stamp.text_state.foreground_color = ap.Color.blue_violet

    # Add stamp to particular page
    for page in document.pages:
        page.add_stamp(page_number_stamp)

    # Save output document
    document.save(output_file_name)

Live Example

Add PDF page numbers is an online free web application that allows you to investigate how adding page numbers functionality works.

How to add page number in pdf using Python