This quick tutorial guides you on how to crop PDF pages using Python. It provides the IDE configuration, a step-by-step process to write this application, and a ready-to-run sample code to crop PDF page by page using Python. No third-party tool or other software is required to crop the PDF.
Steps to Crop PDF File using Python
- Set the environment to use Aspose.PDF for Python via .NET to crop a PDF
- Load the source PDF file into the Document class object to crop a page
- Display the existing dimensions of boxes like crop, trim, art, bleed, and media
- Create and set a new rectangle to change the dimensions of the above boxes
- Save the cropped PDF file on the disk
These steps summarize the process to crop PDF pages using Python. The guidance is provided right from starting the IDE configuration followed by the loading and viewing of the existing dimensions of the different boxes used to set the boundaries of multiple views and finally setting new dimensions of these boxes. This process can be applied on a single page at a time however you may iterate through all the pages to crop the complete PDF file.
Code to Crop PDF using Python
This code demonstrates the process to crop a page in PDF using Python. The page class contains the properties like crop_box for the size of the page displayed in Adobe Acrobat, trim_box for the final box size after printing, art_box for the box drawn around contents, bleed_box for the extent of color beyond the page, and media_box for the largest page box. All the existing properties are viewed here in terms of a rectangle and a new rectangle is set for all of them to observe the effect in different scenarios.
This article has taught us to crop one page from PDF using Python. If you want to learn the process to flatten a PDF file, refer to the article on how to flatten PDF in Python.