This how-to guide explains how to add page number in PDF using Python. It contains all the details to set the environment, a list of steps to write the application, and a runnable sample code to put page number in PDF using Python. Different configurations are discussed to place the page number at different locations on the page having custom formatting.
Steps to Insert Page Number in PDF using Python
- Set the environment to use Aspose.PDF for Python via .NET to add page numbers
- Open the PDF file using the Document class object to insert the page number
- Create the stamp for the page number using the PageNumberStamp object
- Set the stamp format to be displayed while adding the page number
- Set the stamp display properties
- Loop through all the pages and add a stamp on each page
- Save the resultant PDF file having the page number in it
These steps describe the process to add page numbers to PDF file using Python where the process is commenced by loading the target PDF file followed by creating an abject of PageNumberStamp object that is actually used for adding page numbers. In the next steps, the formatting of the page number display is set along with the display properties of the stamp. In the final step, the stamp is added to all or selected pages in the currently loaded PDF file.
Code to Add Page Number in PDF using Python
This code demonstrates the process to insert page numbers in PDF document using Python. It uses PageNumberStamp class to set a variety of properties for example the format property is used to set the style and contents while displaying the page number, the location of adding the page number using the margins, alignment, starting page number, and PageNumberStamp.text_state to set the fonts. By default, the page number is added at the bottom of the page however you may use VerticalAlignment.TOP property to display the page number at the top of the page.
In this article, we have learned the process to add pagination to PDF using Python. If you want to learn the process to add a watermark to the PDF, refer to the article on how to add watermark to PDF in Python.