Articles in this section
Category / Section

How to change the orientation of an existing PDF document using C# and VB.NET

5 mins read

The Syncfusion Essential® PDF is a .NET PDF library used to create, read, and edit PDF documents. Using this library, you can change the orientation of an existing PDF document using C# and VB.NET.

Steps to change the orientation of an existing PDF document programmatically:

  1. Create a new C# Console application project.

Create C# console application

  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 namespaces 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

 

  1. Use the following code sample to change the orientation of an existing PDF document.

C#

//Load a PDF document
PdfLoadedDocument loadedDocument = new PdfLoadedDocument("../../Data/Barcode.pdf");
 
//Create a new PDF document
PdfDocument document = new PdfDocument();
 
//Set the orientation
document.PageSettings.Orientation = PdfPageOrientation.Landscape;
 
//Set margin
document.PageSettings.Margins.All = 0;
 
for (int i = 0; i < loadedDocument.Pages.Count; i++)
{
    //Load the page
    PdfLoadedPage loadedPage = loadedDocument.Pages[i] as PdfLoadedPage;
 
    //Create the template from the page.
    PdfTemplate template = loadedPage.CreateTemplate();
 
    //Add the page
    PdfPage page = document.Pages.Add();
 
    //Create the graphics
    PdfGraphics graphics = page.Graphics;
 
    //Draw the template
    graphics.DrawPdfTemplate(template, PointF.Empty, new SizeF(page.Size.Width, page.Size.Height));
}
 
//Save the document
document.Save("Output.pdf");
 
//Close both document instances
loadedDocument.Close(true);
document.Close(true);
 
//This will open the PDF file and the result will be seen in the default PDF Viewer
Process.Start("Output.pdf");

 

VB.NET

'Load a PDF document
Dim loadedDocument As PdfLoadedDocument = New PdfLoadedDocument("../../Data/Barcode.pdf")
 
'Create a new PDF document
Dim document As PdfDocument = New PdfDocument()
 
'Set the orientation
document.PageSettings.Orientation = PdfPageOrientation.Landscape
 
'Set margin
document.PageSettings.Margins.All = 0
 
For i As Integer = 0 To loadedDocument.Pages.Count - 1
 
    'Load the page
    Dim loadedPage As PdfLoadedPage = TryCast(loadedDocument.Pages(i), PdfLoadedPage)
 
    'Create the template from the page
    Dim template As PdfTemplate = loadedPage.CreateTemplate()
 
    'Add the page
    Dim page As PdfPage = document.Pages.Add()
 
    'Create the graphics
    Dim graphics As PdfGraphics = page.Graphics
 
    'Draw the template
    graphics.DrawPdfTemplate(template, PointF.Empty, New SizeF(page.Size.Width, page.Size.Height))
Next
 
'Save the document
document.Save("Output.pdf")
 
'Close both document instances
loadedDocument.Close(True)
document.Close(True)
 
'This will open the PDF file and the result will be seen in the default PDF Viewer
Process.Start("Output.pdf")

 

A complete working sample can be download from ChageOrientationSample.zip.

 

Take a moment to peruse the documentation. You can find the options like create a new PDF template, creating templates from an existing PDF document, creating document overlays.

 

Refer to this link to explore a rich set of Syncfusion Essential® PDF features.

 

Note:

Starting with v16.2.0.x, if you reference Syncfusion® assemblies from trial setup or the NuGet feed, include a license key in your product. Refer to this link to learn about generating and registering the Syncfusion® license key in your application to use the components without trail message.

 

See Also:

Change the orientation of a new PDF document

 

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