Crop PDF

Hi, is there a way to programmatically crop a pdf by specifying (x, y, height, width) ? I have found the articles about how to crop part of a page into an image and how to crop a page using only height and width but neither of these fit my use case. Thanks.

1 Reply 1 reply marked as answer

CM Chinnu Muniyappan Syncfusion Team May 11, 2021 02:51 PM UTC

Hi Lucas Oliverrothenberger, 
 
Thank you for contacting Syncfusion support. 
 
We do not have support to crop the PDF document using specified bounds. Instead of we can crop the PDF document pages by manually processing the cropping bounds as follows. 
 
//Load the existing PDF document. 
PdfLoadedDocument ldoc = new PdfLoadedDocument("../../Input.pdf"); 
 
//Create a new resultant PDF document. 
PdfDocument doc = new PdfDocument(); 
 
for (int i = 0; i < ldoc.Pages.Count; i++) 
{ 
    //Load the exisiting PDF page. 
    PdfLoadedPage lpage = ldoc.Pages[0] as PdfLoadedPage; 
 
    RectangleF cropBounds = new RectangleF(0, 100, lpage.Size.Width, 300); 
 
    //Create a new section and set the page size. 
    PdfSection section = doc.Sections.Add(); 
 
    if(lpage.Size.Width > 300) //cropping height. 
    { 
        section.PageSettings.Orientation = PdfPageOrientation.Landscape; 
    } 
    section.PageSettings.Size = new SizeF(lpage.Size.Width, 300); 
 
    //Create new PDF page. 
    PdfPage page = section.Pages.Add(); 
 
    //Get the Pdf page graphics. 
    PdfGraphics graphics = page.Graphics; 
                 
 
    //Create existing page as template and draw to the new page. 
    PdfTemplate template = lpage.CreateTemplate(); 
 
    //Draw template 
    graphics.DrawPdfTemplate(template, new PointF(cropBounds.X, -cropBounds.Y)); 
} 
 
//Save the new document. 
doc.Save("output.pdf"); 
 
//Close the documents. 
doc.Close(true); 
ldoc.Close(true); 
 
Kindly try the above code in your end and let us know whether it achieves your requirement. 
 
Regards,  
Chinnu M 


Marked as answer
Loader.
Up arrow icon