How to add the header in PdfLightTable
The Syncfusion Essential PDF is a feature-rich and high-performance .NET PDF library used to create, read, and edit PDF documents programmatically without Adobe dependencies. This library also offers functionality to merge, split, stamp, forms, compress, and secure PDF files.
This sample explains how to add header in PdfLightTable.
Steps to add header in PdfLightTable programmatically in C#:
- Create a new C# console 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 Program.cs file
C#
using Syncfusion.Pdf; using Syncfusion.Pdf.Graphics; using Syncfusion.Pdf.Tables; using System.Drawing;
VB.NET
Imports Syncfusion.Pdf Imports Syncfusion.Pdf.Graphics Imports Syncfusion.Pdf.Tables Imports System.Drawing
- You can add header in a PdfLightTable by enabling the ShowHeader property of PdfLightTableStyle class.
C#
//Add header to the table lightTable.Style.ShowHeader = true;
VB.NET
'Add header to the table lightTable.Style.ShowHeader = True
- Include the following code snippet in main method of Program.cs file to add header in PdfLightTable and apply the HeaderStyle.
C#
//Create a new PDF document PdfDocument document = new PdfDocument(); //Add a page to the PDF document PdfPage page = document.Pages.Add(); //Create a PdfLightTable and set the DatSourceType PdfLightTable lightTable = new PdfLightTable(); lightTable.DataSourceType = PdfLightTableDataSourceType.TableDirect; //Add columns to the table lightTable.Columns.Add(new PdfColumn("Employee ID")); lightTable.Columns.Add(new PdfColumn("Employee Name")); lightTable.Columns.Add(new PdfColumn("Title")); lightTable.Columns.Add(new PdfColumn("Birth Date")); lightTable.Columns.Add(new PdfColumn("Hire Date")); //Add rows to the table lightTable.Rows.Add(new object[] { "E01", "Nancy Davolio", "Sales Representative", "12/8/1948", "5/1/1992" }); lightTable.Rows.Add(new object[] { "E02", "Andrew Fuller", "Vice President, Sales", "2/19/1952", "8/14/1992" }); lightTable.Rows.Add(new object[] { "E03", "Janet Leverling", "Sales Representative", "8/30/1963", "4/1/1992" }); lightTable.Rows.Add(new object[] { "E04", "Margeret Peacock", "Sales Representative", "9/19/1937", "5/3/1993" }); lightTable.Rows.Add(new object[] { "E05", "Steven Buchnam", "Sales Manager", "3/4/1955", "10/17/1993" }); lightTable.Rows.Add(new object[] { "E06", "Michael Suyama", "Sales Representative", "7/2/1963", "10/17/1993" }); lightTable.Rows.Add(new object[] { "E07", "Robert King", "Sales Representative", "5/29/1960", "1/2/1994" }); //Add header to the table lightTable.Style.ShowHeader = true; //Create a style for header using PdfCellStyle PdfCellStyle headerStyle = new PdfCellStyle(new PdfStandardFont(PdfFontFamily.Helvetica, 10), PdfBrushes.OrangeRed, PdfPens.Black); lightTable.Style.HeaderStyle = headerStyle; //Draw the table on PDF page lightTable.Draw(page, PointF.Empty); //Save the PDF document document.Save("Output.pdf"); //Close the instance of PdfDocument document.Close(true);
VB.NET
'Create a new PDF document Dim document As PdfDocument = New PdfDocument 'Add a page to the PDF document Dim page As PdfPage = document.Pages.Add 'Create a PdfLightTable and set the DatSourceType Dim lightTable As PdfLightTable = New PdfLightTable lightTable.DataSourceType = PdfLightTableDataSourceType.TableDirect 'Add columns to the table lightTable.Columns.Add(New PdfColumn("Employee ID")) lightTable.Columns.Add(New PdfColumn("Employee Name")) lightTable.Columns.Add(New PdfColumn("Title")) lightTable.Columns.Add(New PdfColumn("Birth Date")) lightTable.Columns.Add(New PdfColumn("Hire Date")) 'Add rows to the table lightTable.Rows.Add(New Object() {"E01", "Nancy Davolio", "Sales Representative", "12/8/1948", "5/1/1992"}) lightTable.Rows.Add(New Object() {"E02", "Andrew Fuller", "Vice President, Sales", "2/19/1952", "8/14/1992"}) lightTable.Rows.Add(New Object() {"E03", "Janet Leverling", "Sales Representative", "8/30/1963", "4/1/1992"}) lightTable.Rows.Add(New Object() {"E04", "Margeret Peacock", "Sales Representative", "9/19/1937", "5/3/1993"}) lightTable.Rows.Add(New Object() {"E05", "Steven Buchnam", "Sales Manager", "3/4/1955", "10/17/1993"}) lightTable.Rows.Add(New Object() {"E06", "Michael Suyama", "Sales Representative", "7/2/1963", "10/17/1993"}) lightTable.Rows.Add(New Object() {"E07", "Robert King", "Sales Representative", "5/29/1960", "1/2/1994"}) 'Add header to the table lightTable.Style.ShowHeader = True 'Create a style for header using PdfCellStyle Dim headerStyle As PdfCellStyle = New PdfCellStyle(New PdfStandardFont(PdfFontFamily.Helvetica, 10), PdfBrushes.OrangeRed, PdfPens.Black) lightTable.Style.HeaderStyle = headerStyle 'Draw the table on PDF page lightTable.Draw(page, PointF.Empty) 'Save the PDF document document.Save("Output.pdf") 'Close the instance of PdfDocument document.Close(True)
- You can repeat the header on each page in PdfLightTable by enabling the RepeatHeader property of PdfLightTableStyle class.
C#
//Repeat the header on each page for the table lightTable.Style.RepeatHeader = true;
VB.NET
'Repeat the header on each page for the table lightTable.Style.RepeatHeader = True
A complete work sample to add header and apply style to it in PdfLightTable can be downloaded from TableHeader.zip.
By executing the program, you will get the PDF document as follows.
Take a moment to peruse the documentation, where you will find other options like simple table creation, table customization, adding multiple tables, row and column spanning, row styles, cell styles, string formatting and more both in PdfLightTable and PdfGrid.
Click here to know the differences between PdfLightTable and PdfGrid.
Click here to explore the rich set of Syncfusion Essential PDF features.
An online sample link for different table features.
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.