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 Text & Pages Programmatically
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:
var Renderer = new IronPdf.HtmlToPdf();
Renderer.PrintOptions.InputEncoding = System.Text.Encoding.UTF8;
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");
var Renderer = new IronPdf.HtmlToPdf();
Renderer.PrintOptions.InputEncoding = System.Text.Encoding.UTF8;
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");
Dim Renderer = New IronPdf.HtmlToPdf()
Renderer.PrintOptions.InputEncoding = System.Text.Encoding.UTF8
Dim 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")
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.
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;
}
Cshtml:
<tr>
<thclass="short-name">Short Name</th>
@foreach(var col in colSet1)
{
<thclass="rotate"><div><span>@col</span></div></th>
}
</tr>
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;
}
Cshtml:
<tr>
<thclass="short-name">Short Name</th>
@foreach(var col in colSet1)
{
<thclass="rotate"><div><span>@col</span></div></th>
}
</tr>
Th.short-name
If True Then
width:
300px
End If
Th.rotate
If True Then
height:
175px
white-space: nowrap
End If
'INSTANT VB TODO TASK: Local functions are not converted by Instant VB:
'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;
'
'}
'INSTANT VB TODO TASK: Local functions are not converted by Instant VB:
'th.rotate> div> span
'{
'
'padding:
'1px1px;
'
'}
Cshtml:
'INSTANT VB TODO TASK: Local functions are not converted by Instant VB:
'(Of tr) <thclass="short-name"> @Short Name</th> @foreach(var col in colSet1)
'
'{
'
'<thclass="rotate"><div><span> @col</span></div></th>
'
'}
'INSTANT VB TODO TASK: The following line uses invalid syntax:
'</tr>