Add Table to PDF (.NET Core/Framework)
Adding a table to a PDF using DynamicPDF Core Suite is straightforward, as the example on this page illustrates.
Watch the Video
How to Add a Table to a PDF in C#
The following steps and sample code illustrates adding a table to a PDF document using DynamicPDF Core Suite.
Steps for Adding a Table to a PDF Document
- Create a
Document
object. - Create a
Page
object and add it to theDocument
instance. - Create a
Table2
object. - Create
Column2
object and columns to the table. - Create
Row2
object and add rows to the table. - Create
Cell2
object to add the cells to rows. - Set the necessary table parameters (Border, Cell spacing, Cell padding).
- Add the table to the
Page
instance. - Save the PDF document.
Sample Code - C#
Document document = new Document();
Page page = new Page();
document.Pages.Add(page);
Table2 table = new Table2(0, 0, 600, 600);
Column2 column1 = table.Columns.Add(150);
column1.CellDefault.Align = TextAlign.Center;
table.Columns.Add(90);
table.Columns.Add(90);
table.Columns.Add(90);
Row2 row1 = table.Rows.Add(40, Font.HelveticaBold, 16, Grayscale.Black, Grayscale.Gray);
row1.CellDefault.Align = TextAlign.Center;
row1.CellDefault.VAlign = VAlign.Center;
row1.Cells.Add("Header 1");
row1.Cells.Add("Header 2");
row1.Cells.Add("Header 3");
row1.Cells.Add("Header 4");
Row2 row2 = table.Rows.Add(30);
Cell2 cell1 = row2.Cells.Add("Rowheader 1", Font.HelveticaBold, 16, Grayscale.Black, Grayscale.Gray, 1);
cell1.Align = TextAlign.Center;
cell1.VAlign = VAlign.Center;
row2.Cells.Add("Item 1");
row2.Cells.Add("Item 2");
row2.Cells.Add("Item 3");
Row2 row3 = table.Rows.Add(30);
Cell2 cell2 = row3.Cells.Add("Rowheader 2", Font.HelveticaBold, 16, Grayscale.Black, Grayscale.Gray, 1);
cell2.Align = TextAlign.Center;
cell2.VAlign = VAlign.Center;
row3.Cells.Add("Item 4");
row3.Cells.Add("Item 5");
row3.Cells.Add("Item 6");
table.CellDefault.Padding.Value = 5.0f;
table.CellSpacing = 5.0f;
table.Border.Top.Color = RgbColor.Blue;
table.Border.Bottom.Color = RgbColor.Blue;
table.Border.Top.Width = 2;
table.Border.Bottom.Width = 2;
table.Border.Left.LineStyle = LineStyle.None;
table.Border.Right.LineStyle = LineStyle.None;
page.Elements.Add(table);
document.Draw(@"Output.pdf");
GitHub Project
Clone or view the example project at GitHub. This example code is contained in the Examples/TableExample.cs file.
Getting Started
NuGet Package
DynamicPDF Core Suite is available on NuGet and is part of the ceTe.DynamicPDF.CoreSuite.NET
package. The easiest way to install the package is through the Visual Studio Package Manager. You can also download the package directly from NuGet.
DynamicPDF Core Suite Information
Adding a table to a PDF is just one of DynamicPDF Core Suite's over 75 page elements you can use to add rich content to the PDFs you create. Using these elements, you can create:
- Images,
- Text Objects (TextArea and Label),
- HTML,
- Barcodes (47 types),
- Form Fields,
- and Charts.
More information on DynamicPDF Core Suite can be found on its website.
- [DynamicPDF Core Suite for .NET](https://www.dynamicpdf.com/docs/dotnet/dynamic-pdf-core-suite-welcome"Create PDFs in .NET Core")
Available on Other Platforms
DynamicPDF Core Suite is also available for the Java and COM/ActiveX platforms. Refer to the respective product pages for more details.
- Java - DynamicPDF Generator for Java
- COM/ActiveX - DynamicPDF Generator for COM/ActiveX