Articles in this section
Category / Section

Adjust Page Orientation in PDFs Programmatically using .NET PDF Library

4 mins read

The Syncfusion Essential® PDF is a .NET PDF library used to create, read, and edit PDF documents. This guide demonstrates how to change the orientation of a specific page within a PDF document using C# and VB.NET.

Steps to change page orientation in a PDF document

  1. Create a Console Application: Set up a new console application project.
    Screenshot (1337).png
  2. Install Syncfusion® Package: Add the Syncfusion.Pdf.Net.Core NuGet package to your console application.
    Screenshot.png
  3. Include Required Namespaces: Add the following namespaces in Program.cs.

C#

using Syncfusion.Pdf.Graphics;
using Syncfusion.Pdf.Parsing;
using Syncfusion.Pdf;
using Syncfusion.Drawing;

VB.NET

Imports Syncfusion.Pdf.Graphics
Imports Syncfusion.Pdf.Parsing
Imports Syncfusion.Pdf
Imports Syncfusion.Drawing
  1. Code Implementation for Orienting Pages: Use the following code to adjust the orientation of a specific page in an existing PDF document.

C#

//Load a PDF document.
FileStream docStream = new FileStream("Barcode.pdf", FileMode.Open, FileAccess.Read);
PdfLoadedDocument loadedDocument = new PdfLoadedDocument(docStream);
//Create a new PDF document.
PdfDocument document = new PdfDocument();
//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 section to the document.
   PdfSection section = document.Sections.Add();
   section.PageSettings.Margins.All = 0;
   section.PageSettings.Size = new SizeF(loadedPage.Size.Width, loadedPage.Size.Height);
   if (i == 0)
   {
       //Add the page orientation.
       section.PageSettings.Orientation = PdfPageOrientation.Landscape;
   }
   else
   {
       //Add the page orientation.
       section.PageSettings.Orientation = PdfPageOrientation.Portrait;
   }
   PdfPage page = section.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));
}
//Create file stream.
using (FileStream outputFileStream = new FileStream(@"Output.pdf", FileMode.Create, FileAccess.ReadWrite))
{
   //Save the PDF document to the file stream.
   document.Save(outputFileStream);
}
//Close the document.
document.Close(true);

VB.NET

' Load a PDF document.
Dim docStream As New FileStream("Barcode.pdf", FileMode.Open, FileAccess.Read)
Dim loadedDocument As New PdfLoadedDocument(docStream)

' Create a new PDF document.
Dim document As New PdfDocument()

' 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 section to the document.
   Dim section As PdfSection = document.Sections.Add()
   section.PageSettings.Margins.All = 0
   section.PageSettings.Size = New SizeF(loadedPage.Size.Width, loadedPage.Size.Height)

   If i = 0 Then
       ' Add the page orientation.
       section.PageSettings.Orientation = PdfPageOrientation.Landscape
   Else
       ' Add the page orientation.
       section.PageSettings.Orientation = PdfPageOrientation.Portrait
   End If

   Dim page As PdfPage = section.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

' Create file stream.
Using outputFileStream As New FileStream(Path.GetFullPath("Output.pdf"), FileMode.Create, FileAccess.ReadWrite)
   ' Save the PDF document to file stream.
   document.Save(outputFileStream)
End Using

' Close the document.
document.Close(True)

Note: Changing the document orientation may cause the page content to stretch or shrink, as it adjusts to the new page size dimensions.

A complete working sample can be downloaded from ChangePageOrientation.zip.

By executing the program, the output PDF document will be generated as shown below.

Screenshot (1341).png

Take a moment to explore the documentation on working with pages, where you’ll find options such as inserting, rotating, importing, and rearranging pages from an existing document, as well as removing pages and splitting a PDF file into individual pages

Conclusion
I hope you enjoyed learning about how to change the orientation in specific page using .NET PDF library.

You can refer to our .NET PDF library 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 library Example to understand how to create and manipulate data.

For current customers, you can check out our ASP.NET Core 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!

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