How to draw rotated headers in a PDF grid using C# and VB.NET?
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:
- Create a new C# Windows Forms application project.
- Install the Syncfusion.Pdf.WinForms NuGet package as a reference to your .NET Framework application from the NuGet.org.
- 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
- 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.
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.
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 draw rotated headers in a PDF grid using C# and VB.NET.
You can refer to our PDF feature tour page to know about its other groundbreaking feature representations. You can also explore our documentation 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 the comments section below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!