Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
198 changes: 196 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,196 @@
# rotating-a-PDF-document-using-csharp
This repository contains examples for rotating a PDF page, text, table and image in PDF document using C#
# Rotating a PDF using .Net Console

The [Syncfusion PDF Library](https://www.syncfusion.com/document-processing/pdf-framework/net/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](https://github.com/SyncfusionExamples/rotating-a-PDF-document-using-csharp/master/RotatePDFPage) | It means changing the orientation of the page by a specified degree.
[Rotating a text in PDF](https://github.com/SyncfusionExamples/rotating-a-PDF-document-using-csharp/master/RotatePDFText) | It means changing the orientation of the text by a specified degree.
[Rotating an image in PDF](https://github.com/SyncfusionExamples/rotating-a-PDF-document-using-csharp/master/RotatePDFImage) | It means changing the orientation of the image by a specified degree.
[Rotating a Table in PDF](https://github.com/SyncfusionExamples/rotating-a-PDF-document-using-csharp/master/RotatePDFTable) | 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#.

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

<img src="Screenshot/PageOutput.png" alt="Add attachment output" width="100%" Height="Auto"/>


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

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

<img src="Screenshot/TextOutput.png" alt="Add attachment output" width="100%" Height="Auto"/>

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

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

<img src="Screenshot/ImageOutput.png" alt="Add attachment output" width="100%" Height="Auto"/>

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

```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));
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.

<img src="Screenshot/TableOutput.png" alt="Add attachment output" width="100%" Height="Auto"/>


# 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
* **Product page:** [Syncfusion PDF Framework](https://www.syncfusion.com/document-processing/pdf-framework/net)
* **Documentation page:** [Syncfusion .NET PDF library](https://help.syncfusion.com/file-formats/pdf/overview)
* **Online demo:** [Syncfusion .NET PDF library - Online demos](https://ej2.syncfusion.com/aspnetcore/PDF/CompressExistingPDF#/bootstrap5)
* **Blog:** [Syncfusion .NET PDF library - Blog](https://www.syncfusion.com/blogs/category/pdf)
* **Knowledge Base:** [Syncfusion .NET PDF library - Knowledge Base](https://www.syncfusion.com/kb/windowsforms/pdf)
* **EBooks:** [Syncfusion .NET PDF library - EBooks](https://www.syncfusion.com/succinctly-free-ebooks)
* **FAQ:** [Syncfusion .NET PDF library - FAQ](https://www.syncfusion.com/faq/)

# Support and feedback
* For any other queries, reach our [Syncfusion support team](https://www.syncfusion.com/support/directtrac/incidents/newincident?utm_source=github&utm_medium=listing&utm_campaign=github-docio-examples) or post the queries through the [community forums](https://www.syncfusion.com/forums?utm_source=github&utm_medium=listing&utm_campaign=github-docio-examples).
* Request new feature through [Syncfusion feedback portal](https://www.syncfusion.com/feedback?utm_source=github&utm_medium=listing&utm_campaign=github-docio-examples).

# 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](https://www.syncfusion.com/eula/es/?utm_source=github&utm_medium=listing&utm_campaign=github-docio-examples). You can purchase a licnense [here](https://www.syncfusion.com/sales/products?utm_source=github&utm_medium=listing&utm_campaign=github-docio-examples) or start a free 30-day trial [here](https://www.syncfusion.com/account/manage-trials/start-trials?utm_source=github&utm_medium=listing&utm_campaign=github-docio-examples).

# 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](https://www.syncfusion.com/blazor-components?utm_source=github&utm_medium=listing&utm_campaign=github-docio-examples), [ASP.NET Core](https://www.syncfusion.com/aspnet-core-ui-controls?utm_source=github&utm_medium=listing&utm_campaign=github-docio-examples), [ASP.NET MVC](https://www.syncfusion.com/aspnet-mvc-ui-controls?utm_source=github&utm_medium=listing&utm_campaign=github-docio-examples), [ASP.NET WebForms](https://www.syncfusion.com/jquery/aspnet-webforms-ui-controls?utm_source=github&utm_medium=listing&utm_campaign=github-docio-examples), [JavaScript](https://www.syncfusion.com/javascript-ui-controls?utm_source=github&utm_medium=listing&utm_campaign=github-docio-examples), [Angular](https://www.syncfusion.com/angular-ui-components?utm_source=github&utm_medium=listing&utm_campaign=github-docio-examples), [React](https://www.syncfusion.com/react-ui-components?utm_source=github&utm_medium=listing&utm_campaign=github-docio-examples), [Vue](https://www.syncfusion.com/vue-ui-components?utm_source=github&utm_medium=listing&utm_campaign=github-docio-examples), and [Flutter](https://www.syncfusion.com/flutter-widgets?utm_source=github&utm_medium=listing&utm_campaign=github-docio-examples)), mobile ([Xamarin](https://www.syncfusion.com/xamarin-ui-controls?utm_source=github&utm_medium=listing&utm_campaign=github-docio-examples), [Flutter](https://www.syncfusion.com/flutter-widgets?utm_source=github&utm_medium=listing&utm_campaign=github-docio-examples), [UWP](https://www.syncfusion.com/uwp-ui-controls?utm_source=github&utm_medium=listing&utm_campaign=github-docio-examples), and [JavaScript](https://www.syncfusion.com/javascript-ui-controls?utm_source=github&utm_medium=listing&utm_campaign=github-docio-examples)), and desktop development ([WinForms](https://www.syncfusion.com/winforms-ui-controls?utm_source=github&utm_medium=listing&utm_campaign=github-docio-examples), [WPF](https://www.syncfusion.com/wpf-ui-controls?utm_source=github&utm_medium=listing&utm_campaign=github-docio-examples), [WinUI(Preview)](https://www.syncfusion.com/winui-controls?utm_source=github&utm_medium=listing&utm_campaign=github-docio-examples), [Flutter](https://www.syncfusion.com/flutter-widgets?utm_source=github&utm_medium=listing&utm_campaign=github-docio-examples) and [UWP](https://www.syncfusion.com/uwp-ui-controls?utm_source=github&utm_medium=listing&utm_campaign=github-docio-examples)). 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.


25 changes: 25 additions & 0 deletions RotatePDFImage/RotatePDFImage.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.5.33502.453
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "RotatePDFImage", "RotatePDFImage\RotatePDFImage.csproj", "{28D64592-DB58-4678-8ABF-B203AD60B917}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{28D64592-DB58-4678-8ABF-B203AD60B917}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{28D64592-DB58-4678-8ABF-B203AD60B917}.Debug|Any CPU.Build.0 = Debug|Any CPU
{28D64592-DB58-4678-8ABF-B203AD60B917}.Release|Any CPU.ActiveCfg = Release|Any CPU
{28D64592-DB58-4678-8ABF-B203AD60B917}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {E4C8BCD0-EB4C-43EF-9E6E-6816DCCB0DC6}
EndGlobalSection
EndGlobal
Binary file added RotatePDFImage/RotatePDFImage/Data/Input.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
28 changes: 28 additions & 0 deletions RotatePDFImage/RotatePDFImage/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using Syncfusion.Pdf;
using Syncfusion.Pdf.Graphics;

//Created the PDF document.
PdfDocument document = new PdfDocument();
//Add a new page
PdfPage page = document.Pages.Add();

FileStream fileStream = new FileStream("../../../Data/input.jpg", FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
//Load a bitmap
PdfBitmap image = new PdfBitmap(fileStream);
//save the current graphics state
PdfGraphicsState state = page.Graphics.Save();
//Rotate the coordinate system
page.Graphics.RotateTransform(40);
//Draw image
image.Draw(page, 280, 20);
//Restore the graphics state
page.Graphics.Restore(state);

//Create file stream.
using (FileStream outputFileStream = new FileStream(Path.GetFullPath(@"../../../Output.pdf"), FileMode.Create, FileAccess.ReadWrite))
{
//Save the PDF document to file stream.
document.Save(outputFileStream);
}
//Close the document.
document.Close(true);
14 changes: 14 additions & 0 deletions RotatePDFImage/RotatePDFImage/RotatePDFImage.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Syncfusion.Pdf.Net.Core" Version="21.1.38" />
</ItemGroup>

</Project>
25 changes: 25 additions & 0 deletions RotatePDFPage/RotatePDFPage.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.5.33502.453
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "RotatePDFPage", "RotatePDFPage\RotatePDFPage.csproj", "{A65F8D4E-A714-47B1-BA7E-13133310B306}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{A65F8D4E-A714-47B1-BA7E-13133310B306}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{A65F8D4E-A714-47B1-BA7E-13133310B306}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A65F8D4E-A714-47B1-BA7E-13133310B306}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A65F8D4E-A714-47B1-BA7E-13133310B306}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {94774311-868C-4067-B6FB-5068B0A22C7D}
EndGlobalSection
EndGlobal
Binary file added RotatePDFPage/RotatePDFPage/Data/Input.pdf
Binary file not shown.
20 changes: 20 additions & 0 deletions RotatePDFPage/RotatePDFPage/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using Syncfusion.Pdf.Parsing;
using Syncfusion.Pdf;

FileStream inputStream = new FileStream("../../../Data/Input.pdf", FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
//Load the existing PDF document.
PdfLoadedDocument loadedDocument = new PdfLoadedDocument(inputStream);
//Gets the page.
PdfPageBase loadedPage = loadedDocument.Pages[0] as PdfPageBase;

//Set the rotation for loaded page.
loadedPage.Rotation = PdfPageRotateAngle.RotateAngle90;

//Create file stream.
using (FileStream outputFileStream = new FileStream(Path.GetFullPath(@"../../../Output.pdf"), FileMode.Create, FileAccess.ReadWrite))
{
//Save the PDF document to file stream.
loadedDocument.Save(outputFileStream);
}
//Close the document.
loadedDocument.Close(true);
14 changes: 14 additions & 0 deletions RotatePDFPage/RotatePDFPage/RotatePDFPage.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Syncfusion.Pdf.Net.Core" Version="21.1.38" />
</ItemGroup>

</Project>
25 changes: 25 additions & 0 deletions RotatePDFTable/RotatePDFTable.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.5.33502.453
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "RotatePDFTable", "RotatePDFTable\RotatePDFTable.csproj", "{52170CC2-49DA-46BB-9C3C-C8542C133CE5}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{52170CC2-49DA-46BB-9C3C-C8542C133CE5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{52170CC2-49DA-46BB-9C3C-C8542C133CE5}.Debug|Any CPU.Build.0 = Debug|Any CPU
{52170CC2-49DA-46BB-9C3C-C8542C133CE5}.Release|Any CPU.ActiveCfg = Release|Any CPU
{52170CC2-49DA-46BB-9C3C-C8542C133CE5}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {8DA4A816-B8BF-493B-A013-C58EA0C37B86}
EndGlobalSection
EndGlobal
Loading