Rotate PDF Text and Pages in .NET

After converting HTML to PDF in .NET, we may need to rotate text or entire pages programmatically. A common request includes rendering vertically aligned text to a PDF using HTML5. We can do this using CSS3 and the IronPDF C# library. Let's see how.

/**
Rotate PDF Text
anchor-use-css-to-rotate-pdf-text
**/

var Renderer = new IronPdf.ChromePdfRenderer();

Renderer.RenderingOptions.InputEncoding = System.Text.Encoding.UTF8;

using var Pdf = Renderer.RenderHtmlAsPdf(@"

<html>

<head>

 <meta charset = 'utf-8'>

 <style>

  .rotated{

  -webkit-transform: rotate(-180deg);

  width:400;

  height:400;

  }

</style>

</head>

<body>

<p class='rotated'>Rotated Text</p>

</body>

</html>

");

Pdf.SaveAs("rotated.pdf");
/**
Rotate PDF Text
anchor-use-css-to-rotate-pdf-text
**/

var Renderer = new IronPdf.ChromePdfRenderer();

Renderer.RenderingOptions.InputEncoding = System.Text.Encoding.UTF8;

using var Pdf = Renderer.RenderHtmlAsPdf(@"

<html>

<head>

 <meta charset = 'utf-8'>

 <style>

  .rotated{

  -webkit-transform: rotate(-180deg);

  width:400;

  height:400;

  }

</style>

</head>

<body>

<p class='rotated'>Rotated Text</p>

</body>

</html>

");

Pdf.SaveAs("rotated.pdf");
'''
'''Rotate PDF Text
'''anchor-use-css-to-rotate-pdf-text
'''*

Dim Renderer = New IronPdf.ChromePdfRenderer()

Renderer.RenderingOptions.InputEncoding = System.Text.Encoding.UTF8

Using Pdf = Renderer.RenderHtmlAsPdf("

<html>

<head>

 <meta charset = 'utf-8'>

 <style>

  .rotated{

  -webkit-transform: rotate(-180deg);

  width:400;

  height:400;

  }

</style>

</head>

<body>

<p class='rotated'>Rotated Text</p>

</body>

</html>

")
	
	Pdf.SaveAs("rotated.pdf")
End Using
VB   C#

Step 1

1. Install IronPDF to Project

With just a few lines of code using the IronPDF C# Library, you can rotate PDF text and pages programmatically. The software is free for development with step-by-step examples below.

Access the ZIP file or you can use access it using NuGet.


 PM > Install-Package IronPdf

How To Tutorial

2. Use CSS3 to Rotate PDF Text

CSS3 helps to rotate text to any angle after a PDF to HTML conversion, while using the PDF .NET Library you installed before. This is done through the -webkit-transform: rotate CSS3 style, which may be used to rotate any HTML element though any angle.

-webkit-transform allows many types of 3D and 3D rotational transforms and effects for HTML elements.

A short example of C# HTML to PDF with text rotated 180 degrees is:


3. Rotate PDF Pages Programmatically

A PDF page may be rotated programmatically in .NET with the use of the IronPdf.PdfDocument.RotatePage Rotate PDF Page Method.

Alternatively, the IronPdf.PdfDocument.RotateAllPages Rotate All PDF Pages method can be used to rotate an entire PDF in a single line of C#, F# or Visual Basic. Here is an example.

/**
Rotate PDF Page
anchor-rotate-pdf-pages-programmatically
**/
<style>
th.short-name{

width: 300px;

}

th.rotate{

height: 175px;

white-space: nowrap;

}

th.rotate>div{

transform: translate(0px,-5px)rotate(280deg);

-webkit-transform: translate(0px,-5px)rotate(280deg);

-o-transform: translate(0px,-5px)rotate(280deg);

-moz-transform: translate(0px,-5px)rotate(280deg);

width: 30px;

}

th.rotate>div>span{

padding: 1px1px;

}
</style>

Cshtml:

<tr>

<th class="short-name">Short Name</th>

@foreach(var col in colSet1)
  {

  <th class="rotate"><div><span>@col</span></div></th>

  }

</tr>
HTML