When working with PDF documents in C# applications, it is common to encounter situations where only a specific part of a page is needed. The document may contain extra margins, headers, or irrelevant content that needs to be removed for better presentation or processing.
To handle this scenario, you can crop PDF pages programmatically using Spire.PDF for .NET. This allows developers to define a visible region of a page and exclude unnecessary areas without modifying the original content structure.
In this article, we will demonstrate how to crop PDF pages in C# and also show how to export the cropped result as an image.
Crop PDF Page in C#
In Spire.PDF for .NET, page cropping is achieved through the CropBox property provided by the PdfPageBase class. Instead of physically deleting content from the page, the CropBox defines which portion of the page should remain visible.
By setting a rectangular region, you can precisely control the displayed area of a PDF page. This approach is especially useful in document preprocessing workflows where layout adjustment or content isolation is required.
The following example demonstrates how to crop the first page of a PDF document.
using System.Drawing;
using Spire.Pdf;
namespace CropPDFPage
{
class Program
{
static void Main(string[] args)
{
//Create a PdfDocument object
PdfDocument pdf = new PdfDocument();
//Load a PDF file from disk
pdf.LoadFromFile("Sample1.pdf");
//Get the first page
PdfPageBase page = pdf.Pages[0];
//Crop the page by the specified area
page.CropBox = new RectangleF(0, 300, 600, 260);
//Save the result file
pdf.SaveToFile("CropPDF.pdf");
pdf.Close();
}
}
}
In this example, we load an existing PDF file and access its first page. A rectangular crop area is then defined using RectangleF, where the parameters represent the position and size of the visible region. Finally, the modified document is saved as a new PDF file.
Crop PDF Page and Convert to Image
In many real-world applications, cropping is not only used for generating a new PDF but also for extracting visual content. For example, you may need to convert a cropped section of a page into an image for preview, reporting, or web display purposes.
Spire.PDF for .NET also supports rendering PDF pages as images after applying a crop region. This allows you to reuse the same cropped layout in different output formats.
The following example shows how to crop a PDF page and export it as a PNG image.
using System.Drawing;
using System.Drawing.Imaging;
using Spire.Pdf;
using Spire.Pdf.Graphics;
namespace CropPDFPageToImage
{
class Program
{
static void Main(string[] args)
{
//Create a PdfDocument object
PdfDocument pdf = new PdfDocument();
//Load a PDF file from disk
pdf.LoadFromFile("Sample1.pdf");
//Get the first page
PdfPageBase page = pdf.Pages[0];
//Crop the page by the specified area
page.CropBox = new RectangleF(0, 300, 600, 260);
//Convert the page to an image
Image image = pdf.SaveAsImage(0, PdfImageType.Bitmap);
//Save the image as a PNG file
image.Save("CropPDFSaveAsImage.png", ImageFormat.Png);
//Save the image as a JPG file
//image.Save("ToJPG.jpg", ImageFormat.Jpeg);
//Save the image as a BMP file
//image.Save("ToBMP.bmp", ImageFormat.Bmp);
}
}
}
Here, the workflow is similar to the previous example. After loading the document and defining the crop region, the page is rendered as an image. The output can then be saved in a standard image format such as PNG.
Conclusion
Cropping PDF pages in C# becomes straightforward with Spire.PDF for .NET. By using the CropBox property, developers can easily control which part of a page is visible without altering the original document content.
This technique is widely used in document processing systems, PDF editors, and automated reporting solutions where layout control and content extraction are required.