How to rotate a table from an existing PDF in ASP.NET MVC?
Syncfusion Essential PDF is a .NET PDF library used to create, read, and edit PDF documents. Using this library, you can rotate tables from an existing PDF document in ASP.NET MVC.
Steps to rotate tables from an existing PDF programmatically:
- Create a new ASP.NET MVC application project.
- Install the Syncfusion.Pdf.AspNet.Mvc NuGet package as a reference to your .NET Framework applications from NuGet.org.
- A default controller with name HomeController.cs gets added on creation of ASP.NET MVC project. Include the following namespaces in that HomeController.cs file.
C#
using Syncfusion.Pdf; using Syncfusion.Pdf.Graphics; using Syncfusion.Pdf.Tables; using Syncfusion.Pdf.Parsing; using System.Data; using System.Drawing; using System.IO;
VB.NET
Imports Syncfusion.Pdf Imports Syncfusion.Pdf.Graphics Imports Syncfusion.Pdf.Tables Imports Syncfusion.Pdf.Parsing Imports System.Data Imports System.Drawing Imports System.IO
- A default action method named Index will be present in HomeController.cs. Right-click this action method and select Go To View, where you will be directed to its associated view page Index.cshtml.
- Add a new button in the Index.cshtml as follows.
@using (Html.BeginForm("RotatePDF", "Home", FormMethod.Post)) { <input type="submit" value="RotateTables" /> }
- Add a new action method named RotatePDF in HomeController.cs and include the following code snippet to rotate tables from an existing PDF file and download it.
C#
public ActionResult RotatePDF() { //Create an instance of PdfDocument PdfDocument document = new PdfDocument(); //Add a section PdfPage page = document.Pages.Add(); //Create PDF graphics for the page PdfGraphics graphics = page.Graphics; //Create a PdfLightTable PdfLightTable pdfTable = new PdfLightTable(); //Subscribing to events pdfTable.BeginPageLayout += PdfTable_BeginPageLayout; pdfTable.EndPageLayout += PdfTable_EndPageLayout; //Create a DataTable DataTable dataTable = new DataTable(); //Add columns to the DataTable dataTable.Columns.Add("OrderID"); dataTable.Columns.Add("CustomerID"); dataTable.Columns.Add("ShipName"); dataTable.Columns.Add("ShipAddress"); dataTable.Columns.Add("ShipCity"); dataTable.Columns.Add("ShipPostalCode"); dataTable.Columns.Add("ShipCountry"); //Add rows to the DataTable dataTable.Rows.Add(new object[] { "10248", "VINET", "Vins et alcools Chevalier", "59 rue de l'Abbaye", "Reims", "51100", "France" }); dataTable.Rows.Add(new object[] { "10249", "TOMSP", "Toms Spezialitäten", "Luisenstr. 48", "Münster", "44087", "Germany" }); dataTable.Rows.Add(new object[] { "10250", "HANAR", "Hanari Carnes", "Rua do Paço, 67", "Rio de Janeiro", "05454-876", "Brazil" }); dataTable.Rows.Add(new object[] { "10251", "VICTE", "Victuailles en stock", "2, rue du Commerce", "Lyon", "69004", "France" }); dataTable.Rows.Add(new object[] { "10252", "SUPRD", "Suprêmes délices", "Boulevard Tirou, 255", "Charleroi", "B-6000", "Belgium" }); dataTable.Rows.Add(new object[] { "10253", "HANAR", "Hanari Carnes", "Rua do Paço, 67", "Rio de Janeiro", "05454-876", "Brazil" }); pdfTable.Style.ShowHeader = true; //Assign data source pdfTable.DataSource = dataTable; //Draw the PdfLightTable pdfTable.Draw(page, new RectangleF(50, 0, 0, page.GetClientSize().Width)); MemoryStream stream = new MemoryStream(); //Save the document document.Save(stream); document.Close(true); //Load a PDF document PdfLoadedDocument loadedDocument = new PdfLoadedDocument(stream); //Set the standard font PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 15); //Set the format for string PdfStringFormat stringFormat = new PdfStringFormat(); stringFormat.WordWrap = PdfWordWrapType.Word; string text = "We can rotate tables from an existing PDF"; for (int i = 0; i < loadedDocument.Pages.Count; i++) { PdfLoadedPage lPage = loadedDocument.Pages[i] as PdfLoadedPage; PdfGraphics graphics1 = lPage.Graphics; graphics1.DrawString(text, font, PdfBrushes.Black, new RectangleF(10, 50, lPage.Size.Width - 10, 100), stringFormat); } //Save the document loadedDocument.Save("RotateTable.pdf", HttpContext.ApplicationInstance.Response, HttpReadType.Save); retrun null; } static PdfGraphicsState state = null; static PdfGraphics graphics = null; private static void PdfTable_EndPageLayout(object sender, EndPageLayoutEventArgs e) { if (state != null && graphics != null) { graphics.Restore(state); } } private static void PdfTable_BeginPageLayout(object sender, BeginPageLayoutEventArgs e) { PdfPage page = e.Page; PdfGraphics graphics = e.Page.Graphics; state = graphics.Save(); graphics.TranslateTransform(page.GetClientSize().Width, 0); graphics.RotateTransform(90); }
VB.NET
Sub Main() 'Create an instance of PdfDocument Dim document As PdfDocument = New PdfDocument() 'Add a section Dim page As PdfPage = document.Pages.Add() 'Create PDF graphics for the page Dim graphics As PdfGraphics = page.Graphics() 'Create a PdfLightTable Dim pdfTable As PdfLightTable = New PdfLightTable() 'Subscribing to events AddHandler pdfTable.BeginPageLayout, New BeginPageLayoutEventHandler(AddressOf table_BeginPageLayout) AddHandler pdfTable.EndPageLayout, New EndPageLayoutEventHandler(AddressOf table_EndPageLayout) Dim dataTable As DataTable = New DataTable() 'Add columns to the DataTable dataTable.Columns.Add("OrderID") dataTable.Columns.Add("CustomerID") dataTable.Columns.Add("ShipName") dataTable.Columns.Add("ShipAddress") dataTable.Columns.Add("ShipCity") dataTable.Columns.Add("ShipPostalCode") dataTable.Columns.Add("ShipCountry") 'Add rows to the DataTable dataTable.Rows.Add(New Object() {"10248", "VINET", "Vins et alcools Chevalier", "59 rue de l'Abbaye", "Reims", "51100", "France"}) dataTable.Rows.Add(New Object() {"10249", "TOMSP", "Toms Spezialitäten", "Luisenstr. 48", "Münster", "44087", "Germany"}) dataTable.Rows.Add(New Object() {"10250", "HANAR", "Hanari Carnes", "Rua do Paço, 67", "Rio de Janeiro", "05454-876", "Brazil"}) dataTable.Rows.Add(New Object() {"10251", "VICTE", "Victuailles en stock", "2, rue du Commerce", "Lyon", "69004", "France"}) dataTable.Rows.Add(New Object() {"10252", "SUPRD", "Suprêmes délices", "Boulevard Tirou, 255", "Charleroi", "B-6000", "Belgium"}) dataTable.Rows.Add(New Object() {"10253", "HANAR", "Hanari Carnes", "Rua do Paço, 67", "Rio de Janeiro", "05454-876", "Brazil"}) pdfTable.Style.ShowHeader = True 'Assign data source pdfTable.DataSource = dataTable 'Draw the PdfLightTable pdfTable.Draw(page, New RectangleF(50, 0, 0, page.GetClientSize.Width)) Dim stream As MemoryStream = New MemoryStream() 'Save the document document.Save(stream) document.Close(True) Dim loadedDocument As PdfLoadedDocument = New PdfLoadedDocument(stream) Dim font As PdfFont = New PdfStandardFont(PdfFontFamily.Helvetica, 15) Dim stringFormat As PdfStringFormat = New PdfStringFormat() stringFormat.WordWrap = PdfWordWrapType.Word Dim text As String = "We can rotate tables from an existing PDF" Dim i As Integer = 0 Do While (i < loadedDocument.Pages.Count) Dim lPage As PdfLoadedPage = CType(loadedDocument.Pages(i), PdfLoadedPage) Dim graphics1 As PdfGraphics = lPage.Graphics graphics1.DrawString(text, font, PdfBrushes.Black, New RectangleF(10, 50, (lPage.Size.Width - 10), 100), stringFormat) i = (i + 1) Loop 'Save the document loadedDocument.Save("RotateTable.pdf") loadedDocument.Close(True) End Sub Dim state As PdfGraphicsState = Nothing Dim graphics As PdfGraphics = Nothing Public Sub table_BeginPageLayout(ByVal sender As Object, ByVal args As BeginPageLayoutEventArgs) Dim page As PdfPage = args.Page Dim graphics As PdfGraphics = args.Page.Graphics state = graphics.Save graphics.TranslateTransform(page.GetClientSize.Width, 0) graphics.RotateTransform(90) End Sub Public Sub table_EndPageLayout(ByVal sender As Object, ByVal e As EndPageLayoutEventArgs) If ((Not (state) Is Nothing) _ AndAlso (Not (graphics) Is Nothing)) Then graphics.Restore(state) End If End Sub
A complete working sample can be downloaded from RotateTableInPDF.zip.
By executing the program, you will get the PDF file as follows.
Take a moment to peruse the documentation for working with tables, where you will find other options like grid pagination and different levels of grid customization.
Refer here to explore the rich set of Syncfusion Essential PDF features.
An online sample link to create a richly formatted table.
Starting with v16.2.0.x, if you reference Syncfusion assemblies from trial setup or from the NuGet feed, include a license key in your projects. Refer to link to learn about generating and registering Syncfusion license key in your application to use the components without trail message.
Conclusion
I hope you enjoyed learning about how to rotate a table from an existing PDF document using C# and VB.NET.
You can refer to our ASP.NET MVC PDF feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. You can also explore our ASP.NET MVC PDF example example to understand how to create and manipulate data.
For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.
If you have any queries or require clarifications, please let us know in comments below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!