How to split PDF page into half using C# and VB.NET?
Syncfusion Essential® PDF is a .NET PDF library used to create, read, and edit PDF documents. Using this library, you can split PDF page into half using C# and VB.NET.
Steps to split PDF page in to half programmatically:
- Create a new C# console application project.
- Install the Syncfusion.Pdf.WinForms NuGet package as reference to your .NET Framework application from NuGet.org.
- Include the following namespaces in Program.cs file.
C#
using Syncfusion.Pdf; using Syncfusion.Pdf.Parsing; using Syncfusion.Pdf.Graphics; using System.Drawing;
VB.NET
Imports Syncfusion.Pdf Imports Syncfusion.Pdf.Parsing Imports Syncfusion.Pdf.Graphics Imports System.Drawing
- Use the following code snippet to split the PDF page into half and save it as a new PDF document.
C#
//Load the existing PDF document PdfLoadedDocument loadedDocument = new PdfLoadedDocument("Sample.pdf"); //Create a new PDF document PdfDocument document = new PdfDocument(); //Set the document margin document.PageSettings.Margins.All = 0; //Get page count from loaded document int count = loadedDocument.Pages.Count; //Load each page and draw it as template to new document for (int i=0;i<count;i++) { //Load the page PdfLoadedPage loadedPage = loadedDocument.Pages[i] as PdfLoadedPage; //Add a section PdfSection section = document.Sections.Add(); //Set the size section.PageSettings.Width = loadedPage.Size.Width; section.PageSettings.Height = loadedPage.Size.Height/2; //Add a page to the section PdfPage page = section.Pages.Add(); //Create the graphics PdfGraphics graphics = page.Graphics; //Create the template from the page PdfTemplate template = loadedPage.CreateTemplate(); //Create layout format to paginate the template to next page PdfLayoutFormat format = new PdfLayoutFormat(); format.Break = PdfLayoutBreakType.FitPage; format.Layout = PdfLayoutType.Paginate; //Draw the template template.Draw(page, PointF.Empty, format); } //Save the new document document.Save("SplitPage.pdf"); //Close the documents loadedDocument.Close(true); document.Close(true); //This will open the PDF file so, the result will be seen in default PDF Viewer Process.Start("SplitPage.pdf");
VB.NET
'Load the existing PDF document Dim loadedDocument As PdfLoadedDocument = New PdfLoadedDocument("Sample.pdf") 'Create a new PDF document Dim document As PdfDocument = New PdfDocument() 'Set the document margin document.PageSettings.Margins.All = 0 'Get the page count from loaded document Dim count As Integer = loadedDocument.Pages.Count 'Load each page and draw it as template to new document For i As Integer = 0 To count - 1 'Load the page Dim loadedPage As PdfLoadedPage = TryCast(loadedDocument.Pages(i), PdfLoadedPage) 'Add a section Dim section As PdfSection = document.Sections.Add() 'Set the size to section section.PageSettings.Width = loadedPage.Size.Width section.PageSettings.Height = loadedPage.Size.Height / 2 'Add a page to the section Dim page As PdfPage = section.Pages.Add() 'Create the graphics Dim graphics As PdfGraphics = page.Graphics 'Create the template from the page Dim template As PdfTemplate = loadedPage.CreateTemplate() 'Create layout format to paginate the template to next page Dim format As PdfLayoutFormat = New PdfLayoutFormat() format.Break = PdfLayoutBreakType.FitPage format.Layout = PdfLayoutType.Paginate 'Draw the template template.Draw(page, PointF.Empty, format) Next 'Save the new document document.Save("SplitPage.pdf") 'Close the documents loadedDocument.Close(True) document.Close(True) 'This will open the PDF file so, the result will be seen in default PDF Viewer Process.Start("SplitPage.pdf")
A complete working sample can be downloaded from SplitPageSample.zip.
By executing the program, you will get the PDF document as follows.
Take a moment to peruse the documentation for working with templates, where you will find options like creating a new template or creating template from an existing document and creating document overlay with code example.
Refer here to explore the rich set of Syncfusion Essential® PDF features.
Starting with v16.2.0.x, if you reference Syncfusion® assemblies from trial setup or from the NuGet feed, include a license key in your projects. Refer to link to learn about generating and registering Syncfusion® license key in your application to use the components without trail message.
Conclusion
I hope you enjoyed learning about how to split PDF page into half using C# and VB.NET.
You can refer to our .NET PDF feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. You can also explore our .NET PDF example to understand how to create and manipulate data.
For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion®, you can try our 30-day free trial to check out our other controls.
If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!