Crop PDF via Python

PDF cropping programmatically using Aspose.PDF for Python for .NET Library

Crop PDF with Python

Do you need to crop PDF? Programmatic modification of PDF documents is an essential part of modern digital workflows. With Python libraries like Aspose.PDF – are stand-alone solutions that don’t rely on other software and are ready for commercial use. They cover all possible needs of professional Python developers.

This Python library enables developers to crop PDF documents quickly and efficiently through programming. With this library, you can easily crop PDF with a number of properties, such as the width, height, bleed-, crop- and trimbox. Aspose.PDF allows you to access these properties. It is a standalone solution for processing PDF documents in Python that does not require the installation of any third-party software.

In order to crop PDF file, we’ll use Aspose.PDF for .NET API which is a feature-rich, powerful and easy-to-use document manipulation API for Python via .NET platform. Open NuGet package manager, search for Aspose.PDF and install. You may also use the following command from the Package Manager Console.

Python Package Manager Console

pip install aspose-pdf

How to Crop PDF via Python


You need Aspose.PDF for Python to try the code in your environment.

  1. Open a PDF document using Document object.
  2. Choose the type of box and create new Box Rectagle
  3. Save the updated document to the new path using the Save() method.

If you want to crop a PDF page, a cropping solution can help. With this powerful Aspose.PDF Python for .NET library, you can change your PDF while keeping its quality. This professional tool uses Python to crop PDF effectively. The following example demonstrates how to modify a PDF document in Python:

Crop PDF Files - Python

This sample code shows how to Crop PDF Page - Python

Input file:

File not added

Output format:

Output file:


import aspose.pdf as ap

document = ap.Document(input_pdf)

# Create new Box Rectagle
new_box = ap.Rectangle(100, 100, 80, 10, True)
document.pages[1].crop_box = new_box
document.pages[1].trim_box = new_box
document.pages[1].art_box = new_box
document.pages[1].bleed_box = new_box

document.save(output_pdf)

About Aspose.PDF for Python API

Aspose.PDF for Python via .NET API supports most established PDF standards and PDF specifications. It allows developers to insert tables, graphs, images, hyperlinks, custom fonts - and more - into PDF documents. Moreover, it is also possible to crop PDF documents. Aspose.PDF for Python via .NET provides excellent security features to develop secure PDF documents. Some of the key features of Aspose.PDF for Python via .NET API include:

  • Ability to read & export PDF in multiple image formats including BMP, GIF, JPEG & PNG.
  • Set basic information (e.g. author, creator) of the PDF document.
  • Conversion Features: Convert PDF to Word, Excel, and PowerPoint. Convert PDF to Images formats. Convert PDF file to HTML format and vice versa. Convert PDF to EPUB, Text, XPS, etc.

You can find more information about Aspose.PDF for Python via .NET API on our documentation on how to use API.