コード例 #1
0
  protected void insertCell(
      PdfPTable table, String text, int align, int colspan, Font font, int border) {
    PdfPCell cell = new PdfPCell(new Phrase(text.trim(), font));
    cell.setHorizontalAlignment(align);
    cell.setColspan(colspan);
    cell.setBorder(border);
    cell.setBorderColor(borderColor);
    cell.setBackgroundColor(backgroundColor);

    if (text.trim().equalsIgnoreCase("")) {
      cell.setMinimumHeight(minimumCellHeight);
    }

    table.addCell(cell);
  }
  private void addTable(
      JSONArray store,
      String[] res,
      String[] colIndex,
      String[] colHeader,
      String[] mainHeader,
      String[] val,
      Document document)
      throws JSONException, DocumentException {

    java.awt.Color tColor = new Color(0, 0, 0);
    fontSmallBold.setColor(tColor);
    Font f1 = FontFactory.getFont("Helvetica", 8, Font.NORMAL, tColor);

    float[] colw = new float[4];
    for (int x = 0; x < 4; x++) {
      colw[x] = 150;
    }
    int col = 0;

    for (int x = 0; x < mainHeader.length; x++) {
      // table start
      PdfPTable table = new PdfPTable(colw);
      table.setTotalWidth(88);
      table.setWidthPercentage(colw, document.getPageSize());
      // table.setSpacingBefore(10);

      PdfPTable mainTable = new PdfPTable(1);
      mainTable.setTotalWidth(90);
      mainTable.setWidthPercentage(100);
      mainTable.setSpacingBefore(20);

      // header cell for mainTable
      PdfPCell headcell = null;
      headcell = new PdfPCell(new Paragraph(mainHeader[x], fontSmallBold));
      headcell.setBackgroundColor(new Color(0xEEEEEE));
      headcell.setPadding(padding);
      mainTable.addCell(headcell);
      document.add(mainTable);

      // header cell added to mainTable
      int row = 3;
      if (x == 0 || x == 4) {
        row = 4;
      } else if (x == 5) {
        row = 0;
      }
      for (; row > 0; row--) {
        for (int y = 1;
            y < colw.length + 1;
            y++) { // for each column add the colHeader and value cell
          if (col != colHeader.length) {
            if (y % 2 != 0) {
              Paragraph p = new Paragraph(colHeader[col], f1);
              if (colHeader[col].contains("Variance")) {
                p = new Paragraph(colHeader[col], fontSmallBold);
              }
              PdfPCell pcell = new PdfPCell(p);
              if (gridBorder) {
                pcell.setBorder(PdfPCell.BOTTOM | PdfPCell.LEFT | PdfPCell.RIGHT | PdfPCell.TOP);
              } else {
                pcell.setBorder(0);
              }
              pcell.setPadding(padding);
              pcell.setBorderColor(new Color(0xF2F2F2));
              pcell.setHorizontalAlignment(Element.ALIGN_CENTER);
              pcell.setVerticalAlignment(Element.ALIGN_CENTER);
              table.addCell(pcell);
            } else {
              Paragraph p;
              p = new Paragraph(val[col], f1);
              if (colHeader[col].contains("Start Variance")
                  || colHeader[col].contains("End Variance")) {
                p = new Paragraph(val[col] + " days", fontSmallBold);
              } else if (colHeader[col].contains("Duration")) {
                if (colHeader[col].contains("Duration Variance"))
                  p = new Paragraph(val[col] + " days", fontSmallBold);
                else p = new Paragraph(val[col] + " days", f1);
              } else if (colHeader[col].contains("Work")) {
                if (colHeader[col].contains("Work Variance"))
                  p = new Paragraph(val[col] + " hrs", fontSmallBold);
                else p = new Paragraph(val[col] + " hrs", f1);
              } else if (colHeader[col].contains("Cost")) {
                if (colHeader[col].contains("Cost Variance"))
                  p = new Paragraph(currSymbol + " " + val[col], fontSmallBold);
                else p = new Paragraph(currSymbol + " " + val[col], f1);
              } else if (colHeader[col].contains("Percent Complete")) {
                p = new Paragraph(val[col] + " %", f1);
              }
              PdfPCell pcell = new PdfPCell(p);
              if (gridBorder) {
                pcell.setBorder(PdfPCell.BOTTOM | PdfPCell.LEFT | PdfPCell.RIGHT | PdfPCell.TOP);
              } else {
                pcell.setBorder(0);
              }
              pcell.setPadding(padding);
              pcell.setBorderColor(new Color(0xF2F2F2));
              pcell.setHorizontalAlignment(Element.ALIGN_CENTER);
              pcell.setVerticalAlignment(Element.ALIGN_CENTER);
              table.addCell(pcell);
              col++;
            }
          }
        }
      }
      if (x == 5) {
        int y = 0;
        row = res.length / 4;
        for (; row > 0; row--) {
          for (int c = 0; c < 2; c++) {
            Paragraph p = new Paragraph(res[y], f1);
            PdfPCell pcell = new PdfPCell(p);
            if (gridBorder) {
              pcell.setBorder(PdfPCell.BOTTOM | PdfPCell.LEFT | PdfPCell.RIGHT | PdfPCell.TOP);
            } else {
              pcell.setBorder(0);
            }
            pcell.setPadding(padding);
            pcell.setBorderColor(new Color(0xF2F2F2));
            pcell.setHorizontalAlignment(Element.ALIGN_CENTER);
            pcell.setVerticalAlignment(Element.ALIGN_CENTER);
            table.addCell(pcell);
            p = new Paragraph(res[y + 1], f1);
            pcell = new PdfPCell(p);
            if (gridBorder) {
              pcell.setBorder(PdfPCell.BOTTOM | PdfPCell.LEFT | PdfPCell.RIGHT | PdfPCell.TOP);
            } else {
              pcell.setBorder(0);
            }
            pcell.setPadding(padding);
            pcell.setBorderColor(new Color(0xF2F2F2));
            pcell.setHorizontalAlignment(Element.ALIGN_CENTER);
            pcell.setVerticalAlignment(Element.ALIGN_CENTER);
            table.addCell(pcell);
            y += 2;
          }
        }
      }
      document.add(table);
    }
  }