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 landscape pages into portrait pages in an existing PDF document using C# and VB.NET.
Steps to split landscape pages into 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 the 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 landscape pages into portrait pages 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 width in 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 another page.
page = document.Pages.Add();
// Draw the template.
page.Graphics.DrawPdfTemplate(template, new PointF(-page.GetClientSize().Width / 2, 0));
}
else
{
// Import the page into the new PDF document.
document.ImportPage(loadedDocument, i);
}
}
// Save the PDF document.
string fileName = "Sample.pdf";
document.Save(fileName);
// Close the document.
document.Close(true);
loadedDocument.Close(true);
// Open the PDF file and view the result 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 width in inches.
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 another page.
page = document.Pages.Add()
' Draw the template.
page.Graphics.DrawPdfTemplate(template, New PointF(-page.GetClientSize().Width / 2, 0))
Else
' Import the page into the new PDF document.
document.ImportPage(loadedDocument, i)
End If
Next
' Save the PDF document.
Dim fileName As String = "Sample.pdf"
document.Save(fileName)
' Close the document.
document.Close(True)
loadedDocument.Close(True)
' Open the PDF file and view the result 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 a trial setup or from the NuGet feed, include a license key in your projects. Refer to the link to learn about generating and registering a Syncfusion license key in your application to use the components without a trial message.