我正在使用 iText PDF 庫從資料庫生成 PDF 檔案。現在我必須在相同日期的基礎上以兩種顏色(白色和黃色)顯示 PDF 表格的替代行。
這是我的代碼:
PdfPTable table = new PdfPTable(10);
table.setTotalWidth(100);
table.setWidthPercentage(100);
while (rs.next()) {
table.addCell(rs.getString("date"));
table.addCell(rs.getString("destination"));
}
我已經添加了一個例子來清楚地理解

uj5u.com熱心網友回復:
如果您有兩列,則應在PdfTable建構式中指定它。然后不只是玩這些值:
PdfPTable table = new PdfPTable(2);
table.setTotalWidth(100);
table.setWidthPercentage(100);
BaseColor color1 = WebColors.GetRGBColor("#FFFFFF");
BaseColor color2 = WebColors.GetRGBColor("#00FFFF");
String lastDateValue = "";
BaseColor lastColor = color2;
while (rs.next()) {
String currentDateValue = rs.getString("date"));
BaseColor color;
if ( lastDateValue.equals(currentDateValue)){
color = lastColor == color1 ? color1 : color2;
} else {
color = lastColor == color1 ? color2 : color1;
}
PdfPCell cell1 = new PdfPCell(new Phrase(currentDateValue));
PdfPCell cell2 = new PdfPCell(new Phrase(rs.getString("destination")));
cell1.setBackgroundColor(color);
cell2.setBackgroundColor(color);
table.addCell(cell1);
table.addCell(cell2);
lastDateValue = currentDateValue;
lastColor = color
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/386305.html
