Rotate PDF Text and Pages in .NET

Rotating PDF text or pages refers to changing the orientation of either the entire page or specific text elements within a PDF document. This rotation can be done in degrees (typically 90, 180, or 270 degrees) to reposition content either clockwise or counterclockwise.


C# NuGet Library for PDF

Install with NuGet

Install-Package IronPdf
or
Java PDF JAR

Download DLL

Download DLL

Manually install into your project

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
using IronPdf;

// Import PDF
PdfDocument pdf = PdfDocument.FromFile("multi-page.pdf");

// Set rotation for a single page
pdf.SetPageRotation(0, PdfPageRotation.Clockwise90);

// Set rotation for multiple pages
pdf.SetPageRotations(Enumerable.Range(1,3), PdfPageRotation.Clockwise270);

// Set rotation for the entire document
pdf.SetAllPageRotations(PdfPageRotation.Clockwise180);

pdf.SaveAs("rotated.pdf");
Imports IronPdf

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

' Set rotation for a single page
pdf.SetPageRotation(0, PdfPageRotation.Clockwise90)

' Set rotation for multiple pages
pdf.SetPageRotations(Enumerable.Range(1,3), PdfPageRotation.Clockwise270)

' Set rotation for the entire document
pdf.SetAllPageRotations(PdfPageRotation.Clockwise180)

pdf.SaveAs("rotated.pdf")
VB   C#

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 PDF .NET Library you installed earlier. This is achieved using the -webkit-transform: rotate CSS3 style, which can rotate any HTML element to 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:

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

var renderer = new IronPdf.ChromePdfRenderer();

var pdf = renderer.RenderHtmlAsPdf(@"
<html>
<head>
 <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");
Imports IronPdf

Private renderer = New IronPdf.ChromePdfRenderer()

Private pdf = renderer.RenderHtmlAsPdf("
<html>
<head>
 <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")
VB   C#