|
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 | + |
0 commit comments