News Category

C#/VB.NET: Rotate Pages in PDF

2022-06-16 08:42:00 Written by  support iceblue
Rate this item
(0 votes)

In some cases, you might need to rotate PDF pages. For example, when you receive a PDF document that contains disoriented pages, you may wish to rotate the pages so you can read the document easier. In this article, you will learn how to rotate pages in PDF in C# and VB.NET using Spire.PDF for .NET.

Install Spire.PDF for .NET

To begin with, you need to add the DLL files included in the Spire.PDF for.NET package as references in your .NET project. The DLL files can be either downloaded from this link or installed via NuGet.

PM> Install-Package Spire.PDF

Rotate a Specific Page in PDF using C# and VB.NET

Rotation is based on 90-degree increments. You can rotate a PDF page by 0/90/180/270 degrees. The following are the steps to rotate a PDF page:

  • Create an instance of PdfDocument class.
  • Load a PDF document using PdfDocument.LoadFromFile() method.
  • Get the desired page by its index (zero-based) through PdfDocument.Pages[pageIndex] property.
  • Get the original rotation angle of the page through PdfPageBase.Rotation property.
  • Increase the original rotation angle by desired degrees.
  • Apply the new rotation angle to the page through PdfPageBase.Rotation property.
  • Save the result document using PdfDocument.SaveToFile() method.
  • C#
  • VB.NET
using Spire.Pdf;

namespace RotatePdfPage
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a PdfDocument instance
            PdfDocument pdf = new PdfDocument();
            //Load a PDF document
            pdf.LoadFromFile("Sample.pdf");

            //Get the first page
            PdfPageBase page = pdf.Pages[0];

            //Get the original rotation angle of the page
            int rotation = (int)page.Rotation;

            //Rotate the page 180 degrees clockwise based on the original rotation angle
            rotation += (int)PdfPageRotateAngle.RotateAngle180;
            page.Rotation = (PdfPageRotateAngle)rotation;

            //Save the result document
            pdf.SaveToFile("Rotate.pdf");
        }
    }
}

C#/VB.NET: Rotate Pages in PDF

Rotate All Pages in PDF using C# and VB.NET

The following are the steps to rotate all pages in a PDF document:

  • Create an instance of PdfDocument class.
  • Load a PDF document using PdfDocument.LoadFromFile() method.
  • Loop through each page in the document.
  • Get the original rotation angle of the page through PdfPageBase.Rotation property.
  • Increase the original rotation angle by desired degrees.
  • Apply the new rotation angle to the page through PdfPageBase.Rotation property.
  • Save the result document using PdfDocument.SaveToFile() method.
  • C#
  • VB.NET
using Spire.Pdf;

namespace RotateAllPdfPages
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a PdfDocument instance
            PdfDocument pdf = new PdfDocument();
            //Load a PDF document
            pdf.LoadFromFile("Sample.pdf");

            foreach (PdfPageBase page in pdf.Pages)
            {
                //Get the original rotation angle of the page
                int rotation = (int)page.Rotation;
                //Rotate the page 180 degrees clockwise based on the original rotation angle
                rotation += (int)PdfPageRotateAngle.RotateAngle180;
                page.Rotation = (PdfPageRotateAngle)rotation;
            }

            //Save the result document
            pdf.SaveToFile("RotateAll.pdf");
        }
    }
}

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.

Additional Info

  • tutorial_title:
Last modified on Tuesday, 20 June 2023 01:55