Rotate Pages, Text, Tables, and Images in a PDF Using C#
Tags: fileformats, csharp, pdf, dotnet
Published: 2023-06-26 12:50:10

---
title: Rotate Pages, Text, Tables, and Images in a PDF Using C#
published: true
date: 2023-06-26 11:49:53 UTC
tags: fileformats, csharp, pdf, dotnet
canonical_url: https://www.syncfusion.com/blogs/post/rotate-page-text-table-image-pdf-csharp.aspx
cover_image: https://dev-to-uploads.s3.amazonaws.com/uploads/articles/phi0y5mv4wpx8mgyqkmz.png
---
The [Syncfusion .NET PDF Library](https://www.syncfusion.com/pdf-framework/net/pdf-library "Syncfusion .NET PDF Library") provides support for rotating the elements in a PDF document by setting the rotation angle of the elements.  

This article will cover the process of rotating a page, text, table, and an image in a PDF file using the Syncfusion .NET PDF Library.

**Agenda** :

- [Rotating a page in a PDF](#page)
- [Rotating text in a PDF](#text)
- [Rotating a table in a PDF](#table)
- [Rotating an image in a PDF](#image)

## Getting started with app creation 

1. First, create a new C# .NET console application using [Visual Studio](https://visualstudio.microsoft.com/ "Visual Studio"). ![Creating a Console App](https://www.syncfusion.com/blogs/wp-content/uploads/2023/06/Console-App-1.png)
2. Then, install the  [Syncfusion.Pdf.Net.Core ](https://www.nuget.org/packages/Syncfusion.Pdf.Net.Core "Syncfusion.Pdf.Net.Core  NuGet Package") NuGet package as a reference in your .NET app from the [NuGet Gallery](https://www.nuget.org/ "NuGet Gallery"). ![Install Syncfusion.Pdf.Net.Core NuGet Package](https://www.syncfusion.com/blogs/wp-content/uploads/2023/06/NuGet-Package..png)

##<a name="page">Rotating a page in a PDF document</a>

Rotating a PDF page means changing the orientation of the page to a specified degree.

For example, rotating a page by 90 degrees would turn it from portrait to landscape orientation or vice versa. This can be useful when a page has been scanned or saved in an inappropriate orientation or if you want to view a page from a different perspective. The rotation can be done either clockwise or counterclockwise. 

The following code example shows how to rotate a page in a PDF document using C#.

```csharp
//Load PDF document as a stream.  
FileStream inputStream = new FileStream("Input.pdf", FileMode.Open, FileAccess.Read, FileShare.ReadWrite); 
//Load the existing PDF document.  
PdfLoadedDocument loadedDocument = new PdfLoadedDocument(inputStream); 
for (int i = 0; i < loadedDocument.PageCount; i++) 
{ 
    //Gets the page. 
    PdfPageBase loadedPage = loadedDocument.Pages[i] as PdfPageBase; 
    //Set the rotation for the loaded page.  
    loadedPage.Rotation = PdfPageRotateAngle.RotateAngle90; 
} 
//Create the file stream to save the PDF document.   
using(FileStream fileStream = new FileStream("Output.pdf", FileMode.CreateNew, FileAccess.ReadWrite)) 
{ 
  loadedDocument.Save(fileStream); 
} 
//Close the document.  
loadedDocument.Close(true);
```

By executing this code example, you will get a PDF document like in the following screenshot.

![Rotating a page in a PDF document](https://www.syncfusion.com/blogs/wp-content/uploads/2023/06/Rotating-a-page-in-a-PDF-document.png)
_Rotating a page in a PDF document_

##<a name="text">Rotating text in a PDF document</a>

Rotating PDF text refers to changing the orientation of the text from its default horizontal alignment to a vertical or diagonal alignment. This can be useful in various scenarios, such as fitting more text on a page, improving the visual appeal of a document, or making the text easier to read in certain circumstances. 

The following code example shows how to rotate text in a PDF document using C#.

```csharp
//Create a new PDF document. 
PdfDocument pdfDocument = new PdfDocument();
//Add a page to the PDF document. 
PdfPage pdfPage = pdfDocument.Pages.Add();
//Create PDF graphics for the page. 
PdfGraphics graphics = pdfPage.Graphics;
//Set the font. 
PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 20);
//Add watermark text. 
PdfGraphicsState state = graphics.Save();
graphics.RotateTransform(-40);
graphics.DrawString("Rotating a text in PDF document", font, PdfPens.Red, PdfBrushes.Red, new PointF(-150, 450));
//Create the file stream to save the PDF document.  
using (FileStream outputStream = new FileStream("Output.pdf", FileMode.CreateNew, FileAccess.ReadWrite))
{
    pdfDocument.Save(outputStream);
}
//Close the document. 
pdfDocument.Close(true);
```

By executing this code, you will get a PDF like in the following screenshot.
<figure>

  <img src="https://www.syncfusion.com/blogs/wp-content/uploads/2023/06/Rotating-text-in-a-PDF-document-1.png" alt="Rotating text in a PDF document" style="width:100%">

  <figcaption>Rotating text in a PDF document</figcaption>

</figure>

##<a name="image">Rotating an image in a PDF document</a>

We can also rotate an image in a PDF using the Syncfusion .NET PDF Library. This involves changing the orientation of the image embedded in the document to a specified degree.

For example, rotating an image by 90 degrees turns it from portrait to landscape orientation or vice versa. The rotation can be done either clockwise or counterclockwise. 

The following code example shows how to rotate an image in a PDF document using C#.

```csharp
//Created the PDF document. 
PdfDocument document = new PdfDocument();
//Add a new page. 
PdfPage page = document.Pages.Add();
FileStream imageStream = new FileStream("Input.png", FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
//Load a bitmap. 
PdfBitmap image = new PdfBitmap(imageStream);
//Save the current graphics state. 
PdfGraphicsState state = page.Graphics.Save();
//Rotate the coordinate system. 
page.Graphics.RotateTransform(-40);
//Draw image. 
image.Draw(page, -150, 450);
//Restore the graphics state. 
page.Graphics.Restore(state);
//Create the file stream to save the PDF document.  
using (FileStream fileStream = new FileStream("Output.pdf", FileMode.CreateNew, FileAccess.ReadWrite))
{
    document.Save(fileStream);
}
//Close the document. 
document.Close(true);
```

By executing this code example, we will get a PDF document like in the following screenshot.
<figure>

  <img src="https://www.syncfusion.com/blogs/wp-content/uploads/2023/06/Rotating-an-image-in-a-PDF-document.png" alt="Rotating an image in a PDF document" style="width:100%">

  <figcaption>Rotating an image in a PDF document</figcaption>

</figure>

##<a name="table">Rotating a table in a PDF document</a>

By rotating a table in a PDF, we can change the orientation from its original position to a different angle. This can be useful when the table’s original orientation is not convenient for viewing or when the table’s data needs to be presented in a different orientation. 

The following code example shows how to rotate a table in a PDF document using C#.

```csharp
//Create a new PDF document.  
PdfDocument document = new PdfDocument(); 
//Add a page.  
PdfPage page = document.Pages.Add(); 
//Create a PdfGrid.  
PdfGrid pdfGrid = new PdfGrid(); 
//Add a handler to rotate the table.  
pdfGrid.BeginPageLayout += PdfGrid_BeginPageLayout; 
//Create a DataTable.  
DataTable dataTable = new DataTable(); 
//Add columns to the DataTable.  
dataTable.Columns.Add("ID", typeof(int)); 
dataTable.Columns.Add("Name", typeof(string)); 
//Add rows to the DataTable.  
for (int i = 0; i < 10; i++) 
{ 
    dataTable.Rows.Add(57, "AAA"); 
    dataTable.Rows.Add(130, "BBB"); 
} 
//Assign data source.  
pdfGrid.DataSource = dataTable; 
//Set a repeat header for the table.   
pdfGrid.RepeatHeader = true; 
//Draw a grid to the page of a PDF document.  
pdfGrid.Draw(page, new RectangleF(100, 20, 0, page.GetClientSize().Width)); 
//Create the file stream to save the PDF document.   
using (FileStream fileStream = new FileStream("Output.pdf", FileMode.CreateNew, FileAccess.ReadWrite)) 
{ 
    document.Save(fileStream); 
} 
//Close the document.  
document.Close(true); 
void PdfGrid_BeginPageLayout(object sender, Syncfusion.Pdf.Graphics.BeginPageLayoutEventArgs e) 
{ 
    PdfPage page = e.Page; 
    PdfGraphics graphics = e.Page.Graphics; 
    //Translate the coordinate system to the required position.  
    graphics.TranslateTransform(page.GetClientSize().Width, 0); 
    //Rotate the coordinate system.  
    graphics.RotateTransform(90); 
}
```

By executing this code example, we will get a PDF document like in the following screenshot.
<figure>

  <img src="https://www.syncfusion.com/blogs/wp-content/uploads/2023/06/Rotating-a-table-in-a-PDF.png" alt="Rotating a table in a PDF" style="width:100%">

  <figcaption>Rotating a table in a PDF</figcaption>

</figure>

## GitHub reference

For more details, check out the examples in the [GitHub repository](https://github.com/SyncfusionExamples/rotating-a-PDF-document-using-csharp "Rotating elements in a PDF using C# GitHub demo").

## Conclusion

Thanks for reading! In this blog, we have learned how to rotate pages, text, tables, and images in a PDF using the [Syncfusion .NET PDF Library](https://www.syncfusion.com/pdf-framework/net/pdf-library "Syncfusion PDF Library"). Try out these methods and leave your feedback in the comments section of this blog post!

Take a moment to look at the [documentation](https://help.syncfusion.com/file-formats/pdf/working-with-pages#rotating-a-pdf-page "Rotating a page in a PDF"), where you will find other options and features, all with accompanying code samples.

For existing customers, the new version of Essential Studio is available for download from the [License and Downloads](https://www.syncfusion.com/Account/Login "Essential Studio License and Downloads Page") page. If you are not yet a Syncfusion customer, you can try our 30-day [free trial](https://www.syncfusion.com/downloads "Free evaluation of the Essential Studio products") to check out our available features.

For questions, you can contact us through our [support forum](https://www.syncfusion.com/forums "Syncfusion Support Forum"), [support portal](https://support.syncfusion.com/ "Syncfusion Support Portal"), or [feedback portal](https://www.syncfusion.com/feedback/pdf "Syncfusion Feedback Portal"). We are delighted to assist you!

## Related blogs

- [Easily Create, Update, and Remove Bookmarks in PDFs Using C#](https://www.syncfusion.com/blogs/post/bookmarks-in-pdf-csharp.aspx "Blog: Easily Create, Update, and Remove Bookmarks in PDFs Using C#")
- [Easily Add and Remove Attachments in Your PDF Documents Using C#](https://www.syncfusion.com/blogs/post/add-remove-attachment-pdf-csharp.aspx "Blog: Easily Add and Remove Attachments in Your PDF Documents Using C#")
- [Step-by-Step Guide to Adding and Removing Watermarks in PDF using C#](https://www.syncfusion.com/blogs/post/add-remove-watermark-pdf-csharp.aspx "Blog: Step-by-Step Guide to Adding and Removing Watermarks in PDF using C#")
- [Create a PDF Digital Signature Web Service Using Azure Key Vault and the Syncfusion C# PDF Library](https://www.syncfusion.com/blogs/post/pdf-digital-signature-web-service-using-azure-key-vault-and-csharp-pdf-library.aspx "Blog: Create a PDF Digital Signature Web Service Using Azure Key Vault and the Syncfusion C# PDF Library")