How to create nested table in a PDF using C# and VB.NET in WF?
Syncfusion Essential PDF is a .NET PDF library used to create, read, and edit PDF documents. Using this library, you can create nested table in PDF using C# and VB.NET.
Steps to create nested table in PDF programmatically:
- Create a new C# console application project.
- Install the Syncfusion.Pdf.WinForms NuGet package as 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.Grid; using System.Drawing;
VB.NET
Imports Syncfusion.Pdf Imports Syncfusion.Pdf.Grid Imports System.Drawing
- Use the following code snippet to create nested table in a PDF.
C#
//Create a new PDF document PdfDocument pdfDocument = new PdfDocument(); //Create the page PdfPage pdfPage = pdfDocument.Pages.Add(); //Create the parent grid PdfGrid parentPdfGrid = new PdfGrid(); parentPdfGrid.Columns.Add(2); //Add the rows PdfGridRow row1 = parentPdfGrid.Rows.Add(); row1.Cells[0].Value = "Employee ID"; row1.Cells[1].Value = "E01"; PdfGridRow row2 = parentPdfGrid.Rows.Add(); row2.Cells[0].Value = "Employee Name"; row2.Cells[1].Value = "Simons Bistro"; PdfGridRow row3 = parentPdfGrid.Rows.Add(); row3.Cells[0].Value = "Salary"; row3.Cells[1].Value = "$10,000"; PdfGridRow row4 = parentPdfGrid.Rows.Add(); row4.Cells[0].Value = "Contact Details"; //Create the child table PdfGrid childPdfGrid = new PdfGrid(); childPdfGrid.Columns.Add(2); //Add the rows PdfGridRow childRow1 = childPdfGrid.Rows.Add(); childRow1.Cells[0].Value = "Phone"; childRow1.Cells[1].Value = "31 12 34 56"; PdfGridRow childRow2 = childPdfGrid.Rows.Add(); childRow2.Cells[0].Value = "Email"; childRow2.Cells[1].Value = "simonsbistro@outlook.com"; PdfGridRow childRow3 = childPdfGrid.Rows.Add(); childRow3.Cells[0].Value = "Address"; childRow3.Cells[1].Value = "Vinbaeltet 34, Denmark"; //Set the value as another PdfGrid in a cell parentPdfGrid.Rows[3].Cells[1].Value = childPdfGrid; parentPdfGrid.Rows[3].Cells[1].Style.CellPadding = new PdfPaddings(5, 5, 5, 5); //Draw the parent PdfGrid parentPdfGrid.Draw(pdfPage, PointF.Empty); //Save the document pdfDocument.Save("NestedTable.pdf"); //Close the document pdfDocument.Close(true); //This will open the PDF file so, the result will be seen in default PDF viewer System.Diagnostics.Process.Start("NestedTable.pdf");
VB.NET
'Create a new PDF document Dim pdfDocument As PdfDocument = New PdfDocument() 'Create the page Dim pdfPage As PdfPage = pdfDocument.Pages.Add() 'Create the parent grid Dim parentPdfGrid As PdfGrid = New PdfGrid() parentPdfGrid.Columns.Add(2) 'Add the rows Dim row1 As PdfGridRow = parentPdfGrid.Rows.Add() row1.Cells(0).Value = "Employee ID" row1.Cells(1).Value = "E01" Dim row2 As PdfGridRow = parentPdfGrid.Rows.Add() row2.Cells(0).Value = "Employee Name" row2.Cells(1).Value = "Simons Bistro" Dim row3 As PdfGridRow = parentPdfGrid.Rows.Add() row3.Cells(0).Value = "Salary" row3.Cells(1).Value = "$10,000" Dim row4 As PdfGridRow = parentPdfGrid.Rows.Add() row4.Cells(0).Value = "Contact Details" 'Create the child table Dim childPdfGrid As PdfGrid = New PdfGrid() childPdfGrid.Columns.Add(2) 'Add the rows Dim childRow1 As PdfGridRow = childPdfGrid.Rows.Add() childRow1.Cells(0).Value = "Phone" childRow1.Cells(1).Value = "31 12 34 56" Dim childRow2 As PdfGridRow = childPdfGrid.Rows.Add() childRow2.Cells(0).Value = "Email" childRow2.Cells(1).Value = "simonsbistro@outlook.com" Dim childRow3 As PdfGridRow = childPdfGrid.Rows.Add() childRow3.Cells(0).Value = "Address" childRow3.Cells(1).Value = "Vinbaeltet 34, Denmark" 'Set the value as another PdfGrid in a cell parentPdfGrid.Rows(3).Cells(1).Value = childPdfGrid parentPdfGrid.Rows(3).Cells(1).Style.CellPadding = New PdfPaddings(5, 5, 5, 5) 'Draw the PdfGrid parentPdfGrid.Draw(pdfPage, PointF.Empty) 'Save the document pdfDocument.Save("NestedTable.pdf") 'Close the document pdfDocument.Close(True) 'This will open the PDF file so, the result will be seen in default PDF viewer System.Diagnostics.Process.Start("NestedTable.pdf")
A complete working sample can be downloaded from NestedTableSample.zip.
By executing the program, you will get the PDF document as follows.
Take a moment to peruse the documentation, where you can find the options like creating a simple table using PdfLightTable and PdfGrid, cell and row customization in PdfLightTable and PdfGrid, built-in table styles to PdfGrid, pagination in PdfGrid and PdfLightTable with code examples.
Refer here to explore the rich set of Syncfusion Essential PDF features.
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 nested table in a PDF using C# and VB.NET in WF.
You can refer to our PDF feature tour page to know about its other groundbreaking feature representations. You can also explore our documentation 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 forums, Direct-Trac, or feedback portal. We are always happy to assist you!