Beispiel #1
0
 /*
  * (non-Javadoc)
  *
  * @see com.afunms.report.ExportInterface#insertTable(java.util.ArrayList)
  */
 public void insertTable(ArrayList<String[]> tableal) throws Exception {
   // step 3: we open the document
   if (!document.isOpen()) {
     document.open();
   }
   Table pdfTable = new Table(tableal.get(0).length);
   for (int k = 0; k < tableal.size(); k++) {
     String[] row = tableal.get(k);
     for (int j = 0; j < row.length; j++) {
       Cell pdfcell = new Cell();
       if (k == 0) {
         pdfcell.addElement(new Paragraph(row[j], FontChineseTitle));
         pdfcell.setBackgroundColor(Color.gray);
         pdfTable.endHeaders();
       } else {
         pdfcell.addElement(new Paragraph(row[j], FontChineseRow));
         if (k % 2 == 0) {
           pdfcell.setBackgroundColor(Color.LIGHT_GRAY);
         }
       }
       // 合并单元格
       // pdfcell.setColspan(1);
       // pdfcell.setRowspan(1);
       // 对齐方式
       pdfcell.setHorizontalAlignment(Element.ALIGN_CENTER);
       pdfcell.setVerticalAlignment(Element.ALIGN_MIDDLE);
       pdfTable.addCell(pdfcell);
     }
   }
   pdfTable.setWidth(100);
   // 设置表格填距
   pdfTable.setPadding(5);
   pdfTable.setAlignment(Element.ALIGN_CENTER);
   // pdfTable.setTableFitsPage(true);
   document.add(pdfTable);
   // step 5: we close the document
 }
Beispiel #2
0
  /**
   * Shows how a table is split if it doesn't fit the page.
   *
   * @param args no arguments needed
   */
  public static void main(String[] args) {
    System.out.println("table splitting");
    // creation of the document with a certain size and certain margins
    Document document = new Document(PageSize.A4.rotate(), 50, 50, 50, 50);

    try {
      // creation of the different writers
      PdfWriter.getInstance(document, new FileOutputStream("repeatingtable.pdf"));

      // we add some meta information to the document
      document.addAuthor("Alan Soukup");
      document.addSubject("This is the result of a Test.");

      document.open();

      Table datatable = new Table(10);

      int headerwidths[] = {10, 24, 12, 12, 7, 7, 7, 7, 7, 7};
      datatable.setWidths(headerwidths);
      datatable.setWidth(100);
      datatable.setPadding(3);

      // the first cell spans 10 columns
      Cell cell =
          new Cell(
              new Phrase(
                  "Administration -System Users Report",
                  FontFactory.getFont(FontFactory.HELVETICA, 24, Font.BOLD)));
      cell.setHorizontalAlignment(Element.ALIGN_CENTER);
      cell.setLeading(30);
      cell.setColspan(10);
      cell.setBorder(Rectangle.NO_BORDER);
      cell.setBackgroundColor(new Color(0xC0, 0xC0, 0xC0));
      datatable.addCell(cell);

      // These cells span 2 rows
      datatable.getDefaultCell().setBorderWidth(2);
      datatable.getDefaultCell().setHorizontalAlignment(1);
      datatable.addCell("User Id");
      datatable.addCell("Name\nAddress");
      datatable.addCell("Company");
      datatable.addCell("Department");
      datatable.addCell("Admin");
      datatable.addCell("Data");
      datatable.addCell("Expl");
      datatable.addCell("Prod");
      datatable.addCell("Proj");
      datatable.addCell("Online");

      // this is the end of the table header
      datatable.endHeaders();

      datatable.getDefaultCell().setBorderWidth(1);

      for (int i = 1; i < 30; i++) {

        datatable.getDefaultCell().setHorizontalAlignment(Element.ALIGN_LEFT);

        datatable.addCell("myUserId");
        datatable.addCell(
            "Somebody with a very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very long long name");
        datatable.addCell("No Name Company");
        datatable.addCell("D" + i);

        datatable.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER);
        datatable.addCell("No");
        datatable.addCell("Yes");
        datatable.addCell("No");
        datatable.addCell("Yes");
        datatable.addCell("No");
        datatable.addCell("Yes");
      }
      document.add(new Paragraph("com.lowagie.text.Table - Cells split"));
      document.add(datatable);
      document.newPage();
      document.add(new Paragraph("com.lowagie.text.pdf.PdfPTable - Cells split\n\n"));
      datatable.setConvert2pdfptable(true);
      document.add(datatable);
      document.newPage();
      document.add(new Paragraph("com.lowagie.text.Table - Cells kept together"));
      datatable.setConvert2pdfptable(false);
      datatable.setCellsFitPage(true);
      document.add(datatable);
      document.newPage();
      document.add(new Paragraph("com.lowagie.text.pdf.PdfPTable - Cells kept together\n\n"));
      datatable.setConvert2pdfptable(true);
      document.add(datatable);
    } catch (Exception e) {
      e.printStackTrace();
    }

    // we close the document
    document.close();
  }
  @Override
  protected Table indexPdfDoc(
      Map<String, String> params, Font titleFont, Font minFont, Font haderFont, Font contextFont)
      throws DocumentException {
    List<Map<String, Object>> list = service.financialIndexData_02(params);
    Table table =
        ITextUtil.loadTable(
            7, new int[] {16, 14, 14, 14, 14, 14, 14}, 95, Element.ALIGN_CENTER, true, 1);

    // 设置表头
    table.addCell(
        ITextUtil.loadHeaderCell(
            params.get("statMonth") + getReportName(),
            titleFont,
            7,
            1,
            new float[] {0, 0, 0, 0},
            null,
            null));

    table.addCell(
        ITextUtil.loadCell(
            "表号:002   \n单位:万元   ",
            minFont,
            true,
            Element.ALIGN_RIGHT,
            Element.ALIGN_MIDDLE,
            new float[] {0, 0, 0, 0},
            null,
            null,
            7,
            1));

    table.addCell(ITextUtil.loadHeaderCell("地区", haderFont, 1, 2));

    table.addCell(ITextUtil.loadHeaderCell("利润总额", haderFont, 2, 1));
    table.addCell(ITextUtil.loadHeaderCell("资产总额", haderFont, 2, 1));
    table.addCell(ITextUtil.loadHeaderCell("所有者权益", haderFont, 2, 1));

    table.addCell(ITextUtil.loadHeaderCell("本期", haderFont, 1, 1));
    table.addCell(ITextUtil.loadHeaderCell("同比(%)", haderFont, 1, 1));
    table.addCell(ITextUtil.loadHeaderCell("本期", haderFont, 1, 1));
    table.addCell(ITextUtil.loadHeaderCell("同比(%)", haderFont, 1, 1));
    table.addCell(ITextUtil.loadHeaderCell("本期", haderFont, 1, 1));
    table.addCell(ITextUtil.loadHeaderCell("同比(%)", haderFont, 1, 1));

    table.endHeaders();

    // 计算
    if (list != null && list.size() > 0) {
      for (Map<String, Object> map : list) {
        table.addCell(ITextUtil.loadContextCell(map.get("ENT_NAME").toString(), contextFont));
        table.addCell(
            ITextUtil.loadContextCell(StringUitl.obj2doule(map.get("TJ1"), 2) + "", contextFont));
        table.addCell(
            ITextUtil.loadContextCell(StringUitl.obj2doule(map.get("TB1"), 2) + "", contextFont));
        table.addCell(
            ITextUtil.loadContextCell(StringUitl.obj2doule(map.get("TJ2"), 2) + "", contextFont));
        table.addCell(
            ITextUtil.loadContextCell(StringUitl.obj2doule(map.get("TB2"), 2) + "", contextFont));
        table.addCell(
            ITextUtil.loadContextCell(StringUitl.obj2doule(map.get("TJ3"), 2) + "", contextFont));
        table.addCell(
            ITextUtil.loadContextCell(StringUitl.obj2doule(map.get("TB3"), 2) + "", contextFont));
      }
    }
    return table;
  }