Rotate Pages, Text, Tables, and Images in a PDF Using C#
Live Chat Icon For mobile
Live Chat Icon
Popular Categories.NET  (175).NET Core  (29).NET MAUI  (208)Angular  (109)ASP.NET  (51)ASP.NET Core  (82)ASP.NET MVC  (89)Azure  (41)Black Friday Deal  (1)Blazor  (220)BoldSign  (15)DocIO  (24)Essential JS 2  (107)Essential Studio  (200)File Formats  (67)Flutter  (133)JavaScript  (221)Microsoft  (119)PDF  (81)Python  (1)React  (101)Streamlit  (1)Succinctly series  (131)Syncfusion  (920)TypeScript  (33)Uno Platform  (3)UWP  (4)Vue  (45)Webinar  (51)Windows Forms  (61)WinUI  (68)WPF  (159)Xamarin  (161)XlsIO  (37)Other CategoriesBarcode  (5)BI  (29)Bold BI  (8)Bold Reports  (2)Build conference  (8)Business intelligence  (55)Button  (4)C#  (151)Chart  (132)Cloud  (15)Company  (443)Dashboard  (8)Data Science  (3)Data Validation  (8)DataGrid  (63)Development  (633)Doc  (8)DockingManager  (1)eBook  (99)Enterprise  (22)Entity Framework  (5)Essential Tools  (14)Excel  (41)Extensions  (22)File Manager  (7)Gantt  (18)Gauge  (12)Git  (5)Grid  (31)HTML  (13)Installer  (2)Knockout  (2)Language  (1)LINQPad  (1)Linux  (2)M-Commerce  (1)Metro Studio  (11)Mobile  (508)Mobile MVC  (9)OLAP server  (1)Open source  (1)Orubase  (12)Partners  (21)PDF viewer  (43)Performance  (12)PHP  (2)PivotGrid  (4)Predictive Analytics  (6)Report Server  (3)Reporting  (10)Reporting / Back Office  (11)Rich Text Editor  (12)Road Map  (12)Scheduler  (52)Security  (3)SfDataGrid  (9)Silverlight  (21)Sneak Peek  (31)Solution Services  (4)Spreadsheet  (11)SQL  (11)Stock Chart  (1)Surface  (4)Tablets  (5)Theme  (12)Tips and Tricks  (112)UI  (387)Uncategorized  (68)Unix  (2)User interface  (68)Visual State Manager  (2)Visual Studio  (31)Visual Studio Code  (19)Web  (597)What's new  (333)Windows 8  (19)Windows App  (2)Windows Phone  (15)Windows Phone 7  (9)WinRT  (26)
Rotate Pages, Text, Tables, and Images in a PDF Using C#

Rotate Pages, Text, Tables, and Images in a PDF Using C#

The 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:

Experience a leap in PDF technology with Syncfusion's PDF Library, shaping the future of digital document processing.

Getting started with app creation 

  1. First, create a new C# .NET console application using Visual Studio.Creating a Console App
  2. Then, install the  Syncfusion.Pdf.Net.Core  NuGet package as a reference in your .NET app from the  NuGet Gallery.Install Syncfusion.Pdf.Net.Core NuGet Package

Rotating a page in a PDF document

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#.

//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
Rotating a page in a PDF document

Unleash the full potential of Syncfusion's PDF Library! Explore our advanced resources and empower your apps with cutting-edge functionalities.

Rotating text in a PDF document

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#.

//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.

Rotating text in a PDF document
Rotating text in a PDF document

Rotating an image in a PDF document

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#.

//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.

Rotating an image in a PDF document
Rotating an image in a PDF document

Embark on a virtual tour of Syncfusion's PDF Library through interactive demos.

Rotating a table in a PDF document

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#.

//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.

Rotating a table in a PDF
Rotating a table in a PDF

GitHub reference

For more details, check out the examples in the GitHub repository.

Syncfusion’s high-performance PDF Library allows you to create PDF documents from scratch without Adobe dependencies.

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. Try out these methods and leave your feedback in the comments section of this blog post!

Take a moment to look at the documentation, 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 page. If you are not yet a Syncfusion customer, you can try our 30-day free trial to check out our available features.

For questions, you can contact us through our support forum, support portal, or feedback portal. We are delighted to assist you!

Related blogs

Tags:

Share this post:

Popular Now

Be the first to get updates

Subscribe RSS feed

Be the first to get updates

Subscribe RSS feed