Skip to main content

Command Palette

Search for a command to run...

PDF Page Cropping via C#

Published
3 min readView as Markdown

In practical business scenarios, we often need to perform refined processing on PDF documents, among which page cropping is a common requirement. Whether it is to remove blank margins around document edges, extract specific content from pages, or adjust page sizes to meet different display needs, PDF page cropping plays an important role. This article will introduce how to implement PDF page cropping using a free library - Free Spire.PDF for .NET.

Before getting started, you can install Free Spire.PDF via the NuGet Package Manager in Visual Studio:

Install-Package FreeSpire.PDF

I. Core Methods of PDF Cropping

The following are several core objects and methods for implementing PDF cropping:

  • PdfDocument: Responsible for core operations such as loading, saving, and page management of PDF files, serving as the entry point for PDF processing.
  • PdfPageBase: Represents a single page in a PDF document.
  • CropBox property: Used to set or get the cropping area of a page. You can implement page cropping simply by assigning a RectangleF structure to it, without calling additional methods, which is concise and efficient.
  • PDF coordinate system: The origin of the coordinate system in Free Spire.PDF is located at the top-left corner of the page, with the X-axis extending to the right and the Y-axis extending downward.

Core Process of PDF Cropping:

Load the PDF → Get the target page → Assign the cropping area to the CropBox property → Save the PDF → Release resources.


II. Practical Cases: Implement Various PDF Page Cropping Requirements

The following demonstrates how to write C# code to implement PDF page cropping through 2 typical scenarios. All cases are based on console projects, and the code can be copied and run directly.

Scenario 1: Crop a Single PDF Page (Specify a Fixed Area)

Requirement: Load a PDF file, crop only the first page, and retain the specified core area of the page (e.g., remove redundant margins from the top, bottom, left, and right).

using System.Drawing;
using Spire.Pdf;

namespace CropPDFPage
{
    class Program
    {
        static void Main(string[] args)
        {
            // 1. Instantiate a PdfDocument object to carry and process the PDF document
            PdfDocument pdf = new PdfDocument();

            // 2. Load the PDF file to be cropped from the local disk (replace with your actual PDF file path)
            pdf.LoadFromFile("Sample.pdf");

            // 3. Get the first page of the PDF document (page index starts from 0, Pages[0] corresponds to the first page)
            PdfPageBase page = pdf.Pages[0];

            // 4. Assign a value to the CropBox property of the page to define the cropping area and implement page cropping
            // RectangleF(x: horizontal coordinate of the left boundary, y: vertical coordinate of the bottom boundary, width: width of the cropping area, height: height of the cropping area)
            page.CropBox = new RectangleF(0, 300, 600, 260);

            // 5. Save the cropped PDF file to the local disk
            pdf.SaveToFile("CropPDF.pdf");

            // 6. Close the PdfDocument object to release occupied memory resources and avoid memory leaks
            pdf.Close();
        }
    }
}

Scenario 2: Batch Crop All Pages of a PDF

Requirement: Apply the same cropping rule to all pages of a PDF file to batch remove redundant margins. This is suitable for PDF documents with a unified format (such as batch-generated reports and scanned copies).

using System.Drawing;
using Spire.Pdf;

namespace CropPDFPage
{
    class Program
    {
        static void Main(string[] args)
        {
            // 1. Instantiate a PdfDocument object and load the PDF
            PdfDocument pdf = new PdfDocument();
            pdf.LoadFromFile("Sample1.pdf");

            // 2. Define a unified cropping area (shared by all pages)
            RectangleF cropArea = new RectangleF(0, 300, 600, 260);

            // 3. Traverse all pages and apply the cropping rule in batches
            foreach (PdfPageBase page in pdf.Pages)
            {
                page.CropBox = cropArea;
            }

            // 4. Save the file and release resources
            pdf.SaveToFile("BatchCropPDF.pdf");
            pdf.Close();
        }
    }
}

This article demonstrates multiple methods for PDF page cropping in C# with just a few lines of code. From basic single-page cropping to batch unified cropping, these methods meet the needs of practical development scenarios and facilitate developers' PDF processing work.