We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. (Last updated on: November 16, 2018).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

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

Platform: WinForms |
Control: PDF |
Published Date: January 28, 2016 |
Last Revised Date: May 6, 2019
Tags: pdf, pdfgrid, table

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

Steps to draw rotated header in PDF grid programmatically:

  1. Create a new C# Windows Forms application project. Create new windows forms application
  2. Install the Syncfusion.Pdf.WinForms NuGet package as a reference to your .NET Framework application from the NuGet.org. Install nuget packages
  3. Include the following namespaces in the Form1.cs file.

C#

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

 

VB.NET

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

 

  1. Use the following code snippet to draw rotated header in PDF grid.

C#

string[] headerValues = { "OrderID", "CustomerID", "ShipName", "ShipAddress", "ShipCity", "ShipPostalCode" };
bool isNewPage;
private void button1_Click(object sender, EventArgs e)
{
    PdfDocument doc = new PdfDocument();
    PdfPage page = doc.Pages.Add();
    PdfGrid grid = new PdfGrid();
    grid.Columns.Add(6);
    grid.Headers.Add(1);
    //Get the length of largest string 
    var length = 0;
    var longestString = string.Empty;
    for (var i = 0; i < headerValues.Length; i++)
    {
        if (headerValues[i].Length > length)
        {
            length = headerValues[i].Length;
            longestString = headerValues[i];
        }
    }
    //Create a DataTable
    DataTable dataTable1 = new DataTable();
    //Add columns to the DataTable
    for (int i = 0; i < 6; i++)
        dataTable1.Columns.Add();
    //Add rows to the DataTable
    dataTable1.Rows.Add(new string[] { "10248", "VINET", "Vins et alcools Chevalier", "59 rue de l'Abbaye", "Reims", "51100" });
    dataTable1.Rows.Add(new string[] { "10249", "TOMSP", "Toms Spezialitäten", "Luisenstr. 48", "Münster", "44087" });
    dataTable1.Rows.Add(new string[] { "10250", "HANAR", "Hanari Carnes", "Rua do Paço, 67", "Rio de Janeiro", "05454-876" });
    dataTable1.Rows.Add(new string[] { "10251", "VICTE", "Victuailles en stock", "2, rue du Commerce", "Lyon", "69004" });
    dataTable1.Rows.Add(new string[] { "10252", "SUPRD", "Suprêmes délices", "Boulevard Tirou, 255", "Charleroi", "B-6000" });
    dataTable1.Rows.Add(new string[] { "10253", "HANAR", "Hanari Carnes", "Rua do Paço, 67", "Rio de Janeiro", "05454-876" });
    //Assign data source
    grid.DataSource = dataTable1;
    //Measure the font size with longest string
    PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 6);
    SizeF size = font.MeasureString(longestString);
    grid.Headers[0].Height = size.Width * 2;
    //Repeat the header 
    grid.RepeatHeader = true;
    grid.BeginPageLayout += new BeginPageLayoutEventHandler(grid_BeginPageLayout);
    grid.BeginCellLayout += new PdfGridBeginCellLayoutEventHandler(BeginCellEvent);
    grid.Draw(page, new PointF(0, 0));
    doc.Save("DrawRotatedHeader.pdf");
    doc.Close(true);
    System.Diagnostics.Process.Start("DrawRotatedHeader.pdf");
}
 
//Event triggers for new beginning of the page
private void grid_BeginPageLayout(object sender, BeginPageLayoutEventArgs e)
{
    isNewPage = true;
}
 
//Event trigger for beginning of the cell
private void BeginCellEvent(object sender, PdfGridBeginCellLayoutEventArgs args)
{
    PdfGrid grid = (PdfGrid)sender;
    if (isNewPage)
    {
        grid.Headers[0].Cells[args.CellIndex].Value = string.Empty;
        args.Graphics.Save();
        args.Graphics.TranslateTransform(args.Bounds.X, args.Bounds.Height);
        args.Graphics.RotateTransform(-90);
        //Draw the text at particular bounds
        args.Graphics.DrawString(headerValues[args.CellIndex], new PdfStandardFont(PdfFontFamily.Helvetica, 10, PdfFontStyle.Bold), PdfBrushes.Red, new PointF(0, 0));
        args.Graphics.Restore();
        //If all columns drawn
        if (args.CellIndex == grid.Columns.Count - 1)
            isNewPage = false;
    }
}

 

