Skip to content

Commit bb779f9

Browse files
Merge pull request #1 from SyncfusionExamples/823269
823269 Added Rotate PDF Blog sample project
2 parents f6cff35 + 9b7e3ef commit bb779f9

File tree

19 files changed

+479
-2
lines changed

19 files changed

+479
-2
lines changed

README.md

Lines changed: 196 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,196 @@
1-
# rotating-a-PDF-document-using-csharp
2-
This repository contains examples for rotating a PDF page, text, table and image in PDF document using C#
1+
# Rotating a PDF using .Net Console
2+
3+
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.
4+
5+
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:
6+
7+
Name | Description
8+
--- | ---
9+
[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.
10+
[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.
11+
[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.
12+
[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.
13+
14+
15+
16+
## Rotating a page in PDF using C#
17+
18+
Rotating a PDF page means changing the orientation of the page by a specified degree.
19+
20+
The following code example shows how to rotate a page to a PDF file using C#.
21+
22+
```csharp
23+
//Load PDF document as stream.
24+
FileStream inputStream = new FileStream("Input.pdf", FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
25+
//Load the existing PDF document.
26+
PdfLoadedDocument loadedDocument = new PdfLoadedDocument(inputStream);
27+
for (int i = 0; i < loadedDocument.PageCount; i++)
28+
{
29+
//Gets the page.
30+
PdfPageBase loadedPage = loadedDocument.Pages[i] as PdfPageBase;
31+
//Set the rotation for loaded page.
32+
loadedPage.Rotation = PdfPageRotateAngle.RotateAngle90;
33+
}
34+
//Create the filestream to save the PDF document.
35+
using(FileStream fileStream = new FileStream("Output.pdf", FileMode.CreateNew, FileAccess.ReadWrite))
36+
{
37+
loadedDocument.Save(fileStream);
38+
}
39+
//Close the document.
40+
loadedDocument.Close(true);
41+
```
42+
By executing this code example, you will get a PDF document like the following screenshot.
43+
44+
<img src="Screenshot/PageOutput.png" alt="Add attachment output" width="100%" Height="Auto"/>
45+
46+
47+
## Rotating a text in PDF using C#
48+
49+
Rotating a PDF text means changing the orientation of the text by a specified degree.
50+
51+
The following code example shows how to rotate a text to a PDF file using C#.
52+
53+
```csharp
54+
//Create a new PDF document.
55+
PdfDocument pdfDocument = new PdfDocument();
56+
//Add a page to the PDF document.
57+
PdfPage pdfPage = pdfDocument.Pages.Add();
58+
//Create PDF graphics for the page.
59+
PdfGraphics graphics = pdfPage.Graphics;
60+
//Set the font.
61+
PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 20);
62+
//Add watermark text.
63+
PdfGraphicsState state = graphics.Save();
64+
graphics.RotateTransform(-40);
65+
graphics.DrawString("Rotating a text in PDF document", font, PdfPens.Red, PdfBrushes.Red, new PointF(-150, 450));
66+
67+
//Create the filestream to save the PDF document.
68+
FileStream fileStream = new FileStream("Output.pdf", FileMode.CreateNew, FileAccess.ReadWrite);
69+
pdfDocument.Save(fileStream);
70+
//Close the document.
71+
pdfDocument.Close(true);
72+
```
73+
By executing this code example, you will get a PDF document like the following screenshot.
74+
75+
<img src="Screenshot/TextOutput.png" alt="Add attachment output" width="100%" Height="Auto"/>
76+
77+
## Rotating an image in PDF using C#
78+
79+
Rotating a PDF image means changing the orientation of the image by a specified degree.
80+
81+
The following code example shows how to rotate an image to a PDF file using C#.
82+
83+
```csharp
84+
//Created the PDF document.
85+
PdfDocument document = new PdfDocument();
86+
//Add a new page
87+
PdfPage page = document.Pages.Add();
88+
FileStream imageStream = new FileStream("Input.png", FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
89+
//Load a bitmap
90+
PdfBitmap image = new PdfBitmap(imageStream);
91+
//save the current graphics state
92+
PdfGraphicsState state = page.Graphics.Save();
93+
//Rotate the coordinate system
94+
page.Graphics.RotateTransform(-40);
95+
//Draw image
96+
image.Draw(page, -150, 450);
97+
//Restore the graphics state
98+
page.Graphics.Restore(state);
99+
100+
//Create the filestream to save the PDF document.
101+
FileStream fileStream = new FileStream("Output.pdf", FileMode.CreateNew, FileAccess.ReadWrite);
102+
document.Save(fileStream);
103+
//Close the document.
104+
document.Close(true);
105+
```
106+
By executing this code example, you will get a PDF document like the following screenshot.
107+
108+
<img src="Screenshot/ImageOutput.png" alt="Add attachment output" width="100%" Height="Auto"/>
109+
110+
## Rotating a table in PDF using C#
111+
112+
Rotating a PDF table means changing the orientation of the table by a specified degree.
113+
114+
The following code example shows how to rotate an table to a PDF file using C#.
115+
116+
```csharp
117+
//Create a new PDF document.
118+
PdfDocument document = new PdfDocument();
119+
//Add a page.
120+
PdfPage page = document.Pages.Add();
121+
//Create a PdfGrid.
122+
PdfGrid pdfGrid = new PdfGrid();
123+
//Add a handler to rotate the table.
124+
pdfGrid.BeginPageLayout += PdfGrid_BeginPageLayout;
125+
//Create a DataTable.
126+
DataTable dataTable = new DataTable();
127+
128+
//Add columns to the DataTable.
129+
dataTable.Columns.Add("ID", typeof(int));
130+
dataTable.Columns.Add("Name", typeof(string));
131+
dataTable.Columns.Add("Type", typeof(string));
132+
dataTable.Columns.Add("Date", typeof(DateTime));
133+
//Add rows to the DataTable.
134+
for (int i = 0; i < 10; i++)
135+
{
136+
dataTable.Rows.Add(57, "AAA", "ABC", DateTime.Now);
137+
dataTable.Rows.Add(130, "BBB", "BCD", DateTime.Now);
138+
dataTable.Rows.Add(92, "CCC", "CDE", DateTime.Now);
139+
}
140+
//Assign data source.
141+
pdfGrid.DataSource = dataTable;
142+
//Set a repeat header for the table.
143+
pdfGrid.RepeatHeader = true;
144+
//Draw a grid to the page of a PDF document.
145+
pdfGrid.Draw(page, new RectangleF(100, 20, 0, page.GetClientSize().Width));
146+
147+
//Create the filestream to save the PDF document.
148+
FileStream fileStream = new FileStream("Output.pdf", FileMode.CreateNew, FileAccess.ReadWrite);
149+
document.Save(fileStream);
150+
//Close the document.
151+
document.Close(true);
152+
153+
154+
void PdfGrid_BeginPageLayout(object sender, Syncfusion.Pdf.Graphics.BeginPageLayoutEventArgs e)
155+
{
156+
PdfPage page = e.Page;
157+
PdfGraphics graphics = e.Page.Graphics;
158+
//Translate the coordinate system to the required position.
159+
graphics.TranslateTransform(page.GetClientSize().Width, 0);
160+
//Rotate the coordinate system.
161+
graphics.RotateTransform(90);
162+
}
163+
```
164+
By executing this code example, you will get a PDF document like the following screenshot.
165+
166+
<img src="Screenshot/TableOutput.png" alt="Add attachment output" width="100%" Height="Auto"/>
167+
168+
169+
# How to run the examples
170+
* Download this project to a location in your disk.
171+
* Open the solution file using Visual Studio.
172+
* Rebuild the solution to install the required NuGet package.
173+
* Run the application.
174+
175+
# Resources
176+
* **Product page:** [Syncfusion PDF Framework](https://www.syncfusion.com/document-processing/pdf-framework/net)
177+
* **Documentation page:** [Syncfusion .NET PDF library](https://help.syncfusion.com/file-formats/pdf/overview)
178+
* **Online demo:** [Syncfusion .NET PDF library - Online demos](https://ej2.syncfusion.com/aspnetcore/PDF/CompressExistingPDF#/bootstrap5)
179+
* **Blog:** [Syncfusion .NET PDF library - Blog](https://www.syncfusion.com/blogs/category/pdf)
180+
* **Knowledge Base:** [Syncfusion .NET PDF library - Knowledge Base](https://www.syncfusion.com/kb/windowsforms/pdf)
181+
* **EBooks:** [Syncfusion .NET PDF library - EBooks](https://www.syncfusion.com/succinctly-free-ebooks)
182+
* **FAQ:** [Syncfusion .NET PDF library - FAQ](https://www.syncfusion.com/faq/)
183+
184+
# Support and feedback
185+
* 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).
186+
* Request new feature through [Syncfusion feedback portal](https://www.syncfusion.com/feedback?utm_source=github&utm_medium=listing&utm_campaign=github-docio-examples).
187+
188+
# License
189+
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).
190+
191+
# About Syncfusion
192+
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.
193+
194+
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.
195+
196+

RotatePDFImage/RotatePDFImage.sln

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.5.33502.453
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "RotatePDFImage", "RotatePDFImage\RotatePDFImage.csproj", "{28D64592-DB58-4678-8ABF-B203AD60B917}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{28D64592-DB58-4678-8ABF-B203AD60B917}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{28D64592-DB58-4678-8ABF-B203AD60B917}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{28D64592-DB58-4678-8ABF-B203AD60B917}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{28D64592-DB58-4678-8ABF-B203AD60B917}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
GlobalSection(ExtensibilityGlobals) = postSolution
23+
SolutionGuid = {E4C8BCD0-EB4C-43EF-9E6E-6816DCCB0DC6}
24+
EndGlobalSection
25+
EndGlobal
3.44 KB
Loading
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
using Syncfusion.Pdf;
2+
using Syncfusion.Pdf.Graphics;
3+
4+
//Created the PDF document.
5+
PdfDocument document = new PdfDocument();
6+
//Add a new page
7+
PdfPage page = document.Pages.Add();
8+
9+
FileStream fileStream = new FileStream("../../../Data/input.jpg", FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
10+
//Load a bitmap
11+
PdfBitmap image = new PdfBitmap(fileStream);
12+
//save the current graphics state
13+
PdfGraphicsState state = page.Graphics.Save();
14+
//Rotate the coordinate system
15+
page.Graphics.RotateTransform(40);
16+
//Draw image
17+
image.Draw(page, 280, 20);
18+
//Restore the graphics state
19+
page.Graphics.Restore(state);
20+
21+
//Create file stream.
22+
using (FileStream outputFileStream = new FileStream(Path.GetFullPath(@"../../../Output.pdf"), FileMode.Create, FileAccess.ReadWrite))
23+
{
24+
//Save the PDF document to file stream.
25+
document.Save(outputFileStream);
26+
}
27+
//Close the document.
28+
document.Close(true);
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net6.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<PackageReference Include="Syncfusion.Pdf.Net.Core" Version="21.1.38" />
12+
</ItemGroup>
13+
14+
</Project>

RotatePDFPage/RotatePDFPage.sln

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.5.33502.453
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "RotatePDFPage", "RotatePDFPage\RotatePDFPage.csproj", "{A65F8D4E-A714-47B1-BA7E-13133310B306}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{A65F8D4E-A714-47B1-BA7E-13133310B306}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{A65F8D4E-A714-47B1-BA7E-13133310B306}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{A65F8D4E-A714-47B1-BA7E-13133310B306}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{A65F8D4E-A714-47B1-BA7E-13133310B306}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
GlobalSection(ExtensibilityGlobals) = postSolution
23+
SolutionGuid = {94774311-868C-4067-B6FB-5068B0A22C7D}
24+
EndGlobalSection
25+
EndGlobal
3.24 KB
Binary file not shown.
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using Syncfusion.Pdf.Parsing;
2+
using Syncfusion.Pdf;
3+
4+
FileStream inputStream = new FileStream("../../../Data/Input.pdf", FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
5+
//Load the existing PDF document.
6+
PdfLoadedDocument loadedDocument = new PdfLoadedDocument(inputStream);
7+
//Gets the page.
8+
PdfPageBase loadedPage = loadedDocument.Pages[0] as PdfPageBase;
9+
10+
//Set the rotation for loaded page.
11+
loadedPage.Rotation = PdfPageRotateAngle.RotateAngle90;
12+
13+
//Create file stream.
14+
using (FileStream outputFileStream = new FileStream(Path.GetFullPath(@"../../../Output.pdf"), FileMode.Create, FileAccess.ReadWrite))
15+
{
16+
//Save the PDF document to file stream.
17+
loadedDocument.Save(outputFileStream);
18+
}
19+
//Close the document.
20+
loadedDocument.Close(true);
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net6.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<PackageReference Include="Syncfusion.Pdf.Net.Core" Version="21.1.38" />
12+
</ItemGroup>
13+
14+
</Project>

RotatePDFTable/RotatePDFTable.sln

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.5.33502.453
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "RotatePDFTable", "RotatePDFTable\RotatePDFTable.csproj", "{52170CC2-49DA-46BB-9C3C-C8542C133CE5}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{52170CC2-49DA-46BB-9C3C-C8542C133CE5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{52170CC2-49DA-46BB-9C3C-C8542C133CE5}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{52170CC2-49DA-46BB-9C3C-C8542C133CE5}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{52170CC2-49DA-46BB-9C3C-C8542C133CE5}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
GlobalSection(ExtensibilityGlobals) = postSolution
23+
SolutionGuid = {8DA4A816-B8BF-493B-A013-C58EA0C37B86}
24+
EndGlobalSection
25+
EndGlobal

0 commit comments

Comments
 (0)