Change PDF Page Size in Python

Aspose.PDF for Python via .NET lets you change PDF page size with simple lines of code. This topic shows how to update page dimensions using the Document and Page APIs.

Use this guide when you need to resize existing PDF pages, normalize document dimensions, or inspect page box settings in Python.

Set the Page Size of a PDF Page to A4

The example updates the size of the first page in a PDF document to standard A4 dimensions. It also prints the page’s box dimensions (CropBox, TrimBox, ArtBox, BleedBox, MediaBox) before and after resizing so you can verify the changes.

The following code snippet shows how to change the PDF page dimensions to A4 size:

  1. Access the first Page of the Document.
  2. Display the page’s box sizes before modification (CropBox, TrimBox, ArtBox, BleedBox, MediaBox).
  3. Apply A4 dimensions (597.6 × 842.4 points) using the page API.
  4. Display the updated page box sizes.
  5. Save the modified Document to the specified output path.
import aspose.pdf as ap

def set_page_size(input_file_name, output_file_name):
    document = ap.Document(input_file_name)
    # Get particular page
    page = document.pages[1]

    # Set the page size as A4 (8.3 x 11.7 in) and in Aspose.Pdf, 1 inch = 72 points
    # So A4 dimensions in points will be (597.6, 842.4) for portrait orientation
    print("Before set")
    print(f"CropBox: {page.crop_box.width} x {page.crop_box.height}")
    print(f"TrimBox: {page.trim_box.width} x {page.trim_box.height}")
    print(f"ArtBox: {page.art_box.width} x {page.art_box.height}")
    print(f"BleedBox: {page.bleed_box.width} x {page.bleed_box.height}")
    print(f"MediaBox: {page.media_box.width} x {page.media_box.height}")

    page.set_page_size(597.6, 842.4)
    print("After set")
    print(f"CropBox: {page.crop_box.width} x {page.crop_box.height}")
    print(f"TrimBox: {page.trim_box.width} x {page.trim_box.height}")
    print(f"ArtBox: {page.art_box.width} x {page.art_box.height}")
    print(f"BleedBox: {page.bleed_box.width} x {page.bleed_box.height}")
    print(f"MediaBox: {page.media_box.width} x {page.media_box.height}")

    # Save the updated document
    document.save(output_file_name)

Get PDF Page Size

This snippet reads a PDF and retrieves the dimensions (width and height) of the first page. It uses the Page API to extract the page’s bounding Rectangle and prints its size to the console. This is useful for inspecting page layout, verifying formats, or preparing documents for further processing.

  1. Load the PDF as a Document.
  2. Access the first Page.
  3. Retrieve the page’s bounding rectangle using get_page_rect().
  4. Extract the width and height values.
  5. Print the page dimensions.
import aspose.pdf as ap

def get_page_size(input_file_name, output_file_name):
    document = ap.Document(input_file_name)

    # Get particular page
    page = document.pages[1]
    rectangle = page.get_page_rect(True)
    print(f"{rectangle.width} : {rectangle.height}")

Get PDF Page Size Before and After Rotation

Retrieve the dimensions of a PDF page before and after applying a 90° rotation. This demonstrates how rotation affects width and height and how to use get_page_rect() with or without rotation consideration.

  1. Open the PDF as a Document.
  2. Access the first Page.
  3. Apply a 90° rotation using page.rotate = ap.Rotation.ON90 (see the Rotation enum).
  4. Retrieve the page rectangle without rotation using get_page_rect(False) and print its width and height.
  5. Retrieve the page rectangle considering rotation using get_page_rect(True) and print its width and height.
  6. Compare how the dimensions change due to rotation.
import aspose.pdf as ap

def get_page_size_rotation(input_file_name, output_file_name):
    document = ap.Document(input_file_name)
    # Get particular page
    page = document.pages[1]
    page.rotate = ap.Rotation.ON90
    rectangle = page.get_page_rect(False)
    print(f"{rectangle.width} : {rectangle.height}")
    rectangle = page.get_page_rect(True)
    print(f"{rectangle.width} : {rectangle.height}")