Articles in this section
Category / Section

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

7 mins read

Syncfusion Essential PDF is a .NET PDF library used to create, read, and edit the PDF documents. Using this library, you can create PDF and draw table with data sources from data set, data table, arrays, and IEnumerable objects using PdfLightTable class in C# and VB.NET.

Steps to draw table on PDF programmatically:

  1. Create a new C# console application project. Creation of console application project
  2. Install the Syncfusion.Pdf.WinForms NuGet packages as reference to your .NET Framework application from NuGet.org. NuGet package reference
  3. Include the following namespaces in the Program.cs file.

C#

using Syncfusion.Pdf;
using Syncfusion.Pdf.Graphics;
using Syncfusion.Pdf.Tables;

 

VB.NET

Imports Syncfusion.Pdf
Imports Syncfusion.Pdf.Graphics
Imports Syncfusion.Pdf.Tables

 

  1. Use the following code snippet to create the PDF and draw table.

C#

//Create a new PDF document
PdfDocument doc = new PdfDocument();
//Add a page
PdfPage page = doc.Pages.Add();
// Create a PdfLightTable
PdfLightTable pdfLightTable = new PdfLightTable();
// Initialize DataTable to assign as DateSource to the light table
DataTable table = new DataTable();
//Include columns to the DataTable
table.Columns.Add("Name");
table.Columns.Add("Age");
table.Columns.Add("Sex");
table.Columns.Add("Job");
//Include rows to the DataTable
for (int i = 0; i < 14; i++)
{
    table.Rows.Add(new string[] { "John", "21", "Male", "Carpenter" });
    table.Rows.Add(new string[] { "Caty", "24", "Female", "Developer" });
    table.Rows.Add(new string[] { "Eddie", "32", "Male", "Reporter" });
    table.Rows.Add(new string[] { "Tony", "35", "Male", "Mechanic" });
    table.Rows.Add(new string[] { "Monica", "25", "Female", "Cook" });
}
//Applying cell padding to table
pdfLightTable.Style.CellPadding = 3;
pdfLightTable.ApplyBuiltinStyle(PdfLightTableBuiltinStyle.GridTable3Accent3);
//Assign data source
pdfLightTable.DataSource = table;
//Setting this property to true to show the header of table
pdfLightTable.Style.ShowHeader = true;
//Draw PdfLightTable
pdfLightTable.Draw(page, new PointF(0, 0));
//Save the document
doc.Save("PdfTable.pdf");
//Close the document
doc.Close(true);
//This will open the PDF file so, the result will be seen in default PDF viewer
Process.Start("PdfTable.pdf");

 

VB.NET

'Create a new PDF document
Dim doc As PdfDocument = New PdfDocument
'Add a page
Dim page As PdfPage = doc.Pages.Add
'Create a PdfLightTable
Dim pdfLightTable As PdfLightTable = New PdfLightTable
'Initialize DataTable to assign as DateSource to the light table
Dim table As DataTable = New DataTable
'Include columns to the DataTable
table.Columns.Add("Name")
table.Columns.Add("Age")
table.Columns.Add("Sex")
table.Columns.Add("Job")
'Include rows to the DataTable
Dim i As Integer = 0
Do While (i < 14)
    table.Rows.Add(New String() {"John", "21", "Male", "Carpenter"})
    table.Rows.Add(New String() {"Caty", "24", "Female", "Developer"})
    table.Rows.Add(New String() {"Eddie", "32", "Male", "Reporter"})
    table.Rows.Add(New String() {"Tony", "35", "Male", "Mechanic"})
    table.Rows.Add(New String() {"Monica", "25", "Female", "Cook"})
    i = (i + 1)
Loop
 
'Applying cell padding to table
pdfLightTable.Style.CellPadding = 3
pdfLightTable.ApplyBuiltinStyle(PdfLightTableBuiltinStyle.GridTable3Accent3)
'Assign data source
pdfLightTable.DataSource = table
'Setting this property to true to show the header of table
pdfLightTable.Style.ShowHeader = True
'Draw PdfLightTable
pdfLightTable.Draw(page, New PointF(0, 0))
'Save the document
doc.Save("PdfTable.pdf")
'Close the document
doc.Close(True)
'This will open the PDF file so, the result will be seen in default PDF viewer
Process.Start("PdfTable.pdf")

 

A complete working sample can be downloaded from PdfTableSample.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 support for cell customization, rows and columns customization, and built-in table styles, pagination.

An online sample link demonstrating the use of Table in PDF document .

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

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.

 

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