How to Draw a Table in WinForms PDF Using C# and VB.NET?
Syncfusion Essential PDF is a .NET PDF library used to create, read, and edit PDF documents. Using this library, you can create PDFs and draw tables with data sources from datasets, data tables, arrays, and IEnumerable objects using the PdfLightTable class in C# and VB.NET.
Steps to draw a table on a PDF programmatically:
- Create a new C# console application project.
- Install the Syncfusion.Pdf.WinForms NuGet packages as a reference to your .NET Framework application from NuGet.org.
- 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
- Use the following code snippet to create the PDF and draw a 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 DataSource 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 the 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 the 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 the 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 DataSource 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 the 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 the 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 the 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.
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.
Starting with v16.2.0.x, if you reference Syncfusion® assemblies from the trial setup or 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.
I hope you enjoyed learning about how to draw a table in WinForms PDF using C# and VB.NET.
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 or feedback portal. We are always happy to assist you!