How to split the landscape page to portrait in WinForms PDF?
The Syncfusion PDF library is a .NET PDF library used to create, read, and edit PDF documents. Using this library, you can split the landscape pages to portrait pages in an existing PDF document using C# and VB.NET.
Steps to split the landscape pages to portrait pages in an existing PDF document programmatically:
- Create a new C# console application project.
- Install the Syncfusion.Pdf.WinForms NuGet package as a reference to your .NET Framework application from Nuget.org.
- Include the following namespace in Program.cs file.
C#
using Syncfusion.Pdf; using Syncfusion.Pdf.Graphics; using Syncfusion.Pdf.Parsing; using System.Drawing;
VB.NET
Imports Syncfusion.Pdf Imports Syncfusion.Pdf.Graphics Imports Syncfusion.Pdf.Parsing Imports System.Drawing
- Use the following code samples to split the landscape pages to the portrait page in an existing PDF document.
C#
string file = "../../Data/InputDoc.pdf"; //Load the existing PDF document. PdfLoadedDocument loadedDocument = new PdfLoadedDocument(file); //Create a new PDF document. PdfDocument document = new PdfDocument(); for (int i = 0; i < loadedDocument.Pages.Count; i++) { //Load the page. PdfLoadedPage loadedPage = loadedDocument.Pages[i] as PdfLoadedPage; if (loadedPage.Size.Width > loadedPage.Size.Height) { //Create the template from the page. PdfTemplate template = loadedPage.CreateTemplate(); //Calculate the height from inches. float width = template.Width / 2; //Set the document margin and height. document.PageSettings.Height = loadedPage.Size.Height; document.PageSettings.Width = width; document.PageSettings.Margins.All = 0; //Add the page to a new PDF document. PdfPage page = document.Pages.Add(); //Create graphics. PdfGraphics graphics = page.Graphics; //Draw the template. graphics.DrawPdfTemplate(template, new PointF(0, 0)); //Add a page. page = document.Pages.Add(); //Draw a template. page.Graphics.DrawPdfTemplate(template, new PointF(-page.GetClientSize().Width/2, 0)); } else { //Import the pages to the new PDF document. document.ImportPage(loadedDocument, i); } } //Save the PDF document. string fileName = "Sample.pdf"; document.Save(fileName); //Closes the document. document.Close(true); loadedDocument.Close(true); //This will open the PDF file and the result will be seen in the default PDF viewer. Process.Start(fileName);
VB.NET
Dim file As String = "../../Data/InputDoc.pdf" 'Load the existing PDF document. Dim loadedDocument As PdfLoadedDocument = New PdfLoadedDocument(file) 'Create a new PDF document. Dim document As PdfDocument = New PdfDocument() For i As Integer = 0 To loadedDocument.Pages.Count - 1 'Load the page. Dim loadedPage As PdfLoadedPage = TryCast(loadedDocument.Pages(i), PdfLoadedPage) If loadedPage.Size.Width > loadedPage.Size.Height Then 'Create the template from the page. Dim template As PdfTemplate = loadedPage.CreateTemplate() 'Calculate the page height. Dim width As Single = template.Width / 2 'Set the document margin and height. document.PageSettings.Height = loadedPage.Size.Height document.PageSettings.Width = width document.PageSettings.Margins.All = 0 'Add the page to a new PDF document. Dim page As PdfPage = document.Pages.Add() 'Create graphics. Dim graphics As PdfGraphics = page.Graphics 'Draw the template. graphics.DrawPdfTemplate(template, New PointF(0, 0)) 'Add a page. page = document.Pages.Add() 'Draw a template. page.Graphics.DrawPdfTemplate(template, New PointF(-page.GetClientSize().Width / 2, 0)) Else 'Import the pages to a new PDF document. document.ImportPage(loadedDocument, i) End If Next 'Save the PDF document. Dim fileName As String = "Sample.pdf" document.Save(fileName) 'Closes the document. document.Close(True) loadedDocument.Close(True) 'This will open the PDF file and the result will be seen in the default PDF viewer. Process.Start(fileName)
A complete working sample can be download from SplitPDFByWidth.zip.
Input PDF screenshot:
By executing the program, you will get the following PDF document as follows,
Take a moment to peruse the documentation. You can find options like creating a new PDF template, creating templates from an existing PDF document, and creating document overlays.
Refer to this link 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 the NuGet feed, include a license key in your projects. Refer to this link to learn about generating and registering the Syncfusion license key in your application to use the components without trail message.