Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set itext pdf table alernative rows colour in java

Tags:

java

pdf

itext

I am generating pdf file from database using itext pdf library.Now my need i that i have to show alternative rows of pdf table in different colour just like zebra colour (grey and white) but i dont know how to do that...

Here is my code..

        PdfPTable table = new PdfPTable(10);
        table.setTotalWidth(100);
        table.setWidthPercentage(100);
        while (rs.next()) {
            table.addCell(rs.getString("date"));
            table.addCell(rs.getString("time"));
            table.addCell(rs.getString("source"));
            table.addCell(rs.getString("destination"));
            table.addCell(rs.getString("extension"));
         }

Please help me. Thanks in advance.

like image 369
Adi Avatar asked Nov 09 '13 08:11

Adi


1 Answers

boolean b = true;
for(PdfPRow r: table.getRows()) {
  for(PdfPCell c: r.getCells()) {
    c.setBackgroundColor(b ? BaseColor.GREY : BaseColor.WHITE);
  }
  b = !b;
}
like image 87
Robin Green Avatar answered Nov 19 '22 00:11

Robin Green