Articles in this section

How to add multiple tables sequentially in a 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 add multiple PDF grids one after another in the PDF document using C# and VB.NET.

Steps to add multiple PDF grids one after another programmatically:

  1. Create a new C# Windows Forms application project. Create a new C# Windows Forms application project in .NET PDF library
  2. Install the Syncfusion.Pdf.WinForms NuGet package as a reference to your .NET Framework application from NuGet.org. NuGet package reference screenshot in .NET PDF library
  3. Include the following namespaces in the Form1.Designer.cs file.

C#

using Syncfusion.Pdf;
using Syncfusion.Pdf.Grid;
using Syncfusion.Pdf.Graphics;
using System.Drawing;
using System.Data;

 

VB.NET

Imports Syncfusion.Pdf
Imports Syncfusion.Pdf.Grid
Imports Syncfusion.Pdf.Graphics
Imports System.Drawing
Imports System.Data

 

  1. Use the following code snippet to add multiple PDF grids sequentially.

C#

//Create a new PDF document
PdfDocument doc = new PdfDocument();
//Add a page
PdfPage page = doc.Pages.Add();
//Create a PdfGrid
PdfGrid pdfGrid1 = new PdfGrid();
//Create a DataTable
DataTable dataTable1 = new DataTable();
//Add columns to the DataTable
dataTable1.Columns.Add("OrderID");
dataTable1.Columns.Add("CustomerID");
dataTable1.Columns.Add("ShipName");
dataTable1.Columns.Add("ShipAddress");
dataTable1.Columns.Add("ShipCity");
dataTable1.Columns.Add("ShipPostalCode");
dataTable1.Columns.Add("ShipCountry");
//Add rows to the DataTable
dataTable1.Rows.Add(new object[] { "10248", "VINET", "Vins et alcools Chevalier", "59 rue de l'Abbaye", "Reims", "51100", "France" });
dataTable1.Rows.Add(new object[] { "10249", "TOMSP", "Toms Spezialitäten", "Luisenstr. 48", "Münster", "44087", "Germany" });
dataTable1.Rows.Add(new object[] { "10250", "HANAR", "Hanari Carnes", "Rua do Paço, 67", "Rio de Janeiro", "05454-876", "Brazil" });
dataTable1.Rows.Add(new object[] { "10251", "VICTE", "Victuailles en stock", "2, rue du Commerce", "Lyon", "69004", "France" });
dataTable1.Rows.Add(new object[] { "10252", "SUPRD", "Suprêmes délices", "Boulevard Tirou, 255", "Charleroi", "B-6000", "Belgium" });
dataTable1.Rows.Add(new object[] { "10253", "HANAR", "Hanari Carnes", "Rua do Paço, 67", "Rio de Janeiro", "05454-876", "Brazil" });
//Assign data source
pdfGrid1.DataSource = dataTable1;
//Add layout format for grid pagination
PdfGridLayoutFormat layoutFormat = new PdfGridLayoutFormat();
layoutFormat.Layout = PdfLayoutType.Paginate;
//Draw grid to the page of PDF document
PdfLayoutResult result = pdfGrid1.Draw(page, new PointF(10, 10), layoutFormat);
//Create a PdfGrid
PdfGrid pdfGrid2 = new PdfGrid();
//Create a DataTable
DataTable dataTable2 = new DataTable();
//Add columns to the DataTable
dataTable2.Columns.Add("CustomerID");
dataTable2.Columns.Add("CompanyName");
dataTable2.Columns.Add("ContactName");
dataTable2.Columns.Add("Address");
dataTable2.Columns.Add("City");
dataTable2.Columns.Add("PostalCode");
dataTable2.Columns.Add("Country");
dataTable2.Columns.Add("Phone");
dataTable2.Columns.Add("Fax");
//Add rows to the DataTable
dataTable2.Rows.Add(new object[] { "ALFKI", "Alfreds Futterkiste", "Maria Anders", "Obere Str. 57", "Berlin", "12209", "Germany", "030-0074321", "030-0076545" });
dataTable2.Rows.Add(new object[] { "ANATR", "Ana Trujillo Emparedados yhelados", "Ana Trujillo", "Avda. de la Constitución 2222", "México D.F.", "05021", "Mexico", "(5) 555-4729", "(5) 555-3745" });
dataTable2.Rows.Add(new object[] { "ANTON", "Antonio Moreno Taquería", "Antonio Moreno", "Mataderos 2312", "México D.F.", "05023", "Mexico", "(5) 555-3932", "" });
dataTable2.Rows.Add(new object[] { "BLAUS", "Blauer See Delikatessen", "Hanna Moos", "Forsterstr. 57", "Mannheim", "68306", "Germany", "0621-08460", "0621-08924" });
dataTable2.Rows.Add(new object[] { "DRACD", "Drachenblut Delikatessen", "Sven Ottlieb", "Walserweg 21", "Aachen", "52066", "Germany", "0241-039123", "0241-059428" });
//Assign data source
pdfGrid2.DataSource = dataTable2;
//Draw grid to the resultant page of the first grid
pdfGrid2.Draw(result.Page, new PointF(10, result.Bounds.Height + 20));
//Save and close the document
doc.Save("SequentTables.pdf");
doc.Close(true);

 

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 PDF Grid
Dim pdfGrid1 As PdfGrid = New PdfGrid()
'Create a DataTable
Dim dataTable1 As DataTable = New DataTable()
'Add columns to the DataTable
dataTable1.Columns.Add("OrderID")
dataTable1.Columns.Add("CustomerID")
dataTable1.Columns.Add("ShipName")
dataTable1.Columns.Add("ShipAddress")
dataTable1.Columns.Add("ShipCity")
dataTable1.Columns.Add("ShipPostalCode")
dataTable1.Columns.Add("ShipCountry")
'Add rows to the DataTable
dataTable1.Rows.Add(New Object() {"10248", "VINET", "Vins et alcools Chevalier", "59 rue de l'Abbaye", "Reims", "51100", "France"})
dataTable1.Rows.Add(New Object() {"10249", "TOMSP", "Toms Spezialitäten", "Luisenstr. 48", "Münster", "44087", "Germany"})
dataTable1.Rows.Add(New Object() {"10250", "HANAR", "Hanari Carnes", "Rua do Paço, 67", "Rio de Janeiro", "05454-876", "Brazil"})
dataTable1.Rows.Add(New Object() {"10251", "VICTE", "Victuailles en stock", "2, rue du Commerce", "Lyon", "69004", "France"})
dataTable1.Rows.Add(New Object() {"10252", "SUPRD", "Suprêmes délices", "Boulevard Tirou, 255", "Charleroi", "B-6000", "Belgium"})
dataTable1.Rows.Add(New Object() {"10253", "HANAR", "Hanari Carnes", "Rua do Paço, 67", "Rio de Janeiro", "05454-876", "Brazil"})
'Assign data source
pdfGrid1.DataSource = dataTable1
'Add layout format for grid pagination
Dim layoutFormat As PdfGridLayoutFormat = New PdfGridLayoutFormat()
layoutFormat.Layout = PdfLayoutType.Paginate
'Draw grid to the page of PDF document
Dim result As PdfLayoutResult = pdfGrid1.Draw(page, New PointF(10, 10), layoutFormat)
'Create a PDF Grid
Dim pdfGrid2 As PdfGrid = New PdfGrid()
'Create a DataTable
Dim dataTable2 As DataTable = New DataTable()
'Add columns to the DataTable
dataTable2.Columns.Add("CustomerID")
dataTable2.Columns.Add("CompanyName")
dataTable2.Columns.Add("ContactName")
dataTable2.Columns.Add("Address")
dataTable2.Columns.Add("City")
dataTable2.Columns.Add("PostalCode")
dataTable2.Columns.Add("Country")
dataTable2.Columns.Add("Phone")
dataTable2.Columns.Add("Fax")
'Add rows to the DataTable
dataTable2.Rows.Add(New Object() {"ALFKI", "Alfreds Futterkiste", "Maria Anders", "Obere Str. 57", "Berlin", "12209", "Germany", "030-0074321", "030-0076545"})
dataTable2.Rows.Add(New Object() {"ANATR", "Ana Trujillo Emparedados yhelados", "Ana Trujillo", "Avda. de la Constitución 2222", "México D.F.", "05021", "Mexico", "(5) 555-4729", "(5) 555-3745"})
dataTable2.Rows.Add(New Object() {"ANTON", "Antonio Moreno Taquería", "Antonio Moreno", "Mataderos 2312", "México D.F.", "05023", "Mexico", "(5) 555-3932", ""})
dataTable2.Rows.Add(New Object() {"BLAUS", "Blauer See Delikatessen", "Hanna Moos", "Forsterstr. 57", "Mannheim", "68306", "Germany", "0621-08460", "0621-08924"})
dataTable2.Rows.Add(New Object() {"DRACD", "Drachenblut Delikatessen", "Sven Ottlieb", "Walserweg 21", "Aachen", "52066", "Germany", "0241-039123", "0241-059428"})
'Assign data source
pdfGrid2.DataSource = dataTable2
'Draw grid to the resultant page of the first grid
pdfGrid2.Draw(result.Page, New PointF(10, (result.Bounds.Height + 20)))
'Save and close the document
doc.Save("SequentTables.pdf")
doc.Close(True)

 

A complete working sample can be downloaded from AddMultipleTablesSequentially.zip.

By executing the program, you will get the PDF document as follows. Screenshot of document in .NET PDF library

Take a moment to peruse the documentation for working with tables, where you will find other options like grid pagination and different levels of grid customization.

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

An online sample link to create a richly formatted table.


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 multiple tables sequentially in a PDF using C# and VB.NET.

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)
Access denied
Access denied