How to Start Convert PDF Read PDF Build PDF 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

Using C# PDF SDK
C# PDF Library: how to crop pdf pages using c#


C# Demo Code to crop pdf pages using c#













Retrieve box boundary settings in a PDF page


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

//  Open file and get the target page
PDFDocument doc = new PDFDocument(inputFilePath);
PDFPage page = (PDFPage)doc.GetPage(0);

//  Get media box of the target page.
float[] bbox = page.GetPageBoundary(PDFPageBoundaryType.MediaBox);
Console.WriteLine("Media Box: llx={0}, lly={1}, urx={2}, ury={3}", bbox[0], bbox[1], bbox[2], bbox[3]);
//  Get crop box of the target page.
bbox = page.GetPageBoundary(PDFPageBoundaryType.CropBox);
if (bbox != null)
    Console.WriteLine("Crop Box: llx={0}, lly={1}, urx={2}, ury={3}", bbox[0], bbox[1], bbox[2], bbox[3]);
else
    Console.WriteLine("No Crop Box");
//  Get bleed box of the target page.
bbox = page.GetPageBoundary(PDFPageBoundaryType.BleedBox);
if (bbox != null)
    Console.WriteLine("Bleed Box: llx={0}, lly={1}, urx={2}, ury={3}", bbox[0], bbox[1], bbox[2], bbox[3]);
else
    Console.WriteLine("No Bleed Box");
//  Get trim box of the target page.
bbox = page.GetPageBoundary(PDFPageBoundaryType.TrimBox);
if (bbox != null)
    Console.WriteLine("Trim Box: llx={0}, lly={1}, urx={2}, ury={3}", bbox[0], bbox[1], bbox[2], bbox[3]);
else
    Console.WriteLine("No Trim Box");
//  Get art box of the target page.
bbox = page.GetPageBoundary(PDFPageBoundaryType.ArtBox);
if (bbox != null)
    Console.WriteLine("Art Box: llx={0}, lly={1}, urx={2}, ury={3}", bbox[0], bbox[1], bbox[2], bbox[3]);
else
    Console.WriteLine("No Art Box");




Update box boundary settings in a PDF page


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

//  Open file and get the target page
PDFDocument doc = new PDFDocument(inputFilePath);
PDFPage page = (PDFPage)doc.GetPage(0);

float[] bbox = page.GetPageBoundary(PDFPageBoundaryType.MediaBox);
//  Add (or update) bleed box.
page.SetPageBoundary(PDFPageBoundaryType.BleedBox, bbox[0] + 20, bbox[1] + 20, bbox[2] - 50, bbox[3] - 60);
//  Add (or update) trim box.
page.SetPageBoundary(PDFPageBoundaryType.TrimBox, bbox[0] + 60, bbox[1] + 40, bbox[2] - 100, bbox[3] - 160);
//  Add (or update) art box.
page.SetPageBoundary(PDFPageBoundaryType.ArtBox, bbox[0] + 90, bbox[1] + 50, bbox[2] - 150, bbox[3] - 260);

//  Save file
doc.Save(outputFilePath);