Changing Page Size with Python
Aspose.PDF for Python via .NET lets you change PDF page size with simple lines of code in your .NET applications. This topic explains how to update/change the page dimensions (size) of an existing PDF file.
Set the Page Size of a PDF Page to A4
Python library 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, allowing you to verify the changes.
The following code snippet shows how to change the PDF page dimensions to A4 size:
- Access the first page of the document.
- Display the page’s box sizes before modification.
- Apply A4 dimensions (597.6 × 842.4 points).
- Display the updated page box sizes.
- Save the modified PDF to the specified output path.
import os
import aspose.pdf as ap
# Global configuration
DATA_DIR = "your path here"
def set_page_size(input_file_name, output_file_name):
"""
Set the size of the first page in the PDF document to A4 and save the updated document.
Parameters:
- input_file_name (str): Path to the input PDF file.
- output_file_name (str): Path to save the output PDF file.
"""
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 code snippet reads a PDF document and retrieves the dimensions (width and height) of the first page. It uses Aspose.PDF for Python to extract the page’s bounding rectangle and prints its size to the console. This is useful for inspecting page layout, verifying page formats, or preparing documents for further processing.
- Load the PDF document from the given file path.
- Access the first page of the document.
- Retrieve the page’s bounding rectangle using ‘get_page_rect()’.
- Extract the width and height values.
- Print the page dimensions to the console.
import os
import aspose.pdf as ap
# Global configuration
DATA_DIR = "your path here"
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. It shows how page rotation affects the width and height measurements and how to use ‘get_page_rect()’ with or without rotation consideration.
- Open the PDF document using ‘ap.Document()’.
- Access the specific page - first page in this example.
- Apply a 90° rotation using ‘page.rotate = ap.Rotation.ON90’.
- Retrieve the page rectangle without considering rotation using ‘get_page_rect(False)’ and print its width and height.
- Get the page rectangle considering rotation using ‘get_page_rect(True)’ and print its width and height.
- Analyze how the dimensions change due to rotation.
import os
import aspose.pdf as ap
# Global configuration
DATA_DIR = "your path here"
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}")