Change PDF Page Size and Scale Content Accordingly with Python

Alice Yang
4 min readMar 21, 2024

--

Change PDF Page Size with Python
Change PDF Page Size with Python

Page size plays a crucial role in determining the visual layout, formatting, and overall presentation of the content within a PDF file. Whether you need to adjust the page size to meet specific printing requirements, optimize the document for different devices and screen sizes, or ensure consistency across multiple PDF files, knowing how to effectively modify the page size is essential. In this article, we will explore how to change page size or resize pages of PDF documents and scale content accordingly using Python.

We will discuss the following topics:

Python Library to Change PDF Page Size

To change the page size of PDF documents with Python, we can use the Spire.PDF for Python library.

Spire.PDF for Python is a feature-rich and user-friendly library that enables creating, reading, editing, and converting PDF files within Python applications. With this library, you can perform a wide range of manipulations on PDFs, including adding text or images, extracting text or images, adding digital signatures, adding or deleting pages, merging or splitting PDFs, creating bookmarks, adding text or image watermarks, inserting fillable forms and many more. In addition, you are also able to convert PDF files to various file formats, such as Word, Excel, images, HTML, SVG, XPS, OFD, PCL, and PostScript.

You can install Spire.PDF for Python from PyPI using the following pip command:

pip install Spire.Pdf

For more detailed information about the installation, you can check this official documentation: How to Install Spire.PDF for Python in VS Code.

Change PDF Page Size to a Standard Page Size with Python

Adjusting the page size of a PDF document to a standard size, such as A4 (8.27" x 11.69"), A3 (11.69" x 16.54"), Letter (8.5" x 11"), or Legal (8.5" x 14"), is a common requirement in various scenarios. This process can be useful when you need to prepare a PDF for printing on a standard paper size or ensure consistency with other documents that adhere to standard page dimensions.

Here is a simple example that shows how to change the page size of a PDF document from A4 to A3 and scale the content accordingly using Python and Spire.PDF for Python:

from spire.pdf.common import *
from spire.pdf import *

# Load a PDF document
origin_doc = PdfDocument("A4.pdf")

# Create a new PDF document
new_doc = PdfDocument()

# Iterate through each page of the loaded document
for i in range(origin_doc.Pages.Count):
# Get the current page
page = origin_doc.Pages[i]

# Add a new page with A3 size and no margins to the new PDF document
newPage = new_doc.Pages.Add(PdfPageSize.A3(), PdfMargins(0))

# Create a PdfTextLayout object
layout = PdfTextLayout()
# Set content layout type as one page to automatically scale content according to page size
layout.Layout = PdfLayoutType.OnePage

# Draw the page content of the current page on the new page
page.CreateTemplate().Draw(newPage, PointF.Empty(), layout)

# Save the new PDF document
new_doc.SaveToFile("A3.pdf")
origin_doc.Close()
new_doc.Close()

You can easily adjust the code to change the page size of your PDF document to many other standard page sizes like A1, A2, B1, B2, etc. according to your requirements.

Change PDF Page Size to a Custom Page Size with Python

When changing the PDF page size to a custom page size, you have the flexibility to define specific dimensions that meet your unique requirements. This allows you to create documents with non-standard or unconventional page sizes tailored to your specific needs.

Before delving into the code, it’s important to understand that Spire.PDF for Python employs the point as the default measurement unit. If you intend to modify the page size of your document to a custom dimension using alternative units of measurement like pixels, inches, centimeters, or millimeters, you can utilize the PdfUnitConvertor class provided by Spire.PDF. This class allows you to easily convert values expressed in these alternative units to the corresponding equivalent value in points.

Here is a simple example that shows how to change the page size of a PDF document to a custom page size and scale the content accordingly using Python and Spire.PDF for Python:

from spire.pdf.common import *
from spire.pdf import *

# Load a PDF document
origin_doc = PdfDocument("A4.pdf")

# Create a new PDF document
new_doc = PdfDocument()

# Create a PdfUnitConvertor object to convert between different measurement units
unit_converter = PdfUnitConvertor()

# Convert the custom page size from values in inches (8.0 x 8.0 in) to values in points
width = unit_converter.ConvertUnits(8.0, PdfGraphicsUnit.Inch, PdfGraphicsUnit.Point)
height = unit_converter.ConvertUnits(8.0, PdfGraphicsUnit.Inch, PdfGraphicsUnit.Point)
custom_size = SizeF(width, height)

# Iterate through each page of the loaded document
for i in range(origin_doc.Pages.Count):
# Get the current page
page = origin_doc.Pages[i]

# Add a new page with the custom size and no margins to the new PDF document
newPage = new_doc.Pages.Add(custom_size, PdfMargins(0))

# Create a PdfTextLayout object
layout = PdfTextLayout()
# Set content layout type as one page to automatically scale content according to page size
layout.Layout = PdfLayoutType.OnePage

# Draw the page content of the current page on the new page
page.CreateTemplate().Draw(newPage, PointF.Empty(), layout)

# Save the new PDF document
new_doc.SaveToFile("CustomPageSize.pdf")
origin_doc.Close()
new_doc.Close()

Conclusion

This article demonstrated how to change PDF page size to a standard or a custom page size and scale the content accordingly using Python. We hope you find it helpful.

Related Topics

--

--

Alice Yang

Skilled senior software developers with five years of experience in all phases of software development life cycle using .NET, Java and C++ languages.