Skip to content

This repository contains examples for rotating a PDF page, text, table and image in PDF document using C#

Notifications You must be signed in to change notification settings

SyncfusionExamples/rotating-a-PDF-document-using-csharp

Repository files navigation

Rotating a PDF using .Net Console

The Syncfusion PDF Library provides support to rotating a PDF document. The library also provides APIs for changing the orientation of a PDF document by a specified degree.

This article will cover the process to rotate a page, text, image and table from a PDF document using the Syncfusion .NET PDF library. The topics related to this will be discussed in the following sections of this post:

Name Description
Rotating a page in PDF It means changing the orientation of the page by a specified degree.
Rotating a text in PDF It means changing the orientation of the text by a specified degree.
Rotating an image in PDF It means changing the orientation of the image by a specified degree.
Rotating a Table in PDF It means changing the orientation of the table by a specified degree.

Rotating a page in PDF using C#

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

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

//Load PDF document as 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 loaded page.  
    loadedPage.Rotation = PdfPageRotateAngle.RotateAngle90; 
} 
//Create the filestream 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 the following screenshot.

Add attachment output

Rotating a text in PDF using C#

Rotating a PDF text means changing the orientation of the text by a specified degree.

The following code example shows how to rotate a text to a PDF file 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 filestream to save the PDF document.  
FileStream fileStream = new FileStream("Output.pdf", FileMode.CreateNew, FileAccess.ReadWrite); 
pdfDocument.Save(fileStream); 
//Close the document. 
pdfDocument.Close(true);

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

Add attachment output

Rotating an image in PDF using C#

Rotating a PDF image means changing the orientation of the image by a specified degree.

The following code example shows how to rotate an image to a PDF file 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 filestream to save the PDF document.  
FileStream fileStream = new FileStream("Output.pdf", FileMode.CreateNew, FileAccess.ReadWrite); 
document.Save(fileStream); 
//Close the document. 
document.Close(true); 

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

Add attachment output

Rotating a table in PDF using C#

Rotating a PDF table means changing the orientation of the table by a specified degree.

The following code example shows how to rotate an table to a PDF file 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)); 
dataTable.Columns.Add("Type", typeof(string)); 
dataTable.Columns.Add("Date", typeof(DateTime)); 
//Add rows to the DataTable. 
for (int i = 0; i < 10; i++) 
{ 
    dataTable.Rows.Add(57, "AAA", "ABC", DateTime.Now); 
    dataTable.Rows.Add(130, "BBB", "BCD", DateTime.Now); 
    dataTable.Rows.Add(92, "CCC", "CDE", DateTime.Now); 
} 
//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 filestream to save the PDF document.  
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, you will get a PDF document like the following screenshot.

Add attachment output

How to run the examples

  • Download this project to a location in your disk.
  • Open the solution file using Visual Studio.
  • Rebuild the solution to install the required NuGet package.
  • Run the application.

Resources

Support and feedback

License

This is a commercial product and requires a paid license for possession or use. Syncfusion’s licensed software, including this component, is subject to the terms and conditions of Syncfusion's EULA. You can purchase a licnense here or start a free 30-day trial here.

About Syncfusion

Founded in 2001 and headquartered in Research Triangle Park, N.C., Syncfusion has more than 26,000+ customers and more than 1 million users, including large financial institutions, Fortune 500 companies, and global IT consultancies.

Today, we provide 1600+ components and frameworks for web (Blazor, ASP.NET Core, ASP.NET MVC, ASP.NET WebForms, JavaScript, Angular, React, Vue, and Flutter), mobile (Xamarin, Flutter, UWP, and JavaScript), and desktop development (WinForms, WPF, WinUI(Preview), Flutter and UWP). We provide ready-to-deploy enterprise software for dashboards, reports, data integration, and big data processing. Many customers have saved millions in licensing fees by deploying our software.

About

This repository contains examples for rotating a PDF page, text, table and image in PDF document using C#

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages