In the previous page, you learnt to read data from Institution table of testdb database in SQL Server by using LINQ and display the data in the table layout. In this page, you will learn to customize the background color of rows of the table. For example, suppose you want to add the pink background color to every odd row of the table as shown below.
To achieve this goal, for the best way, you need to implement the IPdfPTableEvent interface. This interface has one method called TableLayout that allows you to customize the background color of the table.
As you see in the example code below, the TableLayout method has six parameters:
-table--The table object to which the background color of rows will be added.
-widths--A two-dimensional array that stores the x positions of the column borders of the table. For example, if a table has two columns without colspan, the values stored in the widths array might look similar to the one shown below:
-heights--A one-dimensional array that stores the y positions of rows of the table.
-headerRows--An int value representing the number of rows defined as header of the table. This number can also be obtained by writing the table.readerRows.
-rowStart--An int value representing the index of starting row of the table. Generally, rowStart is 0.
class CustomBackground : IPdfPTableEvent
public void TableLayout(PdfPTable table,float widths, float heights, int headerRows, int rowStart, PdfContentByte canvases)
int footer = widths.Count() - table.FooterRows;
int header = table.HeaderRows - table.FooterRows+1;
for (int row = header; row < footer; row +=2)
columns = widths[row].Count() - 1;
rect = new Rectangle(widths[row], heights[row], widths[row][columns], heights[row + 1]);
rect.Border = Rectangle.NO_BORDER;
Now you have the CustomBackground class that implements the IPdfPTableEvent interface. Next, you need to create an object of CustomBackground class and assign it to the TableEvent property. See the example code below.
CustomBackground cb = new CustomBackground();
table.TableEvent = cb;
You can download the complete C# code of this tutorial from here.
This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages: