Resize PDF Page Size Using Python (Standard and Custom Dimensions)


When working with PDF files, you may find that the original page size doesn’t match your reading habits or display requirements. To ensure a better viewing experience, it's often necessary to adjust the page size to suit different scenarios. With Python’s flexibility and the powerful Spire.PDF for Python library, you can easily resize PDF pages in bulk—whether you're setting them to a standard paper size or customizing the dimensions to meet specific needs.
First, make sure the Spire.PDF for Python library is installed in your project. You can do this using the following pip command:
pip install Spire.PDF
Resize PDF Pages to Standard Sizes
Spire.PDF for Python resizes PDF pages by copying content from the original document onto pages of a new PDF with the desired dimensions. Here's how it works:
Load the original PDF and create a new document
Loop through each page of the original PDF
Add a new page with the target size to the new PDF
Create a template based on the original page content
Draw the template onto the new page
Save the new PDF document
Code example - resetting PDF page size to A4:
from spire.pdf.common import *
from spire.pdf import *
# Load the original PDF document
originalPdf = PdfDocument()
originalPdf.LoadFromFile("E:/Administrator/Python1/input/AI-Generated Art_images.pdf")
# Create a new PDF document
newPdf = PdfDocument()
# Iterate through each page in the original PDF document
for i in range(originalPdf.Pages.Count):
page = originalPdf.Pages.get_Item(i)
# Add a new A4-sized page to the new PDF document
newPage = newPdf.Pages.Add(PdfPageSize.A4(), PdfMargins(0.0))
# Create a PdfTextLayout instance
layout = PdfTextLayout()
# Set the text layout to one page to ensure content fits correctly
layout.Layout = PdfLayoutType.OnePage
# Create a template based on the original PDF page
template = page.CreateTemplate()
# Draw the template onto the new page in the new PDF document
template.Draw(newPage, PointF.Empty(), layout)
# Save the new PDF document
newPdf.SaveToFile("/output/Adjusted_PDF_to_A4_Size.pdf")
newPdf.Close()
Resize PDF page sizes result preview:
Resize PDF Pages to Custom Sizes
In some cases, standard page sizes may not meet specific project requirements or display constraints. That’s when customizing the PDF page size becomes essential. Using Python combined with the powerful Spire.PDF for Python library, you can easily resize PDF pages to any custom dimensions you need. This flexibility allows you to tailor your documents precisely to your target format, whether for print, digital display, or other specialized uses. Below is a practical example demonstrating how to set custom page sizes by converting measurements and applying them to each page in a PDF file.
Code example - reset the PDF page size to 180mm x 225mm:
from spire.pdf.common import *
from spire.pdf import *
# Load the original PDF document
originalPdf = PdfDocument()
originalPdf.LoadFromFile("/input/AI-Generated Art_images.pdf")
# Create a new PDF document
newPdf = PdfDocument()
# Create an instance of the PdfUnitConvertor class to convert between measurement units
unitCvtr = PdfUnitConvertor()
# Convert custom size dimensions from millimeters to points
width = unitCvtr.ConvertUnits(180, PdfGraphicsUnit.Millimeter, PdfGraphicsUnit.Point)
height = unitCvtr.ConvertUnits(225, PdfGraphicsUnit.Millimeter, PdfGraphicsUnit.Point)
# Create a SizeF object with custom width and height
size = SizeF(width, height)
# Iterate through each page in the original PDF document
for i in range(originalPdf.Pages.Count):
page = originalPdf.Pages.get_Item(i)
# Add a new page with custom size (180mm x 225mm) to the new PDF document
newPage = newPdf.Pages.Add(size, PdfMargins(0.0))
# Create a PdfTextLayout instance
layout = PdfTextLayout()
# Set the text layout to one page to ensure content fits correctly
layout.Layout = PdfLayoutType.OnePage
# Create a template based on the original PDF page
template = page.CreateTemplate()
# Draw the template onto the new page in the new PDF document
template.Draw(newPage, PointF.Empty(), layout)
# Save the new PDF document
newPdf.SaveToFile("/output/customizepagesize.pdf")
newPdf.Close()
Resize PDF page sizes result preview:
Detailed steps explained:
Load the original PDF and create a new document
Create an instance of the unit converter to convert dimensions from millimeters to points
Convert the custom width and height from millimeters to points
Define the custom page size using the converted dimensions
Loop through each page of the original PDF
Add a new page with the custom size to the new PDF document
Create a template based on the original page content
Draw the template onto the new page
Save the new PDF document
The Conclusion
Resizing PDF pages to standard or custom sizes using Python and Spire.PDF is a straightforward way to tailor your documents for various needs. Whether you’re preparing files for printing or digital display, these techniques offer flexibility and control. For more step-by-step tutorials and practical tips, visit my homepage. If you have any questions or suggestions, feel free to leave a comment—we’d love to hear from you and help you with your PDF processing tasks!
Subscribe to my newsletter
Read articles from Casie Liu directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
