Semil Sebastian
Semil Sebastian

Reputation: 519

How to set the first row of table colorful?

I have a table and I want to make its header row as gray color. I have tried the below methode but it gives the whole table as gray color

 PdfPTable table1 = new PdfPTable(4);
 table1.SetTotalWidth(new float[] { 50f,80f,50f,330f });
 table1.TotalWidth = 800f;//table size
 table1.LockedWidth = true;

 table1.HorizontalAlignment = 0;
 table1.SpacingBefore = 5f;//both are used to mention the space from heading
 table1.SpacingAfter = 5f;
 table1.DefaultCell.BackgroundColor = BaseColor.LIGHT_GRAY;
 table1.DefaultCell.HorizontalAlignment = Element.ALIGN_CENTER;
 table1.AddCell(new Phrase("NO", time515));
 table1.DefaultCell.HorizontalAlignment = Element.ALIGN_CENTER;
 table1.AddCell(new Phrase("Date & Day", time515));
 table1.DefaultCell.HorizontalAlignment = Element.ALIGN_CENTER;
 table1.AddCell(new Phrase("Hr", time515));
 table1.DefaultCell.HorizontalAlignment = Element.ALIGN_CENTER;
 table1.AddCell(new Phrase("Topics to be Covered", time515));
 Doc.add(table1);

Kindly see my expected output image enter image description here

Upvotes: 1

Views: 1279

Answers (1)

Chris Haas
Chris Haas

Reputation: 55457

You have a couple of options. If you just want to change the background color for a given row when needed you can just do that. The code below will write ten rows of four cells, the first row is gray and the remaining rows are blue.

for (var i = 0; i < 10; i++) {
    if (i == 0) {
        table1.DefaultCell.BackgroundColor = BaseColor.LIGHT_GRAY;
    }
    else {
        table1.DefaultCell.BackgroundColor = BaseColor.BLUE;
    }
    table1.AddCell(new Phrase("NO"));
    table1.AddCell(new Phrase("Date & Day"));
    table1.AddCell(new Phrase("Hr"));
    table1.AddCell(new Phrase("Topics to be Covered"));
}

However, tables in iText actually support a more concrete idea of "Headers" and you declare these simply by telling the PdfPTable how many rows should be considered the "header":

table1.HeaderRows = 1;

What's neat about this is that if your table actually spans multiple pages then your header will automatically be re-drawn on the next page. However, using this method you also need to perform a little extra work. iText exposes and interface for you to implement called IPdfPTableEvent that has a single method called TableLayout. Below is a full working example of an implementation, see the code comments for more details:

private class MyTableEvent : IPdfPTableEvent {

    public void TableLayout(PdfPTable table, float[][] widths, float[] heights, int headerRows, int rowStart, PdfContentByte[] canvases) {

        //Loop through each header row
        for( var row = 0; row < headerRows; row++ ){

            //Loop through each column in the current row
            //NOTE: For n columns there's actually n+1 entries in the widths array.
            for( var col = 0; col < widths[row].Length - 1; col++ ){

                //Get the various coordinates
                var llx = widths[row][col];
                var lly = heights[row];
                var urx = widths[row][col + 1];
                var ury = heights[row + 1];

                //Create a rectangle
                var rect = new iTextSharp.text.Rectangle(llx, lly, urx, ury);

                //Set whatever properties you want on it
                rect.BackgroundColor = BaseColor.PINK;

                //Draw it to the background canvas
                canvases[PdfPTable.BACKGROUNDCANVAS].Rectangle(rect);
            }
        }

    }
}

To use it you just bind an instance to your table object:

table1.TableEvent = new MyTableEvent();

Upvotes: 2

Related Questions