Crop PDF via C#

PDF cropping programmatically using Aspose.PDF for .NET Library

Crop PDF with C#

Do you need to crop PDF? Programmatic modification of PDF documents is an essential part of modern digital workflows. With .NET 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 C# developers.

This C# 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 C# 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 .NET platform. Open NuGet package manager, search for Aspose.PDF and install. You may also use the following command from the Package Manager Console.

Package Manager Console

PM > Install-Package Aspose.PDF

How to Crop PDF via C#


You need Aspose.PDF for .NET 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 .NET library, you can change your PDF while keeping its quality. This professional tool uses C# to crop PDF effectively. The following example demonstrates how to modify a PDF document in C#:

Crop PDF Files - C#

This sample code shows how to Crop PDF Page - C#

Input file:

File not added

Output format:

Output file:


public static void CropPagesPDF()
{
    var pdfDocument1 = new Aspose.Pdf.Document("crop_page.pdf");
    Console.WriteLine(pdfDocument1.Pages[1].CropBox);
    Console.WriteLine(pdfDocument1.Pages[1].TrimBox);
    Console.WriteLine(pdfDocument1.Pages[1].ArtBox);
    Console.WriteLine(pdfDocument1.Pages[1].BleedBox);
    Console.WriteLine(pdfDocument1.Pages[1].MediaBox);

    // Create new Box Rectagle
    var newBox = new Rectangle(100, 100, 80, 10);
    pdfDocument1.Pages[1].CropBox = newBox;
    pdfDocument1.Pages[1].TrimBox = newBox;
    pdfDocument1.Pages[1].ArtBox = newBox;
    pdfDocument1.Pages[1].BleedBox = newBox;
   
    pdfDocument1.Save("crop_page_modified.pdf");           
}

About Aspose.PDF for C# API

Aspose.PDF for C# API provides a wide range of features for working with PDF files. Some of the features include:

  • Create PDF documents from scratch or from HTML, XML, or images.
  • Crop existing PDF documents by adding or removing pages, text, images, and other content.
  • Convert PDF documents to other formats such as HTML, XML, and images.
  • Render PDF documents to images or XPS format.
  • Print PDF documents directly from your application.
  • Digitally sign PDF documents.

You can find more information on Aspose.PDF for C# API in this Aspose documentation