Rotate PDF via C#

Change PDF page orientation. Use Aspose.PDF for .NET to modify PDF documents programmatically

Rotate PDF Pages with C#

Do you need to rotate PDF? Programmatic modification of PDF documents is an essential part of modern digital workflows. .NET libraries like Aspose.PDF – are stand-alone solutions that don’t rely on other software and are ready for commercial use. They cover all possible needs of professional C# developers.

This C# library enables developers to rotate PDF documents quickly and efficiently through programming. This library shows you how easily to update or change the page orientation of pages in an existing PDF file programmatically with C#. It is a standalone solution for processing PDF documents in C# that does not require the installation of any third-party software.

In order to rotate PDF page, we’ll use Aspose.PDF for .NET API which is a feature-rich, powerful and easy-to-use document manipulation API for .NET platform. Open NuGet package manager, search for Aspose.PDF and install. You may also use the following command from the Package Manager Console.

Package Manager Console

PM > Install-Package Aspose.PDF

How to Rotate PDF documents


You need Aspose.PDF for .NET to try the code in your environment.

  1. Load the PDF with an instance of Document.
  2. Move page upper in order to compensate changing page size.
  3. Set old and new page height.
  4. Setting the page rotation angle.
  5. Save the output PDF file.

If you want to rotate PDF files for your tasks, this solution can help. With this powerful Aspose.PDF .NET library, you can update or change the page orientation of pages while keeping its quality. This professional tool uses C# to rotate PDF effectively. The following example demonstrates how to rotate PDF document in C#:

Rotate PDF - C#

This sample code shows how to Rotate PDF Pages - C#

Input file:

File not added

Output format:

PDF

Output file:

    // The path to the documents directory.
    string dataDir = RunExamples.GetDataDir_AsposePdf_Pages();

    Document doc = new Document(dataDir + "input.pdf");
    foreach (Page page in doc.Pages)
    {
        
        Aspose.Pdf.Rectangle r = page.MediaBox;
        double newHeight = r.Width;
        double newWidth = r.Height;
        double newLLX = r.LLX;
        //  We must to move page upper in order to compensate changing page size
        // (lower edge of the page is 0,0 and information is usually placed from the
        //  Top of the page. That's why we move lover edge upper on difference between
        //  Old and new height.
        double newLLY = r.LLY + (r.Height - newHeight);
        page.MediaBox = new Aspose.Pdf.Rectangle(newLLX, newLLY, newLLX + newWidth, newLLY + newHeight);
        // Sometimes we also need to set CropBox (if it was set in original file)
        page.CropBox = new Aspose.Pdf.Rectangle(newLLX, newLLY, newLLX + newWidth, newLLY + newHeight);

        // Setting Rotation angle of page
        page.Rotate = Rotation.on90;
    }

    dataDir = dataDir + "ChangeOrientation_out.pdf";
    // Save output file
    doc.Save(dataDir);

About Aspose.PDF for C# API

Aspose.PDF for C# API provides a wide range of features for working with PDF files. Some of the features include:

  • Create PDF documents from scratch or from HTML, XML, or images.
  • Compress existing PDF documents by adding or removing pages, text, images, and other content.
  • Convert PDF documents to other formats such as HTML, XML, and images.
  • Render PDF documents to images or XPS format.
  • Print PDF documents directly from your application.
  • Digitally sign PDF documents.

You can find more information on Aspose.PDF for C# API in this Aspose documentation