This simple topic focuses on how to crop a PDF using Python. It encompasses the configuration details, the program flow, and a working sample code demonstrating an application to crop a page in PDF using Python. This sample code can be used in any of the Operating Systems supporting the Python and .NET environments and does not require Acrobat or any other third-party tool for cropping some portion of a PDF file.
Steps to Crop PDF using Python
- Establish the environment to use Aspose.PDF for Python via .NET to crop part of a page
- Access the sample PDF file using the Document class object to crop it
- Display the PDF page boxes values (if required)
- Instantiate the Rectangle class object by defining the crop area
- Assign the cropped rectangle to CropBox, TrimBox, ArtBox, and BleedBox
- Save the PDF with the cropped area on the disk
The above steps entail the procedure to crop PDF document using Python. The process is initiated by accessing the source PDF file and demonstrating the existing values for CropBox, TrimBox, ArtBox, BleedBox, and MediaBo inside the console. In the subsequent steps, a cropped area is defined using an instance of the Rectangle class and setting that as a new parameter for all the PDF page boxes specified above.
Code to Crop PDF File using Python
This example code can be utilized as a basic application to crop page in PDF using Python. You can set any PDF box value as per requirement but in order to crop the portion of the source PDF and display it completely in the new PDF file page, apply the new value for all mentioned boxes. The normalizeCoordinates flag can be used to adjust the new rectangle dimensions if required.
In this topic, we have focused on how to crop PDF file using Python. If you are interested in learning about modifying PDF metadata, refer to the article on how to change PDF Metadata using Python.