Get started with IronPDF

Start using IronPDF in your project today with a free trial.

First Step:
green arrow pointer



Rotate PDF Pages

Use the SetPageRotation, SetPageRotations, and SetAllPageRotations methods to set rotation for a single page, multiple pages, and all pages, respectively. These methods completely overwrite the current page rotation with the specified degree, measured clockwise. If the original page rotation is set to the desired degree, the methods would not affect the output file.

:path=/static-assets/pdf/content-code-examples/how-to/rotating-text-set-page-rotation.cs
// Import necessary namespaces
using IronPdf;
using IronPdf.Rendering;
using System.Linq;

// Import the PDF document from a file
PdfDocument pdf = PdfDocument.FromFile("multi-page.pdf");

// Set rotation for the first page (0-based index) to 90 degrees clockwise
// This rotates only the first page of the PDF
pdf.SetPageRotation(0, PdfPageRotation.Clockwise90);

// Set rotation for multiple pages (pages 2, 3, and 4) to 270 degrees clockwise
// Creates a range of page indices starting from 1 to 3
// Note: The range function's second parameter is the number of items, 
// so to rotate pages 2, 3, and 4, it should be Enumerable.Range(1, 3) where 3 is the count.
pdf.SetPageRotations(Enumerable.Range(1, 3), PdfPageRotation.Clockwise270);

// Set rotation for all pages in the document to 180 degrees clockwise
// This will apply a 180-degree rotation to every page in the PDF
// Note that this will override any previous page rotation settings.
pdf.SetAllPageRotations(PdfPageRotation.Clockwise180);

// Save the modified PDF document to a new file
pdf.SaveAs("rotated.pdf");
' Import necessary namespaces

Imports IronPdf

Imports IronPdf.Rendering

Imports System.Linq



' Import the PDF document from a file

Private pdf As PdfDocument = PdfDocument.FromFile("multi-page.pdf")



' Set rotation for the first page (0-based index) to 90 degrees clockwise

' This rotates only the first page of the PDF

pdf.SetPageRotation(0, PdfPageRotation.Clockwise90)



' Set rotation for multiple pages (pages 2, 3, and 4) to 270 degrees clockwise

' Creates a range of page indices starting from 1 to 3

' Note: The range function's second parameter is the number of items, 

' so to rotate pages 2, 3, and 4, it should be Enumerable.Range(1, 3) where 3 is the count.

pdf.SetPageRotations(Enumerable.Range(1, 3), PdfPageRotation.Clockwise270)



' Set rotation for all pages in the document to 180 degrees clockwise

' This will apply a 180-degree rotation to every page in the PDF

' Note that this will override any previous page rotation settings.

pdf.SetAllPageRotations(PdfPageRotation.Clockwise180)



' Save the modified PDF document to a new file

pdf.SaveAs("rotated.pdf")
$vbLabelText   $csharpLabel

Use CSS3 to Rotate Text

After converting HTML to PDF in .NET, there might be a need to programmatically rotate text or entire pages. A frequent requirement is to render vertically aligned text in PDFs using HTML5 and CSS3. Here’s how you can achieve this.

CSS3 allows text rotation to any angle after converting a PDF to HTML using the IronPDF .NET Library you installed earlier. This is achieved using the transform: rotate(...) CSS3 style, which can rotate any HTML element to any angle.

:path=/static-assets/pdf/content-code-examples/how-to/rotating-text-css.cs
using IronPdf;

// Create a new instance of ChromePdfRenderer, which is used to convert HTML to a PDF document.
var renderer = new IronPdf.ChromePdfRenderer();

// The RenderHtmlAsPdf method takes HTML content and converts it to a PDF file.
var pdf = renderer.RenderHtmlAsPdf(@"
<html>
<head>
    <style>
        /* 
           Define a CSS class to rotate text.
           Proper syntax should include valid transform properties and use 'px' for dimensions.
        */
        .rotated {
            transform: rotate(-180deg); /* Correct transformation property */
            width: 400px; /* Width of the element */
            height: 400px; /* Height of the element */
        }
    </style>
</head>
<body>
    <p class='rotated'>Rotated Text</p> <!-- This paragraph uses the rotated class to display rotated text -->
</body>
</html>
");

// Save the rendered PDF to a file named "rotated.pdf" in the current directory.
pdf.SaveAs("rotated.pdf");
Imports IronPdf



' Create a new instance of ChromePdfRenderer, which is used to convert HTML to a PDF document.

Private renderer = New IronPdf.ChromePdfRenderer()



' The RenderHtmlAsPdf method takes HTML content and converts it to a PDF file.

Private pdf = renderer.RenderHtmlAsPdf("

<html>

<head>

    <style>

        /* 

           Define a CSS class to rotate text.

           Proper syntax should include valid transform properties and use 'px' for dimensions.

        */

        .rotated {

            transform: rotate(-180deg); /* Correct transformation property */

            width: 400px; /* Width of the element */

            height: 400px; /* Height of the element */

        }

    </style>

</head>

<body>

    <p class='rotated'>Rotated Text</p> <!-- This paragraph uses the rotated class to display rotated text -->

</body>

</html>

")



' Save the rendered PDF to a file named "rotated.pdf" in the current directory.

pdf.SaveAs("rotated.pdf")
$vbLabelText   $csharpLabel

Frequently Asked Questions

What is PDF text and page rotation?

PDF text and page rotation refers to changing the orientation of either the entire page or specific text elements within a PDF document. This is often done in degrees, typically 90, 180, or 270 degrees, to reposition content either clockwise or counterclockwise.

How can I rotate a PDF file in C#?

To rotate a PDF file in C#, you need to download the IronPDF C# PDF Library and use its methods such as SetPageRotation to specify the desired page rotation. You can then programmatically rotate the PDF pages.

Which methods are available for rotating PDF pages using IronPDF?

IronPDF provides methods like SetPageRotation, SetPageRotations, and SetAllPageRotations. These methods allow you to set rotation for a single page, multiple pages, or all pages, respectively, by specifying the degree of rotation.

Can I use CSS3 to rotate text in a PDF?

Yes, you can use CSS3 to rotate text in a PDF. After converting HTML to PDF using IronPDF, you can apply CSS3 styles such as transform: rotate(...) to rotate text to any angle.

How do I save a rotated PDF using IronPDF?

After applying the desired rotations to your PDF using IronPDF, you can save the modified PDF by using the SaveAs method, specifying the filename for the rotated PDF.

What is the benefit of using HTML5 and CSS3 for text rotation in PDFs?

Using HTML5 and CSS3 allows for flexible and precise control over text rotation in PDFs. You can render vertically aligned text or apply custom rotations to any HTML element, making it easier to achieve the desired layout.

What programming language is used for rotating PDF text and pages with IronPDF?

IronPDF uses C# for rotating PDF text and pages. You can integrate it into your .NET applications to perform these operations programmatically.

Is it possible to rotate specific text elements within a PDF using IronPDF?

Yes, IronPDF allows you to rotate specific text elements within a PDF by using CSS3 styles when converting HTML to PDF, giving you control over individual text orientation.

Chaknith Bin
Software Engineer
Chaknith works on IronXL and IronBarcode. He has deep expertise in C# and .NET, helping improve the software and support customers. His insights from user interactions contribute to better products, documentation, and overall experience.