Change PDF Page Size with Python

Change PDF Page Size

Aspose.PDF for Python via .NET lets you change PDF page size with simple lines of code in your Python applications. This topic explains how to update/change the page dimensions (size) of an existing PDF file.

The Page class contains the set_page_size() method which lets you set the page size. The code snippet below updates page dimensions in a few easy steps:

  1. Load the source PDF file.
  2. Get the pages into the PageCollection object.
  3. Get a given page.
  4. Call the set_page_size() method to update its dimensions.
  5. Call the Document class save() method to generate the PDF file with updated page dimensions.

    import aspose.pdf as ap

    document = ap.Document(input_pdf)

    # Get particular page
    page = document.pages[1]

    # Set the page size as A4 (11.7 x 8.3 in) and in Aspose.Pdf, 1 inch = 72 points
    # So A4 dimensions in points will be (842.4, 597.6)
    page.set_page_size(597.6, 842.4)

    # Save the updated document
    document.save(output_pdf)