Articles in this section
Category / Section

How to draw more than one table sequentially in PDF using C#, VB.NET?

3 mins read

Syncfusion Essential PDF is a .NET PDF library used to create, read, and edit PDF documents. Using this library, you can draw more than one table sequentially in a PDF.

Steps to draw more than one table sequentially in the PDF programmatically:

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

C#

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

 

VB.NET

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

 

  1. Use the following code snippet to draw the table one after another in the PDF.

C#

//Create a new PDF document
PdfDocument document = new PdfDocument();
 
//Add a new PDF page
PdfPage page = document.Pages.Add();
 
//Create font with bold style
PdfFont boldFont = new PdfStandardFont(PdfFontFamily.Helvetica, 12f, PdfFontStyle.Bold);
 
//Create font with regular style
PdfFont regularFont = new PdfStandardFont(PdfFontFamily.Helvetica, 12f, PdfFontStyle.Regular);
 
//Draw the string with bold style font
page.Graphics.DrawString("PdfGrid", boldFont, PdfBrushes.Black, PointF.Empty);
 
//Create PdfGrid
PdfGrid pdfGrid = new PdfGrid();
 
//Create a DataTable
DataTable dataTable = new DataTable();
 
//Add columns to the DataTable
dataTable.Columns.Add("ID");
 
dataTable.Columns.Add("Name");
 
dataTable.Columns.Add("Age");
 
dataTable.Columns.Add("Salary");
 
//Add rows to the DataTable dataTable.Rows.Add(new object[] { "E01", "Clay","25","20000" });
 
dataTable.Rows.Add(new object[] { "E02", "Thomas","30","35000" });
 
//Assign data source
pdfGrid.DataSource = dataTable;
 
//Assign bold font to pdfGrid header
pdfGrid.Headers[0].Style.Font = boldFont;
 
//Assign font to PdfGrid
pdfGrid.Style.Font = regularFont;
 
//Draw grid to the PDF document page
PdfGridLayoutResult result = pdfGrid.Draw(page, new PointF(0, 20));
 
page.Graphics.DrawString("PdfLightTable", boldFont, PdfBrushes.Black, new PointF(0, result.Bounds.Bottom + 10));
 
//Declare a PdfLightTable
PdfLightTable pdfLightTable = new PdfLightTable();
 
//Set the data source as direct
pdfLightTable.DataSourceType = PdfLightTableDataSourceType.TableDirect;
 
//Create columns
pdfLightTable.Columns.Add(new PdfColumn("ID"));
 
pdfLightTable.Columns.Add(new PdfColumn("Name"));
 
pdfLightTable.Columns.Add(new PdfColumn("Age"));
 
pdfLightTable.Columns.Add(new PdfColumn("Salary"));
 
//Add rows
pdfLightTable.Rows.Add(new object[] { "#E01", "@Clay", "25", "20000" });
 
pdfLightTable.Rows.Add(new object[] { "#E02", "@Thomas","30", "35000" });
 
pdfLightTable.Style.ShowHeader = true;
 
pdfLightTable.Style.HeaderStyle = new PdfCellStyle();
pdfLightTable.Style.HeaderStyle.Font = boldFont;
 
pdfLightTable.Style.DefaultStyle = new PdfCellStyle();
pdfLightTable.Style.DefaultStyle.Font = regularFont;
 
//Draw the PdfLightTable
pdfLightTable.Draw(result.Page, new PointF(0, result.Bounds.Bottom + 30));
 
//Save the PDF document
document.Save("Tables.pdf");
 
//Close the PDF document
document.Close(true);
 
//This will open the PDF file so, the result will be seen in default PDF Viewer
 
System.Diagnostics.Process.Start("Tables.pdf");

 

VB.NET

'Create a new PDF document
Dim document As New PdfDocument()
 
'Add a new PDF page
Dim page As PdfPage = document.Pages.Add()
 
'Create font with bold style
Dim boldFont As PdfFont = New PdfStandardFont(PdfFontFamily.Helvetica, 12.0F, PdfFontStyle.Bold)
 
'Create font with regular style
Dim regularFont As PdfFont = New PdfStandardFont(PdfFontFamily.Helvetica, 12.0F, PdfFontStyle.Regular)
 
'Draw the string with bold style font
page.Graphics.DrawString("PdfGrid", boldFont, PdfBrushes.Black, PointF.Empty)
 
'Create PdfGrid
Dim pdfGrid As New PdfGrid()
 
'Create a DataTable
Dim dataTable As New DataTable()
 
'Add columns to the DataTable
dataTable.Columns.Add("ID")
 
dataTable.Columns.Add("Name")
 
dataTable.Columns.Add("Age")
 
dataTable.Columns.Add("Salary")
 
'Add rows to the DataTable
dataTable.Rows.Add(New Object() {"E01", "Clay", "25", "20000"})
 
dataTable.Rows.Add(New Object() {"E02", "Thomas", "30", "35000"})
 
'Assign data source
pdfGrid.DataSource = dataTable
 
'Assign bold font to pdfGrid header
pdfGrid.Headers(0).Style.Font = boldFont
 
'Assign font to PdfGrid
pdfGrid.Style.Font = regularFont
 
'Draw grid to the PDF document page
Dim result As PdfGridLayoutResult = pdfGrid.Draw(page, New PointF(0, 20))
 
page.Graphics.DrawString("PdfLightTable", boldFont, PdfBrushes.Black, New PointF(0, result.Bounds.Bottom + 10))
 
'Declare a PdfLightTable
Dim pdfLightTable As New PdfLightTable()
 
'Set the data source as direct
pdfLightTable.DataSourceType = PdfLightTableDataSourceType.TableDirect
 
'Create columns
pdfLightTable.Columns.Add(New PdfColumn("ID"))
 
pdfLightTable.Columns.Add(New PdfColumn("Name"))
 
pdfLightTable.Columns.Add(New PdfColumn("Age"))
 
pdfLightTable.Columns.Add(New PdfColumn("Salary"))
 
'Add rows
pdfLightTable.Rows.Add(New Object() {"#E01", "@Clay", "25", "20000"})
 
pdfLightTable.Rows.Add(New Object() {"#E02", "@Thomas", "30", "35000"})
 
pdfLightTable.Style.ShowHeader = True
 
pdfLightTable.Style.HeaderStyle = New PdfCellStyle()
pdfLightTable.Style.HeaderStyle.Font = boldFont
 
pdfLightTable.Style.DefaultStyle = New PdfCellStyle()
pdfLightTable.Style.DefaultStyle.Font = regularFont
 
'Draw the PdfLightTable
pdfLightTable.Draw(result.Page, New PointF(0, result.Bounds.Bottom + 30))
 
'Save the PDF document
document.Save("Tables.pdf")
 
'Close the document
document.Close(True)
 
'This will open the PDF file so, the result will be seen in default PDF Viewer
System.Diagnostics.Process.Start("Tables.pdf")

 

  1. You can download the working sample from TableSample.Zip.
  2. By executing the program, the tables will be drawn one after another. You can get the PdfGrid and PdfLightTable rendered bounds in PdfGridLayoutResult and PdfLightTableLayoutResult class respectively. You will get the PDF document as follows. Screenshot of output PDF document

Take a moment to peruse the documentation, where you will find other options like create simple table, cell customization, and table styles.

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

An online sample link demonstrates how to draw table one after another in the PDF. 

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

 

Hope you enjoyed learning about how to draw more than one table sequentially in PDF using C#, VB.NET.

You can refer to our WinForms PDF’s feature tour page to know about its other groundbreaking feature representations. You can 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 sign in to leave a comment
Access denied
Access denied