How to Insert Nested Table in a Cell (PDF) – VB.NET

Sometimes, we can find that a cell of one table isn’t filled with text or number but a small table. Generally speaking, the nested table shows items which are children of data in the cell. For example, cell 1 is data about one product which has three models. We can insert a nested table with models information in cell 1. In this post, I will show how to insert nested table in PDF by using VB.NET.

In my example, I will show a table about vendor information. The first vendor includes several products. So, there will be a nested table insert in first vendor name cell. Besides inserting it, I will set format for the main table and calculate how many vendors a country has.

The following steps show details.

  1. Create PDF document and set margin.
  2. Add one page for drawing table and draw a title for this table, including title format and location.
  3. Write data in table. Define grid and set cell padding.
  4. Draw table header. Firstly, confirm header data and add columns according to header length. Secondly, set column width and add header row. Thirdly, set header format.
  5. Format data. Firstly, add and format rows. Secondly, fill data in rows and set alignment for data in each cell. Thirdly, because when starting with step 5, I set a variable groupbyCountry as dictionary type. At this time, get its key and value according to data in row c 3.
  6. Calculate total amount. Total amount is amount of vendors a country has. Firstly, declare totalAmount variable. Secondly, assign its value as groupByCountry value and set format. Thirdly, add total amount row and get value in cells.
  7. Append product list. Now, I will insert a nested table in first cell. Firstly, get nested table data from database, which is product information which vendor in first cell provides. Secondly, set header of nested table (product list). The header name is vendor name. Finally, set format for product list.
  8. Set PDF document layout.
  9. Save and Launch file.

Main Coding:

Imports System.Text

Imports System.Data.OleDb

Imports System.Drawing

Imports Spire.Pdf

Imports Spire.Pdf.Graphics

Imports Spire.Pdf.Grid

