Skip to main content

Create a nested table

Create a nested table

Put a table inside a cell to show details that belong to a single row. This packing list places an item and quantity table inside the Contents cell for Box 1.

Fluent recipe / 09Create a nested tablePlace an item and quantity table inside a packing-list cell.

What you’ll create​

  • An A5 PDF containing a packing list with Package and Contents columns.
  • A nested table inside the Contents cell, with a header and two item rows.
  • A file named NestedTablePdf_output.pdf in your chosen output directory.
Packing list with a nested Item and Qty table inside the Contents cell for Box 1.

Example code​

Add the following class to a console project that references the DynamicPDF Flow API. See the quick start for an overview. The example adds a nested table and saves the PDF to an existing output directory.

using DynamicPDF.Flow;

namespace FluentExamples.Examples;

internal static class NestedTablePdf
{
public static void Run(string outputRoot)
{
var path = Path.Combine(
outputRoot, "NestedTablePdf_output.pdf");

DocumentBuilder.Create()
.Title("Packing list")
.Style("""
.outer {
padding: 8pt;
border: 1pt solid #334155;
}
.inner {
padding: 4pt;
border: 0.5pt solid #94a3b8;
}
.heading {
font-weight: bold;
background-color: #e2e8f0;
}
""")
.Pages(page => page
.Size(PageSize.A5)
.Margins(36, LengthUnit.Point)
.Table(table => table
.Columns(columns => columns
.RelativeWidth()
.RelativeWidth(2))
.Header(row => row
.Cell("Package", "outer heading")
.Cell("Contents", "outer heading"))
.Row(row => row
.Cell("Box 1", "outer")
.Cell(cell => cell
.Class("outer")
.Table(items => items
.Columns(2)
.Header(header => header
.Cell("Item",
"inner heading")
.Cell("Qty",
"inner heading"))
.Row(item => item
.Cell("Notebook", "inner")
.Cell("2", "inner"))
.Row(item => item
.Cell("Pen", "inner")
.Cell("4", "inner")))))))
.Save(path);

Console.WriteLine($"Created {path}");
}
}
Imports System
Imports System.IO
Imports DynamicPDF.Flow

Friend Module NestedTablePdf
Public Sub Run(outputRoot As String)
Dim outputPath = Path.Combine(
outputRoot, "NestedTablePdf_output.pdf")

DocumentBuilder.Create().
Title("Packing list").
Style(".outer {
padding: 8pt;
border: 1pt solid #334155;
}
.inner {
padding: 4pt;
border: 0.5pt solid #94a3b8;
}
.heading {
font-weight: bold;
background-color: #e2e8f0;
}").
Pages(Sub(page) page.
Size(PageSize.A5).
Margins(36, unit:=LengthUnit.Point).
Table(Sub(table) table.
Columns(Sub(columns) columns.
RelativeWidth().
RelativeWidth(2)).
Header(Sub(row) row.
Cell("Package", "outer heading").
Cell("Contents", "outer heading")).
Row(Sub(row) row.
Cell("Box 1", "outer").
Cell(Sub(cell) cell.
Class("outer").
Table(Sub(items) items.
Columns(2).
Header(Sub(header) header.
Cell("Item", "inner heading").
Cell("Qty", "inner heading")).
Row(Sub(item) item.
Cell("Notebook", "inner").
Cell("2", "inner")).
Row(Sub(item) item.
Cell("Pen", "inner").
Cell("4", "inner"))))))).
Save(outputPath)

Console.WriteLine($"Created {outputPath}")
End Sub
End Module

How it works​

1. Choose where to save the PDF​

Path.Combine joins outputRoot with NestedTablePdf_output.pdf. Make sure the output directory exists before calling Run.

2. Set the title and define cell styles​

.Title("Packing list") sets the document title in the PDF metadata. The document-level .Style(...) defines the outer and inner classes for cell padding and borders. Both tables use the heading class for bold text and shaded header cells.

3. Build the outer table​

.Size(PageSize.A5) selects an A5 page with 36-point margins. Inside .Table(...), the column definitions give the Contents column twice the width of Package. The header names the columns, and the first cell of the body row labels the package Box 1.

4. Add a table inside the Contents cell​

The callback passed to the second .Cell(...) gives you a TableCellBuilder. Call .Table(...) on that builder to place a new table inside the cell. The nested table defines its own Item and Qty columns and adds rows for two notebooks and four pens.

5. Save the PDF​

.Save(path) writes the PDF to outputRoot and overwrites an existing file with the same name. The example prints the output path.

Next steps​

Add another package row with its own contents table, or see Add a table for more examples of column sizing and cell styles.