【问题标题】:iText PDF alternative row color on the basis of same date基于相同日期的 iText PDF 替代行颜色
【发布时间】:2022-01-21 06:11:54
【问题描述】:

我正在使用 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"));
}

我添加了一个示例以便清楚理解

【问题讨论】:

    标签: java pdf itext


    【解决方案1】:

    如果您有两列,则应在 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
         }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-11-21
      • 2016-07-09
      • 2020-11-27
      • 1970-01-01
      • 1970-01-01
      • 2015-10-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多