|
//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); |