How to Generate PDF Table in C#, VB.NET

It is easy to generate word table in C#/VB.NET with the help of Spire.Doc. It is also easy to generate PDF table in C#/VB.NET using Spire.PDF. The following section will demonstrate how to generate PDF table in C#/VB.NET. Before we can generate PDF tables in C#/VB.NET, we have to download Spire.PDF and install it on system first.

Now follow the steps to generate PDF table in your C#/VB.NET.

Step 1: Create project and add reference

Create a project in Visual Studion and add Spire.PDF dll as reference in your new project. The default folder of Spire.PDF.dll is "C:\Program Files\e-iceblue\Spire.PDF\Bin".

Step 2: Import namespace

[C#]
           using Spire.Pdf;
           using Spire.Pdf.Graphics;
           using Spire.Pdf.Tables;
[VB.NET]
Imports Spire.Pdf
Imports Spire.Pdf.Graphics
Imports Spire.Pdf.Tables

Step 3: Create PDF and make setting

[C#]
//Create a pdf document.
PdfDocument doc = new PdfDocument(); //margin PdfUnitConvertor unitCvtr = new PdfUnitConvertor(); PdfMargins margin = 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; // Create new page PdfPageBase page = doc.Pages.Add(PdfPageSize.A4, margin); float y = 10; //title PdfBrush brush1 = PdfBrushes.Black; PdfTrueTypeFont font1 = new PdfTrueTypeFont(new Font("Arial", 16f, FontStyle.Bold)); PdfStringFormat format1 = new PdfStringFormat(PdfTextAlignment.Center); page.Canvas.DrawString("Country List", font1, brush1, page.Canvas.ClientSize.Width / 2, y, format1); y = y + font1.MeasureString("Country List", format1).Height; y = y + 5;
[VB.NET]
'Create a pdf document.
Dim doc As New PdfDocument()
'margin
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
' Create new page
Dim page As PdfPageBase = doc.Pages.Add(PdfPageSize.A4, margin)
Dim y As Single = 10
'title
Dim brush1 As PdfBrush = PdfBrushes.Black
Dim font1 As New PdfTrueTypeFont(New Font("Arial", 16F, FontStyle.Bold))
Dim format1 As New PdfStringFormat(PdfTextAlignment.Center)
page.Canvas.DrawString("Country List", font1, brush1, page.Canvas.ClientSize.Width / 2, y, format1)
y = y + font1.MeasureString("Country List", format1).Height
y = y + 5

Step 4: Set PDF table's data source

Spire.PDF enables users to generate PDF table from database and other sources. Here we take string for example.

[C#]
            String[] data
                = {
                    "Name;Capital;Continent;Area;Population",
                    "Argentina;Buenos Aires;South America;2777815;32300003",
                    "Bolivia;La Paz;South America;1098575;7300000",
                    "Brazil;Brasilia;South America;8511196;150400000",
                    "Canada;Ottawa;North America;9976147;26500000",
                    };
            String[][] dataSource
                = new String[data.Length][];
            for (int i = 0; i < data.Length; i++)
            {
                dataSource[i] = data[i].Split(';');
            }
[VB.NET]
Dim data As [String]() = {"Name;Capital;Continent;Area;Population", "Argentina;Buenos Aires;South America;2777815;32300003", "Bolivia;La Paz;South America;1098575;7300000", "Brazil;Brasilia;South America;8511196;150400000", "Canada;Ottawa;North America;9976147;26500000"}
Dim dataSource As [String]()() = New [String](data.Length - 1)() {}
For i As Integer = 0 To data.Length - 1
	dataSource(i) = data(i).Split(";"C)
Next

Step 5: Create PDF table in C#/VB.NET and set its formats

[C#]
 PdfTable table = new PdfTable();
            table.Style.CellPadding = 2;
            table.Style.HeaderSource = PdfHeaderSource.Rows;
            table.Style.HeaderRowCount = 1;
            table.Style.ShowHeader = true;
            table.DataSource = dataSource;
            PdfLayoutResult result = table.Draw(page, new PointF(0, y));
            y = y + result.Bounds.Height + 5;
            PdfBrush brush2 = PdfBrushes.Gray;
            PdfTrueTypeFont font2 = new PdfTrueTypeFont(new Font("Arial", 9f));
            page.Canvas.DrawString(String.Format("* {0} countries in the list.", data.Length - 1), font2, brush2, 5, y);
[VB.NET]
Dim table As New PdfTable()
table.Style.CellPadding = 2
table.Style.HeaderSource = PdfHeaderSource.Rows
table.Style.HeaderRowCount = 1
table.Style.ShowHeader = True
table.DataSource = dataSource
Dim result As PdfLayoutResult = table.Draw(page, New PointF(0, y))
y = y + result.Bounds.Height + 5
Dim brush2 As PdfBrush = PdfBrushes.Gray
Dim font2 As New PdfTrueTypeFont(New Font("Arial", 9F))
page.Canvas.DrawString([String].Format("* {0} countries in the list.", data.Length - 1), font2, brush2, 5, y)

Step 6: Save and preview

[C#]
            //Save pdf file.
            doc.SaveToFile("SimpleTable.pdf");
            doc.Close();
            System.Diagnostics.Process.Start("SimpleTable.pdf");
[VB.NET]
'Save pdf file.
doc.SaveToFile("SimpleTable.pdf")
doc.Close()
System.Diagnostics.Process.Start("SimpleTable.pdf")

Now Press F5 button to generate PDF table in C#.NET. Below is an effect screenshot.

Effective Screeshot:

Generate PDF Table