2013-11-09 41 views
3

我正在從使用itext pdf庫的數據庫生成pdf文件。現在我需要我,我必須顯示不同顏色的pdf表,如斑馬顏色(灰色和白) 但我不知道該怎麼做......如何在java中設置itext pdf表alernative行顏色

這裏是我的代碼..

 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")); 
     } 

請幫助我。 在此先感謝。

回答

8
boolean b = true; 
for(PdfPRow r: table.getRows()) { 
    for(PdfPCell c: r.getCells()) { 
    c.setBackgroundColor(b ? BaseColor.GREY : BaseColor.WHITE); 
    } 
    b = !b; 
} 
+0

先生我需要交替行,但在這裏它是交替列 – Adi

+0

@Adi我修好了。 –