Spire.PDF supports to horizontally and vertically split a PDF page into two or more pages. This article will show you how to use Spire.PDF to accomplish this function.
The sample PDF file:
Detail steps:
Step 1: Load the sample PDF file and get the first page.
PdfDocument pdf = new PdfDocument(); pdf.LoadFromFile("New Zealand.pdf"); PdfPageBase page = pdf.Pages[0];
Step 2: Create a new PDF file and remove page margins.
PdfDocument newPdf = new PdfDocument(); newPdf.PageSettings.Margins.All = 0;
Step 3: Set page width and height in order to horizontally or vertically split the first page into 2 pages.
//Horizontally Split newPdf.PageSettings.Width = page.Size.Width; newPdf.PageSettings.Height = page.Size.Height / 2; //Vertically split //newPdf.PageSettings.Width = page.Size.Width / 2; //newPdf.PageSettings.Height = page.Size.Height;
Step 5: Add a new page to the new PDF file.
PdfPageBase newPage = newPdf.Pages.Add();
Step 6: Create layout format.
PdfTextLayout format = new PdfTextLayout(); format.Break = PdfLayoutBreakType.FitPage; format.Layout = PdfLayoutType.Paginate;
Step 7: Create template from the first Page of the sample PDF, and draw the template to the new added page with the layout format.
page.CreateTemplate().Draw(newPage, new PointF(0, 0), format);
Step 8: Save and close.
newPdf.SaveToFile("SplitPage.pdf"); newPdf.Close(); pdf.Close();
Horizontally split:
Vertically split:
Full code:
using System.Drawing; using Spire.Pdf; using Spire.Pdf.Graphics; namespace SplitPDFPage { class Program { static void Main(string[] args) { //Load the sample PDF PdfDocument pdf = new PdfDocument(); pdf.LoadFromFile("New Zealand.pdf"); //Get the first page PdfPageBase page = pdf.Pages[0]; //Create a new PDF PdfDocument newPdf = new PdfDocument(); //Remove page margins newPdf.PageSettings.Margins.All = 0; //Set page width and height in order to horizontally split the first page into 2 pages newPdf.PageSettings.Width = page.Size.Width; newPdf.PageSettings.Height = page.Size.Height / 2; //Set page width and height in order to vertically split the first page into 2 pages //newPdf.PageSettings.Width = page.Size.Width / 2; //newPdf.PageSettings.Height = page.Size.Height; //Add a new page to the new PDF PdfPageBase newPage = newPdf.Pages.Add(); //Create layout format PdfTextLayout format = new PdfTextLayout(); format.Break = PdfLayoutBreakType.FitPage; format.Layout = PdfLayoutType.Paginate; //Create template from the first Page of the sample PDF, and draw the template to the new added page with the layout format page.CreateTemplate().Draw(newPage, new PointF(0, 0), format); //Save and close newPdf.SaveToFile("SplitPage.pdf"); newPdf.Close(); pdf.Close(); } } }