How to Start Tutorials Troubleshooting Main Operations Convert PDF Read PDF Edit PDF PDF Report Generator Work with PDF Modules PDF Document PDF Pages Text Image Graph & Path Annotation, Markup & Drawing Redaction Security Digital Signature Forms Watermark Bookmark Link File Attachment File Metadata Printing Work with Other SDKs Barcode read Barcode create OCR Twain

C# PDF Page Crop Library
How to crop a pdf page and save as image using c# in ASP.NET, Windows application


C# source code to crop pdf pages using c#





In this tutorial, you learn about how to crop PDF pages using C# in ASP.NET MVC Web, Windows applications.

  • Crop a single PDF page with specific rectangle
  • Crop all PDF pages
  • Preview the cropped page with bitmap image

How to crop a PDF page using C#

  1. Download XDoc.PDF Page C# library
  2. Install C# library to crop PDF pages
  3. Step by Step Tutorial












You can adjust the visible area (rectangle) in a PDF page using PDF page cropping API using C#.

Cropping a PDF does not reduce file size because visible information data is merely hidden, not removed or deleted. By resetting the cropped PDF page width and height, you can restore the page and its content.

C# PDF page editing library also provides: how to count pdf pages in c#, how to delete a page from a pdf in c#, how to make a pdf of a page in c#, c# pdf add background, c# rotate pdf document, how to add footer in pdf using in c#.





How to crop a PDF page in C#


You can adjust the visible PDF page area by cropping a PDF page. It is an easy task to crop a page using PDF C# library by using method PDFPage.Crop(). Below are the steps and sample C# code to crop a page in PDF.

  1. Create a PDFDocument object with a PDF file loaded
  2. Get the second PDF page PDFPage object to crop
  3. Create a RectangleF object to specify the area to cop
  4. Call PDFPage.Crop() method to crop the page
  5. Save the modified PDF document



String inputFilePath = Program.RootPath + "\\" + "1.pdf";
String outputFilePath = Program.RootPath + "\\" + "1_new.pdf";

//  open the file
PDFDocument doc = new PDFDocument(inputFilePath);
//  get the 2nd page
PDFPage page = (PDFPage)doc.GetPage(1);
//  set crop region: start point (100, 100), width = 300, height = 400
RectangleF cropArea = new RectangleF(100, 100, 300, 400);
//  crop the page
page.Crop(cropArea);

//  output the new document
doc.Save(outputFilePath);




Change size for all pages in a document


You can also crop all PDF pages by using method PDFDocument .CropAllPages(). Below are the sample C# code to crop all pages in PDF.



String inputFilePath = Program.RootPath + "\\" + "1.pdf";
String outputFilePath = Program.RootPath + "\\" + "1_new.pdf";

//  open the file
PDFDocument doc = new PDFDocument(inputFilePath);
//  set crop region: start point (100, 100), width = 300, height = 400
RectangleF cropArea = new RectangleF(100, 100, 300, 400);
//  crop pages
doc.CropAllPages(cropArea);

//  output the new document
doc.Save(outputFilePath);




How to crop PDF page and save as Bitmap image in C#


You can also crop a PDF page and export to scaled image file. Below are the sample C# code to crop a page and save to Bitmap in C#.



            String inputFilePath = @"C:\1.pdf";

            PDFDocument doc = new PDFDocument(inputFilePath);

            // Get the first page object
            PDFPage aPage = (PDFPage)doc.GetPage(0);

            // set crop region: start point (100, 100), width = 300, height = 400
            Rectangle cropArea = new Rectangle(100, 100, 300, 400);

            // output cropped page to bitmap image with scale 2f.
            Bitmap croppedPageImage = aPage.CropImage(cropArea, 2f);