Add a table
Tables organize related values into rows and columns, from a short price list to a detailed invoice. Create a two-column table with a header, three item rows, and a total footer. This example follows AddTableToPdf.cs, applies CSS to the cells, and saves the result as AddTableToPdf_output.pdf.
What you’ll create
- An A5 PDF with 36-point page margins.
- An
Itemscolumn that is three times as wide as theAmountcolumn. - A navy header and footer with white text, three item rows, and a footer total of
$60.00. - Cell padding, borders, a shaded second row, and right-aligned amounts defined through CSS.
- A file named
AddTableToPdf_output.pdfin your chosen output directory.

Example code
Add the following class to a console project that references the DynamicPDF Flow API. See the quick start for an overview. The class adds a styled table and saves the document to an existing output directory.
using System;
using System.IO;
using DynamicPDF.Flow;
namespace FluentExamples.Examples;
internal static class AddTableToPdf
{
public static void Run(string outputRoot)
{
var path = Path.Combine(
outputRoot, "AddTableToPdf_output.pdf");
DocumentBuilder.Create()
.Title("Add a table to a PDF")
.Style("""
* {
font-family: Helvetica;
font-size: 12pt;
color: #111827;
}
.cell {
padding: 6pt;
border: 0.5pt solid #cbd5e1;
vertical-align: middle;
}
.header {
font-weight: bold;
color: #ffffff;
background-color: navy;
border: 0.5pt solid #94a3b8;
}
.stripe {
background-color: #f1f5f9;
}
.footer {
font-weight: bold;
color: #ffffff;
background-color: navy;
border: 0.5pt solid #64748b;
}
.amount {
text-align: right;
}
""")
.Pages(page => page
.Size(PageSize.A5)
.Margins(36, LengthUnit.Point)
.Table(table => table
.Columns(columns => columns
.RelativeWidth(3)
.RelativeWidth())
.Header(row => row
.Cell("Items", "cell header")
.Cell("Amount", "cell header amount"))
.Row(row => row
.Cell("Item-1", "cell")
.Cell("$10.00", "cell amount"))
.Row(row => row
.Cell("Item-2", "cell stripe")
.Cell("$20.00", "cell stripe amount"))
.Row(row => row
.Cell("Item-3", "cell")
.Cell("$30.00", "cell amount"))
.Footer(row => row
.Cell("Total", "cell footer")
.Cell("$60.00", "cell footer amount"))))
.Save(path);
Console.WriteLine($"Created {path}");
}
}
Imports System
Imports System.IO
Imports DynamicPDF.Flow
Friend Module AddTableToPdf
Public Sub Run(outputRoot As String)
Dim outputPath = Path.Combine(
outputRoot, "AddTableToPdf_output.pdf")
DocumentBuilder.Create().
Title("Add a table to a PDF").
Style("* {
font-family: Helvetica;
font-size: 12pt;
color: #111827;
}
.cell {
padding: 6pt;
border: 0.5pt solid #cbd5e1;
vertical-align: middle;
}
.header {
font-weight: bold;
color: #ffffff;
background-color: navy;
border: 0.5pt solid #94a3b8;
}
.stripe {
background-color: #f1f5f9;
}
.footer {
font-weight: bold;
color: #ffffff;
background-color: navy;
border: 0.5pt solid #64748b;
}
.amount {
text-align: right;
}").
Pages(Sub(page) page.
Size(PageSize.A5).
Margins(36, unit:=LengthUnit.Point).
Table(Sub(table) table.
Columns(Sub(columns) columns.
RelativeWidth(3).
RelativeWidth()).
Header(Sub(row) row.
Cell("Items", "cell header").
Cell("Amount", "cell header amount")).
Row(Sub(row) row.
Cell("Item-1", "cell").
Cell("$10.00", "cell amount")).
Row(Sub(row) row.
Cell("Item-2", "cell stripe").
Cell("$20.00", "cell stripe amount")).
Row(Sub(row) row.
Cell("Item-3", "cell").
Cell("$30.00", "cell amount")).
Footer(Sub(row) row.
Cell("Total", "cell footer").
Cell("$60.00", "cell footer amount")))).
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 AddTableToPdf_output.pdf. Make sure the output directory exists before calling Run. Saving overwrites an existing file with the same name.
2. Set the title and define shared CSS rules
DocumentBuilder.Create() starts the document. .Title("Add a table to a PDF") sets its metadata title before adding pages; it does not add visible text to the page.
The document-level .Style(...) defines the shared stylesheet. The * rule sets the base font family, size, and text color. Named rules use dotted class selectors, such as .cell, and cells select them through the second argument of .Cell(...).
| Class | Purpose |
|---|---|
cell | Adds 6-point padding, a thin border, and middle vertical alignment. |
header | Sets bold white text, a navy background, and the header border. |
stripe | Adds a light background to the second item row. |
footer | Sets bold white text, a navy background, and the footer border. |
amount | Aligns cell text to the right. |
A cell can combine classes. For example, "cell header amount" applies the base cell styling, the header appearance, and right alignment.
3. Configure the page and columns
.Size(PageSize.A5) selects an A5 page, and .Margins(36, LengthUnit.Point) sets all page margins to 36 points.
Inside .Table(...), .Columns(...) defines relative widths. .RelativeWidth(3) gives the first column three shares of the available table width, while .RelativeWidth() gives the second column one share. The table flows within the page margins.
4. Add the header, item rows, and footer
.Header(...) adds the Items and Amount headings. Each .Row(...) adds a pair of cells, and .Footer(...) adds the total row.
The row builder’s .Cell(text, classes) adds text and selects the CSS classes for that cell. It returns the row builder so the next .Cell(...) adds the adjacent cell. The second item row adds stripe to both cells, and all amount cells add amount.
The amounts and $60.00 total are literal strings. This example does not calculate totals or format numeric data.
5. Save the PDF
.Save(path) builds and saves the document through the fluent builder. Console.WriteLine prints the output path so you can find and open the file.
Next steps
Replace the item labels and amounts with your own data, adjust the relative column widths, or change the CSS colors and padding. This example uses cell padding; it does not add gaps between cells or set fixed table dimensions.
Explore Styling text using CSS for more styling examples, or browse all examples.
To build a table from records, call table.RowCells(...) once per record and pass the cell text in column order. The current Flow API takes strings here, so parse JSON and format values in C# before adding the row.
TableAdvancedExamples.cs shows both approaches. The JSON example reads product records with System.Text.Json and saves table-json-data-rows.pdf. The object examples format values using de-DE and save both tables in table-data-rows.pdf. Both examples use empty strings for null values and save to the supplied output directory.
The same source shows how to span cells across columns with ColSpan(...) and across rows with RowSpan(...).