Articles in this section
Category / Section

How to draw rotated table in a PDF using C# and VB.NET

5 mins read

Syncfusion Essential® PDF is a .NET PDF library used to create, read, and edit PDF documents. Using this library, you can draw a rotated table in a PDF using C# and VB.NET.

Steps to draw a rotated table in a PDF programmatically:

  1. Create a new Windows Forms application project. Create a console application project
  2. Install the Syncfusion.Pdf.WinForms NuGet package as a reference to your .NET Framework application from NuGet.org. NuGet package reference screenshot
  3. Include the following namespaces in the Form1.Designer.cs file.

C#

using Syncfusion.Pdf;
using Syncfusion.Pdf.Graphics;
using Syncfusion.Pdf.Grid;
using System.Drawing;

 

VB.NET

Imports Syncfusion.Pdf
Imports Syncfusion.Pdf.Graphics
Imports Syncfusion.Pdf.Grid
Imports System.Drawing

 

  1. Use the following code snippet in the click event of the button to draw rotated table in the PDF document.

C#

// Create a new PDF document
PdfDocument document = new PdfDocument();
// Add a page
PdfPage page = document.Pages.Add();
string text = "Adventure Works Cycles, the fictitious company on which the AdventureWorks sample databases are based, is a large, multinational manufacturing company. The company manufactures and sells metal and composite bicycles to North American, European, and Asian commercial markets. While its base operation is located in Washington with 290 employees, several regional sales teams are located throughout their market base.";
// Create a text element with the text and font
PdfTextElement textElement = new PdfTextElement(text, new PdfStandardFont(PdfFontFamily.TimesRoman, 10));
// Draw the text
textElement.Draw(page, new RectangleF(0, 0, page.GetClientSize().Width, page.GetClientSize().Height));
// Create a PdfGrid
PdfGrid pdfGrid = new PdfGrid();
// Add handler to rotate the table
pdfGrid.BeginPageLayout += PdfGrid_BeginPageLayout;
pdfGrid.EndPageLayout += PdfGrid_EndPageLayout;
// 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;
pdfGrid.RepeatHeader = true;
// Draw grid to the page of PDF document
pdfGrid.Draw(page, new RectangleF(100, 20, 0, page.GetClientSize().Width));
// Save the document
document.Save("RotatedTable.pdf");
// Close the document
document.Close(true);
// This will open the PDF file so the result will be seen in the default PDF Viewer 
Process.Start("RotatedTable.pdf");

 

VB.NET

'Create a new PDF document
Dim document As PdfDocument = New PdfDocument()
'Add a page
Dim page As PdfPage = document.Pages.Add()
Dim text As String = "Adventure Works Cycles, the fictitious company on which the AdventureWorks sample databases are based, is a large, multinational manufacturing company. The company manufactures and sells metal and composite bicycles to North American, European, and Asian commercial markets. While its base operation is located in Washington with 290 employees, several regional sales teams are located throughout their market base."
'Create a text element with the text and font
Dim textElement As PdfTextElement = New PdfTextElement(text, New PdfStandardFont(PdfFontFamily.TimesRoman, 10))
'Draw the text
textElement.Draw(page, New RectangleF(0, 0, page.GetClientSize().Width, page.GetClientSize().Height))
'Create a PdfGrid
Dim pdfGrid As PdfGrid = New PdfGrid()
'Add handler to rotate the table
AddHandler pdfGrid.BeginPageLayout, AddressOf PdfGrid_BeginPageLayout
AddHandler pdfGrid.EndPageLayout, AddressOf PdfGrid_EndPageLayout
'Create a DataTable
Dim dataTable As DataTable = New DataTable()
'Add columns to the DataTable
dataTable.Columns.Add("ID", GetType(Integer))
dataTable.Columns.Add("Name", GetType(String))
dataTable.Columns.Add("Type", GetType(String))
dataTable.Columns.Add("Date", GetType(DateTime))
'Add rows to the DataTable
For i As Integer = 0 To 10 - 1
    dataTable.Rows.Add(57, "AAA", "ABC", DateTime.Now)
    dataTable.Rows.Add(130, "BBB", "BCD", DateTime.Now)
    dataTable.Rows.Add(92, "CCC", "CDE", DateTime.Now)
Next
'Assign data source
pdfGrid.DataSource = dataTable
pdfGrid.RepeatHeader = True
'Draw grid to the page of PDF document
pdfGrid.Draw(page, New RectangleF(100, 20, 0, page.GetClientSize().Width))
'Save the document
document.Save("RotatedTable.pdf")
'Close the document
document.Close(True)
'This will open the PDF file so the result will be seen in the default PDF Viewer 
Process.Start("RotatedTable.pdf")
  1. Use the following code in the event handler.

C#

private void PdfGrid_EndPageLayout(object sender, Syncfusion.Pdf.Graphics.EndPageLayoutEventArgs e)
{
    if (state != null && graphics != null)
    {
        // Restore the graphics state
        graphics.Restore(state);
    }
}
 
private void PdfGrid_BeginPageLayout(object sender, Syncfusion.Pdf.Graphics.BeginPageLayoutEventArgs e)
{
 
    PdfPage page = e.Page;
    PdfGraphics graphics = e.Page.Graphics;
    // Save the current graphics state
    state = graphics.Save();
    // Translate the coordinate system to the required position 
    graphics.TranslateTransform(page.GetClientSize().Width, 0);
    // Rotate the coordinate system
    graphics.RotateTransform(90);
}

 

VB.NET

Private Sub PdfGrid_EndPageLayout(ByVal sender As Object, ByVal e As Syncfusion.Pdf.Graphics.EndPageLayoutEventArgs)
     If state IsNot Nothing AndAlso Graphics IsNot Nothing Then
         Graphics.Restore(state)
     End If
 End Sub
 
 Private Sub PdfGrid_BeginPageLayout(ByVal sender As Object, ByVal e As Syncfusion.Pdf.Graphics.BeginPageLayoutEventArgs)
     Dim page As PdfPage = e.Page
     Dim graphics As PdfGraphics = e.Page.Graphics
     state = graphics.Save()
     graphics.TranslateTransform(page.GetClientSize().Width, 0)
     graphics.RotateTransform(90)
 End Sub

 

A complete working sample can be downloaded from TableRotationSample.zip.

By executing the program, you will get the PDF document as follows.

Output document screenshot

Take a moment to peruse the documentation, where you can find options like creating a table using PdfLightTable and PdfGrid, cell and row customization in PdfLightTable and PdfGrid, built-in table styles for PdfGrid, pagination in PdfGrid and PdfLightTable with code examples.

Refer here to learn more about PDF Tables in Essential® PDF.

Note:

Starting with v16.2.0.x, if you reference Syncfusion® assemblies from the trial setup or from the NuGet feed, include a license key in your projects. Refer to the link to learn about generating and registering the Syncfusion® license key in your application to use the components without a trial message.

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied