Exemple #1
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();
  }
  // utility function to make an item Element.
  private Table makeItemElement(ShoppingCartItemData pItm) throws DocumentException {

    Table itmTbl = new PTable(mColumnCount);
    itmTbl.setWidth(100);
    itmTbl.setWidths(itmColumnWidth);
    itmTbl.getDefaultCell().setBorderColor(java.awt.Color.black);
    itmTbl.getDefaultCell().setVerticalAlignment(Cell.ALIGN_TOP);
    itmTbl.setOffset(0);
    itmTbl.setBorder(Table.NO_BORDER);

    if (!catalogOnly) {
      String t0 = "";
      if (pItm.getIsaInventoryItem()) {
        t0 = "i";
      }

      if (null != mSiteData
          && mSiteData.isAnInventoryAutoOrderItem(pItm.getProduct().getProductId())) {
        t0 += "a";
      }

      Cell tpc0 = new Cell(makePhrase(t0, small, true));
      if (!pItm.getIsaInventoryItem()) {
        tpc0.setBorder(0);
      }
      itmTbl.addCell(tpc0);
    }

    Cell tpc01 = new Cell(makePhrase("", normal, true));
    itmTbl.addCell(tpc01);

    String t = "";
    if (pItm.getProduct().isPackProblemSku()) {
      t += "*";
    }
    if (t.length() > 0) t += " ";
    Cell tpc1 = new Cell(makePhrase(t + pItm.getActualSkuNum(), normal, true));
    itmTbl.addCell(tpc1);

    itmTbl.addCell(makePhrase(pItm.getProduct().getCatalogProductShortDesc(), normal, true));
    if (mShowSize) {
      itmTbl.addCell(makePhrase(pItm.getProduct().getSize(), normal, true));
    }
    // itmTbl.addCell(makePhrase(pItm.getProduct().getPack(), normal, true));
    // itmTbl.addCell(makePhrase(pItm.getProduct().getUom(), normal, true));
    // itmTbl.addCell(makePhrase(pItm.getProduct().getManufacturerName(),normal,true));

    if (mShowPrice) {
      BigDecimal price = new BigDecimal(pItm.getPrice());
      String priceStr = "";
      try {
        priceStr = mFormatter.priceFormatWithoutCurrency(price);
      } catch (Exception exc) {
        exc.printStackTrace();
      }
      Cell pcell = new Cell(makePhrase(priceStr, normal, true));
      pcell.setHorizontalAlignment(Element.ALIGN_RIGHT);
      itmTbl.addCell(pcell);
    }

    if (catalogOnly) {
      if (pItm.getProduct() != null
          && pItm.getProduct().getCatalogDistrMapping() != null
          && Utility.isTrue(pItm.getProduct().getCatalogDistrMapping().getStandardProductList())) {
        String yStr = ClwI18nUtil.getMessage(mRequest, "shoppingItems.text.y", null);
        itmTbl.addCell(makePhrase(yStr, normal, true));
      } else {
        String nStr = ClwI18nUtil.getMessage(mRequest, "shoppingItems.text.n", null);
        itmTbl.addCell(makePhrase(nStr, normal, true));
      }
    }

    if (!catalogOnly) {
      // BigDecimal amount = new BigDecimal(pItm.getAmount());
      itmTbl.addCell(makePhrase("", normal, true));
    }

    return itmTbl;
  }