Skip to footer content

How to Rotate Text in PDFs Using C#

In this tutorial, we explore how to rotate text and pages in PDF documents using IronPDF in .NET. We begin by ensuring the IronPDF NuGet package is installed. Next, we move to the Program.cs file, where we import IronPDF and set up the license key. We instantiate the ChromePdfRenderer object for creating and manipulating PDFs. Setting the input encoding to UTF-8 ensures proper HTML content rendering. We use the transform CSS property to rotate text within an HTML string, saving the output as 'rotated-text.pdf'. To rotate existing PDF pages, we open the document using the PdfDocument.FromFile method and rotate the first page 90° clockwise with the SetPageRotation method, saving the result as 'rotated-page.pdf'. For rotating all pages, we use the SetAllPageRotations method, rotating 180° clockwise, saving it as 'rotated-pages.pdf'. Running the project confirms successful rotation, providing enhanced document customization. This tutorial offers a comprehensive guide to rotating text and pages in PDFs, enhancing control over document appearance.

// Program.cs

// Import IronPdf library
using IronPdf;

class Program
{
    static void Main(string[] args)
    {
        // Initialize the IronPdf renderer
        var renderer = new ChromePdfRenderer();

        // Set up any licensing if applicable
        // License.SetLicense("your_license_key"); // Uncomment and insert your license if you have one

        // Example of rotating text inside HTML content
        string htmlContent = @"
            <html>
                <head>
                    <style>
                        .rotated-text {
                            transform: rotate(90deg);
                        }
                    </style>
                </head>
                <body>
                    <div class='rotated-text'>Hello, this text is rotated 90 degrees!</div>
                </body>
            </html>
        ";

        // Render the HTML to PDF
        var pdfDocument = renderer.RenderHtmlAsPdf(htmlContent);

        // Save the PDF document
        pdfDocument.SaveAs("rotated-text.pdf");

        // Example of rotating an existing PDF document
        var existingPdf = PdfDocument.FromFile("input.pdf");

        // Rotate the first page 90 degrees clockwise
        existingPdf.Pages[0].SetPageRotation(90);

        // Save the rotated page PDF
        existingPdf.SaveAs("rotated-page.pdf");

        // Rotate all pages of the PDF 180 degrees
        foreach (var page in existingPdf.Pages)
        {
            page.SetPageRotation(180);
        }

        // Save the PDF with all pages rotated
        existingPdf.SaveAs("rotated-pages.pdf");
    }
}
// Program.cs

// Import IronPdf library
using IronPdf;

class Program
{
    static void Main(string[] args)
    {
        // Initialize the IronPdf renderer
        var renderer = new ChromePdfRenderer();

        // Set up any licensing if applicable
        // License.SetLicense("your_license_key"); // Uncomment and insert your license if you have one

        // Example of rotating text inside HTML content
        string htmlContent = @"
            <html>
                <head>
                    <style>
                        .rotated-text {
                            transform: rotate(90deg);
                        }
                    </style>
                </head>
                <body>
                    <div class='rotated-text'>Hello, this text is rotated 90 degrees!</div>
                </body>
            </html>
        ";

        // Render the HTML to PDF
        var pdfDocument = renderer.RenderHtmlAsPdf(htmlContent);

        // Save the PDF document
        pdfDocument.SaveAs("rotated-text.pdf");

        // Example of rotating an existing PDF document
        var existingPdf = PdfDocument.FromFile("input.pdf");

        // Rotate the first page 90 degrees clockwise
        existingPdf.Pages[0].SetPageRotation(90);

        // Save the rotated page PDF
        existingPdf.SaveAs("rotated-page.pdf");

        // Rotate all pages of the PDF 180 degrees
        foreach (var page in existingPdf.Pages)
        {
            page.SetPageRotation(180);
        }

        // Save the PDF with all pages rotated
        existingPdf.SaveAs("rotated-pages.pdf");
    }
}
' Program.cs

' Import IronPdf library
Imports IronPdf

Friend Class Program
	Shared Sub Main(ByVal args() As String)
		' Initialize the IronPdf renderer
		Dim renderer = New ChromePdfRenderer()

		' Set up any licensing if applicable
		' License.SetLicense("your_license_key"); // Uncomment and insert your license if you have one

		' Example of rotating text inside HTML content
		Dim htmlContent As String = "
            <html>
                <head>
                    <style>
                        .rotated-text {
                            transform: rotate(90deg);
                        }
                    </style>
                </head>
                <body>
                    <div class='rotated-text'>Hello, this text is rotated 90 degrees!</div>
                </body>
            </html>
        "

		' Render the HTML to PDF
		Dim pdfDocument = renderer.RenderHtmlAsPdf(htmlContent)

		' Save the PDF document
		pdfDocument.SaveAs("rotated-text.pdf")

		' Example of rotating an existing PDF document
		Dim existingPdf = PdfDocument.FromFile("input.pdf")

		' Rotate the first page 90 degrees clockwise
		existingPdf.Pages(0).SetPageRotation(90)

		' Save the rotated page PDF
		existingPdf.SaveAs("rotated-page.pdf")

		' Rotate all pages of the PDF 180 degrees
		For Each page In existingPdf.Pages
			page.SetPageRotation(180)
		Next page

		' Save the PDF with all pages rotated
		existingPdf.SaveAs("rotated-pages.pdf")
	End Sub
End Class
$vbLabelText   $csharpLabel

Further Reading: Rotate PDF Text and Pages in .NET

Chipego
Software Engineer
Chipego has a natural skill for listening that helps him to comprehend customer issues, and offer intelligent solutions. He joined the Iron Software team in 2023, after studying a Bachelor of Science in Information Technology. IronPDF and IronOCR are the two products Chipego has been focusing on, but his knowledge of all products is growing daily, as he finds new ways to support customers. He enjoys how collaborative life is at Iron Software, with team members from across the company bringing their varied experience to contribute to effective, innovative solutions. When Chipego is away from his desk, he can often be found enjoying a good book or playing football.