How to Crop a PDF using Java

This simple topic defines how to crop a PDF using Java. It encompasses the environment configuration details, the program workflow, and a working sample code exhibiting a basic application to crop a page in PDF using Java. This example can be used in any of the Java supported environments and does not rely on Adobe Acrobat or any other third-party tool for cropping desired portion of a PDF file.

Steps to Crop PDF using Java

  1. Configure the environment to add Aspose.PDF for Java to crop part of a page
  2. Access the source PDF file using the Document class object to crop it
  3. Display the existing PDF page boxes (if required) inside the console
  4. Create an instance of the Rectangle class object and define the crop area
  5. Apply the created Rectangle value to CropBox, TrimBox, ArtBox, and BleedBox
  6. Save the cropped PDF

The aforementioned steps entail the process to crop PDF document using Java. The process is commenced by accessing the PDF file and optionally outputting the existing values for TrimBox, CropBox, BleedBox, ArtBox, and MediaBox. In the following steps, a new rectangle is created by defining the area on the page that is to be cropped followed by setting this rectangle as an argument for all the PDF page boxes mentioned above.

Code to Crop PDF File using Java

The above example can be referred to as a basis for writing an application to crop page in PDF using Java. You can configure any number of the PDF box values as required but set a new value for all of these boxes to crop the selected portion of the source PDF and display it completely in the new PDF file page. You can also utilize the normalizeCoordinates flag to modify the new rectangle dimensions if required.

In this simple topic, we learned how to crop PDF file using Java. If you want to learn the process change the PDF meta data, refer to the article on how to change PDF Metadata using Java.

 English