Module Grid

    Sub Main()


        Dim doc As New PdfDocument()


        Dim unitCvtr As New PdfUnitConvertor()

        Dim margin As New PdfMargins()

        margin.Top = unitCvtr.ConvertUnits(2.54F, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point)

        margin.Bottom = margin.Top

        margin.Left = unitCvtr.ConvertUnits(3.17F, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point)

        margin.Right = margin.Left

        ‘Add Page

        Dim page As PdfPageBase = doc.Pages.Add(PdfPageSize.A4, margin, PdfPageRotateAngle.RotateAngle0, PdfPageOrientation.Landscape)

        Dim y As Single = 20

        Dim x1 As Single = page.Canvas.ClientSize.Width


        Dim brush1 As PdfBrush = PdfBrushes.DarkBlue

        Dim font1 As New PdfTrueTypeFont(New Font(“Calibri”, 16.0F, FontStyle.Regular), True)

        Dim format1 As New PdfStringFormat(PdfTextAlignment.Center)

        page.Canvas.DrawString(“Vendor List”, font1, brush1, page.Canvas.ClientSize.Width / 2, y, format1)

        y = y + font1.MeasureString(“Vendor List”, format1).Height

        y = y + 5


        Dim data() As String = {“VendorName;Address1;City;Country”, “Cacor Corporation;161 Southfield Rd;Southfield;U.S.A.;”, “Underwater;50 N 3rd Street;Indianapolis;U.S.A.;”, “J.W.  Luscher Mfg.;65 Addams Street;Berkely;U.S.A.;”, “Scuba Professionals;3105 East Brace;Rancho Dominguez;U.S.A.;”, “Divers’ Supply Shop;5208 University Dr;Macon;U.S.A.;”, “Techniques;52 Dolphin Drive;Redwood City;U.S.A.;”, “Perry Scuba;3443 James Ave;Hapeville;U.S.A.;”, “Beauchat, Inc.;45900 SW 2nd Ave;Ft Lauderdale;U.S.A.;”, “Amor Aqua;42 West 29th Street;New York;U.S.A.;”}

        Dim grid As New Spire.Pdf.Grid.PdfGrid()

        grid.Style.CellPadding = New PdfPaddings(1, 1, 1, 1)

        ‘Table Header

        Dim header() As String = data(0).Split(“;”c)


        Dim width As Single = page.Canvas.ClientSize.Width – (grid.Columns.Count + 1)

        grid.Columns(0).Width = width * 0.25F

        grid.Columns(1).Width = width * 0.25F

        grid.Columns(2).Width = width * 0.25F

        grid.Columns(3).Width = width * 0.15F

        Dim headerRow As PdfGridRow = grid.Headers.Add(1)(0)

        headerRow.Style.Font = New PdfTrueTypeFont(New Font(“Calibri”, 14.0F, FontStyle.Bold), True)

        headerRow.Style.TextBrush = PdfBrushes.White

        headerRow.Style.BackgroundBrush = PdfBrushes.LimeGreen

        For i As Integer = 0 To header.Length – 1

            headerRow.Cells(i).Value = header(i)

            headerRow.Cells(i).StringFormat = New PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle)

        Next i

        ‘Data Format

        Dim groupByCountry As New Dictionary(Of String, Integer)()

        For r As Integer = 1 To data.Length – 1

            Dim row As PdfGridRow = grid.Rows.Add()

            row.Style.Font = New PdfTrueTypeFont(New Font(“Calibri”, 12.0F), True)

            row.Style.TextBrush = PdfBrushes.DarkCyan

            row.Style.BackgroundBrush = PdfBrushes.AliceBlue

            Dim rowData() As String = data(r).Split(“;”c)

            For c As Integer = 0 To rowData.Length – 2

                row.Cells(c).Value = rowData(c)

                row.Cells(c).StringFormat = New PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle)

                If c = 3 Then

                    If groupByCountry.ContainsKey(rowData(c)) Then

                        groupByCountry(rowData(c)) = groupByCountry(rowData(c)) + 1


                        groupByCountry(rowData(c)) = 1

                    End If

                End If

            Next c

        Next r

        Dim totalAmount As New StringBuilder()

        For Each country As KeyValuePair(Of String, Integer) In groupByCountry

            totalAmount.AppendFormat(“{0}:” & vbTab & “{1}”, country.Key, country.Value)


        Next country

        Dim totalAmountRow As PdfGridRow = grid.Rows.Add()

        totalAmountRow.Style.BackgroundBrush = PdfBrushes.WhiteSmoke

        totalAmountRow.Cells(0).Value = “Total Amount”

        totalAmountRow.Cells(0).Style.Font = New PdfTrueTypeFont(New Font(“Calibri”, 12.0F, FontStyle.Bold), True)

        totalAmountRow.Cells(0).Style.TextBrush = PdfBrushes.Red

        totalAmountRow.Cells(0).StringFormat = New PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle)

        totalAmountRow.Cells(1).ColumnSpan = 3

        totalAmountRow.Cells(1).Value = totalAmount.ToString()

        totalAmountRow.Cells(1).Style.Font = New PdfTrueTypeFont(New Font(“Calibri”, 12.0F, FontStyle.Bold), True)

        totalAmountRow.Cells(1).Style.TextBrush = PdfBrushes.Red

        totalAmountRow.Cells(1).StringFormat = New PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle)

        ‘Append Product List

        Dim productList As New Spire.Pdf.Grid.PdfGrid()

        productList.Style.Font = New PdfTrueTypeFont(New Font(“Calibri”, 12.0F), True)

        Using conn As New OleDbConnection()

            conn.ConnectionString = “Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\work\My Documents\demo.mdb”

            Dim command As New OleDbCommand()

            command.CommandText = ” select p.Description “ & ” from vendors v “ & ”     inner join parts p “ & ”     on v.VendorNo = p.VendorNo “ & ” where v.VendorName = ‘Cacor Corporation'”

            command.Connection = conn

            Using dataAdapter As New OleDbDataAdapter(command)

                Dim dataTable As New DataTable()


                productList.DataSource = dataTable

            End Using

        End Using

        productList.Headers(0).Cells(0).Value = “Cacor Corporation”

        productList.Headers(0).Cells(0).Style.Font = New PdfTrueTypeFont(New Font(“Calibri”, 12.0F, FontStyle.Regular), True)

        productList.Headers(0).Cells(0).Style.Borders.All = New PdfPen(New PdfTilingBrush(New SizeF(1, 1)), 0)

        productList.Style.TextBrush = PdfBrushes.DarkCyan

        grid.Rows(0).Cells(0).Value = productList

        grid.Rows(0).Cells(0).StringFormat.Alignment = PdfTextAlignment.Left


        Dim result As PdfLayoutResult = grid.Draw(page, New PointF(0, y))

        y = y + result.Bounds.Height + 5






    End Sub

End Module

Result Shown by Following:


Click Here to Learn more about Spire.PDF

Click Here to Download Spire.PDF


Author: janewdaisy

.NET Program Beginner. Share methods about how to use C#/VB.NET to export data, operate Word, Excel, PDF and other useful skills.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s