Set the Border Color of Table in PDF in C#

2016-05-27 09:13:15 Written by  support iceblue
Rate this item
(0 votes)

In order to format a table in PDF, Spire.PDF provides a PdfTableStyle class that represents parameters of PDF light table and a PdfCellStyle class that represents information about cell style. This article will introduce how to set the border color of a table including table border and cell border by using the two classes mentioned above.

Code Snippets:

Step 1: Define a multidimensional array of string.

string[][] dataSource =new string[5][] {new string[] {"Name", "Capital", "Continent", "Area", "Population"},
  new string[] {"Argentina","Buenos Aires", "South American", "2777815", "3230003"},
  new string[] {"Bolivia","La Paz","South America","1098575","7300000"},
  new string[] {"Brazil","Brasilia","South America","8511196","150400000"},
  new string[] {"Canada","Ottawa","North America","9976147","26500000"}
};

Step 2: Initialize a new instance of PdfDocument class and add a page to it.

PdfDocument pdf = new PdfDocument();
PdfPageBase page = pdf.Pages.Add();

Step 3: Initialize a new instance of PdfTbale class, filling the table with the data predefined.

PdfTable table = new PdfTable();
table.DataSource = dataSource;

Step 4: Initialize an instance of PdfTableStyle class, and set the color of table border as Blue, then apply the style to PdfTable.Style.

PdfTableStyle style = new PdfTableStyle();
style.BorderPen = new PdfPen(Color.Blue, 1f);
table.Style = style;

Step 5: The syntax to set style of cell border is much different from setting table border style, since PdfTable class doesn't contain a property of CellStyle. Instead, CellStyle is a property included in BeginRowLayoutEventArgs class, which is an argument of StratRowLayout event. Therefore we customize a method as below to set the color of cell border.

public static void table_BeginRowLayout(object sender, BeginRowLayoutEventArgs args)
{
    PdfCellStyle cellStyle = new PdfCellStyle();
    cellStyle.BorderPen = new PdfPen(Color.Red, 0.5f);
    args.CellStyle = cellStyle;
}

In the Main method, "table_BeginRowLayout" must be added to BeginRowLayout event to ensure the custom method will be invoked when the event occurs.

table.BeginRowLayout += new BeginRowLayoutEventHandler(table_BeginRowLayout);

Step 6: Draw the table on PDF page and save to file.

table.Draw(page, new PointF(0,40));
pdf.SaveToFile(@"SetBorderColor.pdf"); 

Output:

 

How to Set the Border Color of Table in PDF in C#

 

Full Code:

using Spire.Pdf;
using Spire.Pdf.Tables;
using Spire.Pdf.Graphics;
using System.Drawing;

namespace SetBorderColorOfTable
{
    class Program
    {
        static void Main(string[] args)
        {
            //input data
            string[][] dataSource =new string[5][] {new string[] {"Name", "Capital", "Continent", "Area", "Population"},
              new string[] {"Argentina","Buenos Aires", "South American", "2777815", "3230003"},
              new string[] {"Bolivia","La Paz","South America","1098575","7300000"},
              new string[] {"Brazil","Brasilia","South America","8511196","150400000"},
              new string[] {"Canada","Ottawa","North America","9976147","26500000"}
            };

            //initialize an instance of PdfDocument
            PdfDocument pdf = new PdfDocument();
            PdfPageBase page = pdf.Pages.Add();

            //initialize an instance of PdfTable
            PdfTable table = new PdfTable();            
            table.DataSource = dataSource;   
                     
            //set the color of table border
            PdfTableStyle style = new PdfTableStyle();
            style.BorderPen = new PdfPen(Color.Blue, 1f);
            table.Style = style;

            //add custom method to BeginRowLayout event
            table.BeginRowLayout += new BeginRowLayoutEventHandler(table_BeginRowLayout);

            //draw table on PDF and save file
            table.Draw(page, new PointF(0,40));
            pdf.SaveToFile(@"SetBorderColor.pdf");
            System.Diagnostics.Process.Start(@"SetBorderColor.pdf");
        }
        //customize a method to set color of cell border
        public static void table_BeginRowLayout(object sender, BeginRowLayoutEventArgs args)
        {
            PdfCellStyle cellStyle = new PdfCellStyle();
            cellStyle.BorderPen = new PdfPen(Color.Red, 0.5f);
            args.CellStyle = cellStyle;
        }
    }
}

Additional Info

  • tutorial_title: Set the Border Color of Table in PDF in C#
Last modified on Sunday, 26 September 2021 02:04