VB.NET

Dim isNewPage As Boolean
Dim headerValues As String() = New String() {"OrderID", "CustomerID", "ShipName", "ShipAddress", "ShipCity", "ShipPostalCode"}
Sub Main()
    Dim doc = New PdfDocument()
    Dim page = doc.Pages.Add()
    Dim grid = New PdfGrid()
    'Create a DataTable
    Dim dataTable1 As DataTable = New DataTable()
    'Add columns to the DataTable
    Dim i = 0
    Do While (i < 6)
        dataTable1.Columns.Add()
        i = i + 1
    Loop
    'Add rows to the DataTable
    dataTable1.Rows.Add(New Object() {"10248", "VINET", "Vins et alcools Chevalier", "59 rue de l'Abbaye", "Reims", "51100"})
    dataTable1.Rows.Add(New Object() {"10249", "TOMSP", "Toms Spezialitäten", "Luisenstr. 48", "Münster", "44087"})
    dataTable1.Rows.Add(New Object() {"10250", "HANAR", "Hanari Carnes", "Rua do Paço, 67", "Rio de Janeiro"})
    dataTable1.Rows.Add(New Object() {"10251", "VICTE", "Victuailles en stock", "2, rue du Commerce", "Lyon"})
    dataTable1.Rows.Add(New Object() {"10252", "SUPRD", "Suprêmes délices", "Boulevard Tirou, 255", "Charleroi"})
    dataTable1.Rows.Add(New Object() {"10253", "HANAR", "Hanari Carnes", "Rua do Paço, 67", "Rio de Janeiro", "05454-876"})
    'Assign data source
    grid.DataSource = dataTable1
    'Get the length of largest string
    Dim longestString = String.Empty
    i = 0
    Dim length = 0
    Do While (i < headerValues.Length)
        If (headerValues.GetValue(i).Length > length) Then
            length = headerValues.GetValue(i).Length
            longestString = headerValues.GetValue(i)
        End If
        i = i + 1
    Loop
    'Measure the font size with longest string
    Dim font = New PdfStandardFont(PdfFontFamily.Helvetica, 6)
    Dim size = font.MeasureString(longestString)
    grid.Headers.Item(0).Height = size.Width * 2
    'Repeat the header 
    grid.RepeatHeader = True
    AddHandler grid.BeginPageLayout, New BeginPageLayoutEventHandler(AddressOf BeginPageLayout)
    AddHandler grid.BeginCellLayout, New PdfGridBeginCellLayoutEventHandler(AddressOf BeginCellEvent)
    grid.Draw(page, New PointF(0, 0))
    doc.Save("DrawRotatedHeader.pdf")
    doc.Close(True)
    System.Diagnostics.Process.Start("DrawRotatedHeader.pdf")
End Sub
 
'Event triggers for New beginning of the page
Sub BeginPageLayout(ByVal sender As Object, ByVal e As BeginPageLayoutEventArgs)
    isNewPage = True
End Sub
 
'Event trigger for beginning of the cell
Sub BeginCellEvent(ByVal sender As PdfGrid, ByVal args As PdfGridBeginCellLayoutEventArgs)
    Dim grid = sender
    If isNewPage Then
        grid.Headers.Item(0).Cells.Item(args.CellIndex).Value = String.Empty
        args.Graphics.Save()
        args.Graphics.TranslateTransform(args.Bounds.X, args.Bounds.Height)
        'Rotates the header
        args.Graphics.RotateTransform(-90)
        'Draw the text at particular bounds
        args.Graphics.DrawString(headerValues.GetValue(args.CellIndex), New PdfStandardFont(PdfFontFamily.Helvetica, 10, PdfFontStyle.Bold), PdfBrushes.Red, New PointF(0, 0))
        args.Graphics.Restore()
        'If all Then columns drawn
        If (args.CellIndex = grid.Columns.Count - 1) Then
            isNewPage = False
        End If
    End If
End Sub

 

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

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

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.

Click here to explore the rich set of Syncfusion Essential PDF features.

An online sample link to create a richly formatted table.

Note:

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.

 

2X faster development

The ultimate WinForms UI toolkit to boost your development speed.
ADD COMMENT
You must log in to leave a comment

Please sign in to access our KB

This page will automatically be redirected to the sign-in page in 10 seconds.

Up arrow icon

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon