Articles in this section
Category / Section

How to split the landscape page to portrait in WinForms PDF?

7 mins read

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:

  1. Create a new C# console application project.

Create a new C# console application project

  1. Install the Syncfusion.Pdf.WinForms NuGet package as a reference to your .NET Framework application from Nuget.org.

NuGet package reference

  1. 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

 

  1. 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:

Input PDF screenshot

By executing the program, you will get the following PDF document as follows,

Output document screenshot

 

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.

 

Note:

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.

Also see:

Conclusion
I hope you enjoyed learning how to split landscape pages into portrait in WinForms PDF.


You can refer to our WinForms PDF feature tour page to learn about its other groundbreaking feature representations and documentation, and how to quickly get started with configuration specifications. You can also explore our WinForms 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 or feedback portal. We are always happy to assist you!
Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied