Articles in this section
Category / Section

How to create table in a PDF file in WinForms PDF?

2 mins read

 Syncfusion Essential PDF is a .NET PDF library used to create, read, and edit PDF documents. Using this library, you can create a table in PDF documents in C# and VB.NET.

Essential PDF allows you to create the table with data sources from DataSet, data table, arrays and IEnumerable objects using PdfLightTable class along with simple formatting. You can directly add rows and columns, instead of a data source by setting the DataSourceType property to TableDirect.

Steps to create table in a PDF file programmatically:

  1. Create a new console C# application project. Create Console application in Visual Studio
  2. Install the Syncfusion.Pdf.WinForms NuGet package as reference to your .NET Framework applications from NuGet.org. Install nuget packages
  3. 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

 

  1. Use the following code snippet to create table in a PDF file.

C#

//Create a new PDF document
using (PdfDocument document = new PdfDocument())
{
  //Add a page
  PdfPage page = document.Pages.Add();
 
  //Acquire pages graphics
  PdfGraphics graphics = page.Graphics;
 
  //Declare a PdfLightTable
  PdfLightTable pdfLightTable = new PdfLightTable();
 
  //Set the Data source as direct
  pdfLightTable.DataSourceType = PdfLightTableDataSourceType.TableDirect;
 
  //Create columns
  pdfLightTable.Columns.Add(new PdfColumn("Roll Number"));
  pdfLightTable.Columns.Add(new PdfColumn("Name"));
  pdfLightTable.Columns.Add(new PdfColumn("Class"));
 
  //Add rows
  pdfLightTable.Rows.Add(new object[] { "111", "Maxim", "III" });
 
  //Draw the PdfLightTable
  pdfLightTable.Draw(page, PointF.Empty);
 
  //Save the document
  document.Save("Output.pdf");
}

 

VB.NET

'Create a new PDF document
Using document As PdfDocument = New PdfDocument()
 
 'Add a page
  Dim page As PdfPage = document.Pages.Add()
 
  'Acquire pages graphics
  Dim graphics As PdfGraphics = page.Graphics
 
  'Declare a PdfLightTable
  Dim pdfLightTable As PdfLightTable = New PdfLightTable
 
  'Set the Data source as direct
  pdfLightTable.DataSourceType = PdfLightTableDataSourceType.TableDirect
 
  'Create columns
  pdfLightTable.Columns.Add(New PdfColumn("Roll Number"))
  pdfLightTable.Columns.Add(New PdfColumn("Name"))
  pdfLightTable.Columns.Add(New PdfColumn("Class"))
 
  'Add rows
  pdfLightTable.Rows.Add(New Object() {"111", "Maxim", "III"})
 
  'Draw the PdfLightTable
  pdfLightTable.Draw(page, PointF.Empty)
 
  'Save the document
  document.Save("Output.pdf")
 
End Using

 

A complete working sample can be downloaded from Create-PDF-Table.zip

By executing the program, you will get the PDF document as follows. Create Table in PDF File

Refer here to create a table in PDF using PdfGrid and know the  differences between PdfLightTable and PdfGrid.

Take a moment to peruse the documentation, where you can find other options like drawing right-to-left text and multi-column text, consuming TrueType fonts, Standard fonts, and CJK fonts. Also, the features like PDF form filling, extract text or images from PDF, and protect PDF documents with code examples.

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

An online sample link to create table in a PDF file.

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.

 

Conclusion

I hope you enjoyed learning about how to create table in a PDF file

You can refer to our WinForms PDF feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. You can also explore our WinForms PDF example 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 forumsDirect-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
Please sign in to leave a comment
Access denied
Access denied