Articles in this section
Category / Section

How to add the header in PdfLightTable

8 mins read

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 a header in PdfLightTable.

Steps to add a header in PdfLightTable programmatically in C#:

  1. Create a new C# console application project. Create console application in visual studioCreate console application in visual studio
  2. Install the Syncfusion.Pdf.WinForms NuGet package as a reference to your .NET Framework application from the NuGet.org. Refer NuGet package to the project
  3. Include the following namespaces in the 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

 

  1. 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

 

  1. 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 DataSourceType
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 a header to the table
lightTable.Style.ShowHeader = true;

// Create a style for the header using PdfCellStyle
PdfCellStyle headerStyle = new PdfCellStyle(new PdfStandardFont(PdfFontFamily.Helvetica, 10), PdfBrushes.OrangeRed, PdfPens.Black);
lightTable.Style.HeaderStyle = headerStyle;

// Draw the table on the 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)

 

  1. 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.

Table with header

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.

Note:

Starting with v16.2.0.x, if you reference Syncfusion® assemblies from a 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.

 

Conclusion

I hope you enjoyed learning about how to add the header in PdfLightTable.

You can refer to our WinForms PDF’s feature tour page to know about its other groundbreaking feature representations. You can also explore our WinForms PDF documentation to understand how to present and manipulate data.

For current customers, you can check out our WinForms 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 WinForms PDF and other WinForms components.

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!

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