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:
- Access the first
Pageof theDocument. - Display the page’s box sizes before modification (CropBox, TrimBox, ArtBox, BleedBox, MediaBox).
- Apply A4 dimensions (597.6 × 842.4 points) using the page API.
- Display the updated page box sizes.
- Save the modified
Documentto 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.
- Load the PDF as a
Document. - Access the first
Page. - Retrieve the page’s bounding rectangle using
get_page_rect(). - Extract the width and height values.
- 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.
- Open the PDF as a
Document. - Access the first
Page. - Apply a 90° rotation using
page.rotate = ap.Rotation.ON90(see theRotationenum). - Retrieve the page rectangle without rotation using
get_page_rect(False)and print its width and height. - Retrieve the page rectangle considering rotation using
get_page_rect(True)and print its width and height. - 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}")