Recursive AI

Crop a PDF page in C#

OnePDF is a free, open-source PDF library for .NET from Recursive AI. MIT or Apache-2.0, no native dependencies.

Set the visible area of sample.pdf from the zip.

Terminal
dotnet add package RecursiveAI.OnePdf

Requires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.

C#
using OnePdf;
 
using var doc = PdfDocument.Open("sample.pdf",
    new PdfOpenOptions { AllowEditing = true });
doc.Pages[0].SetCropBox(new(36, 36, 576, 756));
doc.Save("cropped.pdf");
Console.WriteLine("Wrote cropped.pdf");

Unzip the example, then dotnet run.

Text
Wrote cropped.pdf

The four numbers are (left, bottom, right, top) in points (72 per inch here); this keeps the center 540 × 720 points of the US Letter page.

Cropping only hides content outside the box, such as the red square in the sample; the content stays in the file unless you redact it.

The full program

The download reopens cropped.pdf to report the saved crop size. Run dotnet run -- input.pdf to try the same crop on your own PDF.

C#
using OnePdf;
 
string input = args.Length == 0 ? "sample.pdf" : args[0];
var edit = new PdfOpenOptions { AllowEditing = true };
using var doc = PdfDocument.Open(input, edit);
PdfPage page = doc.Pages[0];
PdfRectangle before = page.CropBox;
page.SetCropBox(new PdfRectangle(36, 36, 576, 756));
doc.Save("cropped.pdf");
 
using var saved = PdfDocument.Open("cropped.pdf");
PdfRectangle after = saved.Pages[0].CropBox;
Console.WriteLine(FormattableString.Invariant(
    $"Before: {before.Width} x {before.Height} pt"));
Console.WriteLine(FormattableString.Invariant(
    $"Cropped: {after.Width} x {after.Height} pt"));
Text
Before: 612 x 792 pt
Cropped: 540 x 720 pt

An empty crop rectangle, such as one whose left and right coordinates match, throws ArgumentException and leaves the page unchanged.

Variations

Inset the current crop by 18 units on every side. Replace SetCropBox in the snippet at the top with this. Use it on pages wider and taller than 36 units.

C#
var page = doc.Pages[0];
var box = page.CropBox;
page.SetCropBox(new PdfRectangle(
    box.Left + 18, box.Bottom + 18,
    box.Right - 18, box.Top - 18));

Restore the full page. Open cropped.pdf as your input, replace SetCropBox with this line, and save to another path, such as restored.pdf.

C#
doc.Pages[0].ClearCropBox();

The short answer

To crop an existing PDF page in C#, open it with AllowEditing = true, set the page’s CropBox with SetCropBox, and save to a new file. Pages[0] is the first page. The rectangle describes the area to keep, not what to remove. OnePDF writes the new crop to that page even if it previously inherited a crop from the page tree.

How do I crop a PDF page to a specific size?

PdfRectangle takes two opposite corners: (left, bottom, right, top). It does not take (x, y, width, height). Coordinates are in the PDF’s default user space, with y increasing upward. On the sample page, the normal PDF scale is 72 units to an inch, and the full page runs from (0, 0) to (612, 792). The recipe takes 36 units, or half an inch, off each edge: 576 − 36 = 540 units wide and 756 − 36 = 720 units high.

For a different PDF, read the existing page.CropBox before picking coordinates. A PDF can have a nonzero lower-left corner, a rotation, or a different /UserUnit scale. If the page is rotated, use the underlying page coordinates rather than copying x and y from the screen. The page geometry reference explains how GetDisplaySize accounts for rotation and scale when you need dimensions as displayed. The effective crop is clipped to the media box, the full physical page; changing the crop does not enlarge that page or rescale its artwork.

Does cropping a PDF remove the hidden content?

No. The crop box controls what viewers display and print, not what the file stores. On the sample, the blue rectangle remains visible and the red square is hidden. Clearing the crop box makes the red square visible again. The same is true of text or images that extend outside the crop: a reader or tool that uses the full page can still see them. Don’t crop out account numbers, personal details, or anything else you need to remove from a shared PDF. Use redaction for that.

All onepdf recipes