C# – Insert Data Table in PDF

As MS Word, PDF document can be written in various contents, for example, texts and images. Sometimes, we may insert data tables in PDF for showing some necessary information to readers. In this post, I will introduce a method about how to insert table in PDF.

Firstly, I prepare some data information in MS Access. I need to get data in VIP table and present it in my new PDF document. Then, add necessary references. Because I use Spire.PDF to help me realize this function, I need to add its dll file as reference. Then, add system.drawing for setting format.

After adding references, we can go to next steps.

  1. Create a blank PDF document.
  2. Set margin and then add a page for drawing.
  3. Draw page title and set format and location for it.
  4. Set table format, including borders, cell styles.
  5. Connect database and get needed data.
  6. Adjust column width to make the appearance be more wonderful.
  7. Save and launch file.


using System;

using System.Data;

using System.Data.OleDb;

using System.Drawing;

using Spire.Pdf;

using Spire.Pdf.Graphics;

using Spire.Pdf.Tables;

namespace InsertTable


    class table


        static void Main(string[] args)


            //Create a pdf document.PDF

            PdfDocument doc = new PdfDocument();

            //Set Margin and Add Page

            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;

            PdfPageBase page = doc.Pages.Add(PdfPageSize.A4, margin);

            float y = 10;


            PdfBrush brush1 = PdfBrushes.Black;

            PdfTrueTypeFont font1 = new PdfTrueTypeFont(new Font(“Calibri”, 12f, FontStyle.Regular));

            PdfStringFormat format1 = new PdfStringFormat(PdfTextAlignment.Center);

            page.Canvas.DrawString(“VIP Customers”, font1, brush1, page.Canvas.ClientSize.Width / 2, y, format1);

            y = y + font1.MeasureString(“VIP Customers”, format1).Height;

            y = y + 5;

            //Data Table Format

            PdfTable table = new PdfTable();

            table.Style.CellPadding = 1;

            table.Style.BorderPen = new PdfPen(brush1, 0.35f);

            table.Style.DefaultStyle.BackgroundBrush = PdfBrushes.WhiteSmoke;

            table.Style.DefaultStyle.Font = new PdfTrueTypeFont(new Font(“Calibri”, 10f));

            table.Style.HeaderSource = PdfHeaderSource.ColumnCaptions;

            table.Style.HeaderStyle.BackgroundBrush = PdfBrushes.LightGreen;

            table.Style.HeaderStyle.Font = new PdfTrueTypeFont(new Font(“Calibri”, 11f, FontStyle.Regular));

            table.Style.HeaderStyle.StringFormat = new PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle);

            table.Style.ShowHeader = true;

            //Get Data from Database

            using (OleDbConnection conn = new OleDbConnection())


                conn.ConnectionString = @”Provider=Microsoft.ACE.OLEDB.12.0;

                                Data Source=D:\work\My Documents\VIP.mdb;Persist Security Info=False;”;

                OleDbCommand command = new OleDbCommand();


                    = “select Name,Gender,Birthday,Email,Number,Country from VIP”;

                command.Connection = conn;

                using (OleDbDataAdapter dataAdapter = new OleDbDataAdapter(command))


                    DataTable dataTable = new DataTable();


                    table.DataSourceType = PdfTableDataSourceType.TableDirect;

                    table.DataSource = dataTable;



            //Set Column Width

            float width

                = page.Canvas.ClientSize.Width

                    – (table.Columns.Count + 1) * table.Style.BorderPen.Width;

            table.Columns[0].Width = width * 0.17f * width;


                = new PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle);

            table.Columns[1].Width = width * 0.12f * width;


                = new PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle);

            table.Columns[2].Width = width * 0.16f * width;


                = new PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle);

            table.Columns[3].Width = width * 0.32f * width;


                = new PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle);

            table.Columns[4].Width = width * 0.14f * width;


                = new PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle);

            table.Columns[5].Width = width * 0.19f * width;


                = new PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle);

            PdfLayoutResult result = table.Draw(page, new PointF(0, y));

            y = y + result.Bounds.Height + 5;

            //Save and Launch File









Click Here to learn more about Spire.PDF

Click Here to download Spire.PDF