How to Create Tables in PDF Documents

PDF Mosaic Library has ability for creation and manipulation of PDF tables. If you want to add tables in your PDF Reporting Software, you may use our pdf library. PDF Mosaic has useful API for creating table data in PDF documents.

The following code creates a table on a PDF document.

PDF Tables

C# :

using PDFMosaic;
using System.Drawing;

namespace Tables
{
  class Tables
  {
    static void Main()
    {
      PDFDocument document = new PDFDocument();
      document.Pages.Add(new PDFPage(PDFPaperFormat.A4));
      PDFCanvas canvas = document.Pages[0].Canvas;

      PDFTable table = new PDFTable();

      for (int i = 0; i < 10; ++i)
      {
        table.Columns.Add(new PDFTableColumn((i + 1).ToString(), (i + 1).ToString()));
      }
      table.BackgroundColor = new PDFColorGray(123);
      table.Columns[0].BackgroundColor = new PDFColorGray(123);

      for (int i = 0; i < 10; ++i)
      {
        PDFTableRow row = table.NewRow();
        for (int j = 0; j < 10; ++j)
          row[(j + 1).ToString()].Text = ((i + 1) * (j + 1)).ToString();
        row.BackgroundColor = new PDFColorGray(255);
        table.Rows.Add(row);
      }

      table.Rows[4][((int)(4 + 1)).ToString()].BackgroundColor = new PDFColorRGB(255, 0, 0);
      table.Columns[4].BackgroundColor = new PDFColorRGB(0, 255, 0);
      table.Rows[4].BackgroundColor = new PDFColorRGB(0, 255, 0);
      table.Rows[4][((int)(0 + 1)).ToString()].BackgroundColor = new PDFColorRGB(0, 255, 0);

      canvas.DrawTable(table, 20, 20);

      document.Save("Tables.pdf", true);
    }
  }
}


VB.NET :

Imports PDFMosaic
Imports System.Drawing

Module Tables
  Sub Main()
    Dim document As New PDFDocument()
    document.Pages.Add(New PDFPage(PDFPaperFormat.A4))
    Dim canvas As PDFCanvas = document.Pages(0).Canvas

    Dim table As New PDFTable()

    For i As Integer = 0 To 10
      table.Columns.Add(New PDFTableColumn((i + 1).ToString(), (i + 1).ToString()))
    Next

    table.BackgroundColor = New PDFColorGray(123)
    table.Columns(0).BackgroundColor = New PDFColorGray(123)

    For i As Integer = 0 To 10
      Dim row As PDFTableRow = table.NewRow()
      For j As Integer = 0 To 10
        row((j + 1).ToString()).Text = ((i + 1) * (j + 1)).ToString()
      Next
      row.BackgroundColor = New PDFColorGray(255)
      table.Rows.Add(row)
    Next

    table.Rows(4)(5.ToString()).BackgroundColor = New PDFColorRGB(255, 0, 0)
    table.Columns(4).BackgroundColor = New PDFColorRGB(0, 255, 0)
    table.Rows(4).BackgroundColor = New PDFColorRGB(0, 255, 0)
    table.Rows(4)(1.ToString()).BackgroundColor = New PDFColorRGB(0, 255, 0)

    canvas.DrawTable(table, 20, 20)

    document.Save("Tables.pdf", True)
  End Sub
End Module