How to Change Page Size of PDF using Python

This small topic focuses on how to change page size of PDF using Python. It encompasses all the necessary details to configure the development environment, a sequence of steps that are to be followed to create an application and a runnable sample code that exhibits the process to change size of PDF page using Python with the help of a few API calls only. This code can be used in any of the Python and .NET Framework supported environments like Windows, Linux, and macOS.

Steps to Change PDF Paper Size using Python

  1. Set up the environment for using the Aspose.PDF for Python via .NET API
  2. Open the source PDF file using the Document class object whose page dimensions are to be changed
  3. Access the PDF pages collection in the loaded PDF
  4. Access the desired page from the collection for which the size is to be updated
  5. Call the set_page_size() method of the Page class and provide a new page size
  6. Save the resultant PDF file with modified page size

These steps cover the process to edit PDF page size using Python by providing the resources that are required to test this feature. Initially, we load the target PDF file, access the page collection, and then get the reference to the page whose size is to be updated. The Page class instance contains a method set_page_size() that is used to modify the page size by providing the page width and height.

Code to Modify PDF Page Size using Python

This example code demonstrates the process to change the size of a PDF page using Python. The Aspose.PDF use a scaling factor of 72 points for 1 inch of the page resolution. Therefore, you have to calculate the width and height in points by multiplying the number of desired inches by 72. Suppose you want to set the page size to Envelop 10# which has a height of 9.49 inches and a width of 4.13 inches, hence you will multiply both numbers by 72 before passing it to the set_page_size() method.

This example has taught us to change PDF page dimensions using Python. If you are interested in learning about converting a PDF to an Excel file, refer to the article on how to convert PDF to Excel using Python.

 English