Is it possibile to split a single page of a pdf?
I mean i've got a pdf, and i want to split into desidered pieces and put every single piece in another pdf file
static void Main(string[] args)
{
PdfDocument doc = new PdfDocument();
doc.LoadFromFile("test.pdf");
//Number of pieces in the horizontal direction
int horizontalNumber = 3;
//Number of pieces in the vertical direction
int verticalNumber = 2;
//!!!Note, horizontalNumber and verticalNumber must be > 0
//Split the page horizontally into equal-sized pages
PdfDocument temPdf = Split(doc, horizontalNumber, true);
//Split the page vertically into equal-sized pages
PdfDocument newPdf = Split(temPdf, verticalNumber, false);
//Save the Pdf document
string output = "output.pdf";
newPdf.SaveToFile(output);
}
public static PdfDocument Split(PdfDocument pdf, int number, bool isHorizontal)
{
//Create a new Pdf
PdfDocument newPdf = new PdfDocument();
//Remove all the margins
newPdf.PageSettings.Margins.All = 0;
//Spilt pages
foreach (PdfPageBase page in pdf.Pages)
{
//Set the page size of new Pdf
if (isHorizontal)
{
newPdf.PageSettings.Width = page.Size.Width;
newPdf.PageSettings.Height = page.Size.Height / number;
}
else
{
newPdf.PageSettings.Width = page.Size.Width / number;
newPdf.PageSettings.Height = page.Size.Height;
}
//Add a new page
PdfPageBase newPage = newPdf.Pages.Add();
PdfTextLayout format = new PdfTextLayout();
format.Break = PdfLayoutBreakType.FitPage;
format.Layout = PdfLayoutType.Paginate;
//Draw the page in the new page
page.CreateTemplate().Draw(newPage, new PointF(0, 0), format);
}
return newPdf;
}
PdfDocument doc = new PdfDocument();
doc.LoadFromFile("input.pdf");
PdfPageBase page = doc.Pages[0];
float width = page.Size.Width;
float height1 = 300;
float height2 = page.Size.Height - height1;
PdfDocument newDoc = new PdfDocument();
PdfPageBase newPage = newDoc.Pages.Add(new SizeF(width, height1), new Spire.Pdf.Graphics.PdfMargins(0));
page.CreateTemplate().Draw(newPage.Canvas, new PointF(0, 0));
newPage = newDoc.Pages.Add(new SizeF(width, height2), new Spire.Pdf.Graphics.PdfMargins(0));
page.CreateTemplate().Draw(newPage.Canvas, new PointF(0, -height1));
string result = "result.pdf";
newDoc.SaveToFile(result);