How to create tables using ByteScout PDF SDK - ByteScout

How to create tables using ByteScout PDF SDK

  • Home
  • /
  • Articles
  • /
  • How to create tables using ByteScout PDF SDK

This example demonstrates how to create tables using ByteScout PDF SDK.

Program.vb:
VB

'*******************************************************************
'       ByteScout PDF SDK
'                                                                   
'       Copyright © 2016 Bytescout, https://bytescout.com        
'       ALL RIGHTS RESERVED                                         
'                                                                   
'*******************************************************************

Imports Bytescout.PDF

''' <summary>
''' This example demonstrates how to create tables.
''' </summary>
Class Program

    Shared Sub Main()

        ' Create new document
        Dim pdfDocument = New Document()
        pdfDocument.RegistrationName = "demo"
		pdfDocument.RegistrationKey = "demo"
        ' Add page
        Dim page = New Page(PaperFormat.A4)
        pdfDocument.Pages.Add(page)


        Dim lightGrayColor = New ColorGray(200)
        Dim whiteColor = New ColorGray(255)
        Dim lightBlueColor = New ColorRGB(200, 200, 250)
        Dim lightRedColor = New ColorRGB(255, 200, 200)

        ' Create a table and set default background color
        Dim table = New Table()
        table.BackgroundColor = lightGrayColor

        ' Add row headers column and set its color
        table.Columns.Add(New TableColumn("RowHeaders"))
        table.Columns(0).BackgroundColor = lightGrayColor

        ' Add columns A, B, C, ...
        For c As Integer = 0 To 9
            Dim columnName = Convert.ToChar(65 + c).ToString()
            table.Columns.Add(New TableColumn(columnName, columnName))
        Next

        ' Add rows
        For r As Integer = 0 To 9
            ' Create new row and set its background color
            Dim row = table.NewRow()
            row.BackgroundColor = whiteColor

            ' Set row header text
            row("RowHeaders").Text = (r + 1).ToString()

            ' Set cell text
            For c As Integer = 0 To 9
                Dim columnName As String = Convert.ToChar(65 + c).ToString()
                row(columnName).Text = columnName + (r + 1).ToString()
            Next

            ' Add the row to the table
            table.Rows.Add(row)
        Next

        ' Decorate the table
        table.Rows(1)("B").BackgroundColor = lightRedColor
        table.Columns(2).BackgroundColor = lightBlueColor
        table.Rows(1).BackgroundColor = lightBlueColor
        table.Rows(1)("RowHeaders").BackgroundColor = lightBlueColor

        ' Draw the table on canvas
        page.Canvas.DrawTable(table, 20, 20)

        
        ' Save document to file
        pdfDocument.Save("result.pdf")

        ' Cleanup 
        pdfDocument.Dispose()

        ' Open document in default PDF viewer app
        Process.Start("result.pdf")

    End Sub

End Class 

Program.cs:
C#

//*******************************************************************
//       ByteScout PDF SDK		                                     
//                                                                   
//       Copyright © 2016 ByteScout - https://bytescout.com       
//       ALL RIGHTS RESERVED                                         
//                                                                   
//*******************************************************************

using System;
using System.Diagnostics;
using Bytescout.PDF;

namespace Tables
{
	/// <summary>
	/// This example demonstrates how to create tables.
	/// </summary>
	class Program
	{
		static void Main()
		{
			// Create new document
			Document pdfDocument = new Document();
			pdfDocument.RegistrationName = "demo";
			pdfDocument.RegistrationKey = "demo";
			// Add page
			Page page = new Page(PaperFormat.A4);
			pdfDocument.Pages.Add(page);


			DeviceColor lightGrayColor = new ColorGray(200);
			DeviceColor whiteColor = new ColorGray(255);
			DeviceColor lightBlueColor = new ColorRGB(200, 200, 250);
			DeviceColor lightRedColor = new ColorRGB(255, 200, 200);

			// Create a table and set default background color
			Table table = new Table();
			table.BackgroundColor = lightGrayColor;

			// Add row headers column and set its color
			table.Columns.Add(new TableColumn("RowHeaders"));
			table.Columns[0].BackgroundColor = lightGrayColor;

			// Add columns A, B, C, ...
			for (int c = 0; c < 10; c++)
			{
				string columnName = Convert.ToChar('A' + c).ToString();
				table.Columns.Add(new TableColumn(columnName, columnName));
			}

			// Add rows
			for (int r = 0; r < 10; r++)
			{
				// Create new row and set its background color
				TableRow row = table.NewRow();
				row.BackgroundColor = whiteColor;
				
				// Set row header text
				row["RowHeaders"].Text = (r + 1).ToString();

				// Set cell text
				for (int c = 0; c < 10; c++)
				{
					string columnName = Convert.ToChar('A' + c).ToString();
					row[columnName].Text = columnName + (r + 1);
				}

				// Add the row to the table
				table.Rows.Add(row);
			}

			// Decorate the table
			table.Rows[1]["B"].BackgroundColor = lightRedColor;
			table.Columns[2].BackgroundColor = lightBlueColor;
			table.Rows[1].BackgroundColor = lightBlueColor;
			table.Rows[1]["RowHeaders"].BackgroundColor = lightBlueColor;

			// Draw the table on canvas
			page.Canvas.DrawTable(table, 20, 20);

			
			// Save document to file
			pdfDocument.Save("result.pdf");

			// Cleanup 
			pdfDocument.Dispose();

			// Open document in default PDF viewer app
			Process.Start("result.pdf");
		}
	}
} 

Tutorials:

prev
next