How to Crop a PDF using C#

This short tutorial defines how to crop a PDF using C#. In shares the configuration details, the program flow, and a runnable sample code demonstrating a basic application to crop a page in PDF using C#. This sample code can be executed in any of the platforms supporting the .NET environment and does not require Acrobat or any other third-party tool for cropping some portion of a PDF file.

Steps to Crop PDF using C#

  1. Establish the environment to add Aspose.PDF for .NET to crop part of a page
  2. Load the source PDF file in the Document class object to crop it
  3. Display the existing PDF page boxes if required
  4. Create a new Rectangle object defining the crop area
  5. Set the CropBox, TrimBox, ArtBox, and BleedBox values to the newly created rectangle
  6. Save the PDF

These steps summarize the process to crop PDF document using C#. The process starts by loading the PDF file and optionally displaying the existing values for CropBox, TrimBox, ArtBox, BleedBox, and MediaBox. In the next steps, a new rectangle is initialized defining the area of the page that is to be cropped followed by setting this rectangle as a new parameter for all the PDF page boxes mentioned above.

Code to Crop PDF File using C#

This sample code can be used as a basis for developing an application to crop page in PDF using C#. You may set any of the PDF box values as per your need but to crop the portion of the source PDF and display it completely in the new PDF file page, set a new value for all of these boxes. You may also use the normalizeCoordinates flag to adjust the new rectangle dimensions if required.

This quick tutorial has guided us on how to crop PDF file using C#. If you want to learn the process to split a PDF file by pages, refer to the article on how to split PDF file by pages in C#.

 English