【问题标题】:iText Java Table cellspacingiText Java 表格单元格间距
【发布时间】:2014-05-10 06:51:16
【问题描述】:

我正在使用 java 库 iText 来生成 pdf。问题是:如何在像this 这样的 itext 表中定义单元格间距。

提前感谢您!

P.S.:这是我的代码的一部分:

private static PdfPTable createFirstTable() throws DocumentException, IOException {
    PdfPTable table = new PdfPTable(13);

     BaseFont baseFont = BaseFont.createFont("times.ttf", BaseFont.IDENTITY_H, true);
     Font deckFont = new Font(baseFont, 12f);
     deckFont.setColor(BaseColor.RED); 
     table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER);
     table.getDefaultCell().setVerticalAlignment(Element.ALIGN_MIDDLE);
     table.getDefaultCell().setPaddingBottom(15);
     table.getDefaultCell().setPaddingTop(15);

     Font deckFont1 = new Font(baseFont, 12f);
     deckFont1.setColor(BaseColor.BLACK); 
    for (int i = 0; i < 4; i++) {
        switch (i) {
        case 0:

            for (int j = 0; j < 13; j++) {
                switch (j) {
                case 0:
                case 1:
                case 2:
                case 3:
                case 4:
                case 5:
                case 6:
                case 7:


                    table.addCell(new Paragraph(String.valueOf(j+2+"\u2666"),deckFont));
                    break;
                case 8:
                    table.addCell(new Paragraph(String.valueOf(j+2+"\u2666"),deckFont));
                    break;
                case 9:
                    table.addCell(new Paragraph(String.valueOf("J"+"\u2666"),deckFont));
                    break;
                case 10:
                    table.addCell(new Paragraph(String.valueOf("D"+"\u2666"),deckFont));
                    break;
                case 11:
                    table.addCell(new Paragraph(String.valueOf("K"+"\u2666"),deckFont));
                    break;
                case 12:
                    table.addCell(new Paragraph(String.valueOf("A"+"\u2666"),deckFont));
                    break;
                default:
                    table.addCell("Error case");
                    ;
                    break;
                }
            }
            table.completeRow();
            break;

        case 1:
            for (int j = 0; j < 13; j++) {
                switch (j) {
                case 0:
                case 1:
                case 2:
                case 3:
                case 4:
                case 5:
                case 6:
                case 7:
                    table.addCell(new Paragraph(String.valueOf(j+2+"\u2660"),deckFont1));
                    break;
                case 8:
                    table.addCell(new Paragraph(String.valueOf(j+2+"\u2660"),deckFont1));
                    break;
                case 9:
                    table.addCell(new Paragraph(String.valueOf("J"+"\u2660"),deckFont1));
                    break;
                case 10:
                    table.addCell(new Paragraph(String.valueOf("D"+"\u2660"),deckFont1));
                    break;
                case 11:
                    table.addCell(new Paragraph(String.valueOf("K"+"\u2660"),deckFont1));
                    break;
                case 12:
                    table.addCell(new Paragraph(String.valueOf("A"+"\u2660"),deckFont1));
                    break;
                default:
                    System.out.println("Error case");
                    break;
                }
            }

【问题讨论】:

    标签: java itext cellspacing


    【解决方案1】:

    查看官方文档:

    http://itextpdf.com/examples/iia.php?id=95

    /**
     * Creates a table that mimics cellspacing using table and cell events.
     * @param connection
     * @return a table
     * @throws SQLException
     * @throws DocumentException
     * @throws IOException
     */
    public PdfPTable getTable(DatabaseConnection connection)
        throws SQLException, DocumentException, IOException {
        PdfPTable table = new PdfPTable(new float[] { 1, 2, 2, 5, 1 });
        table.setTableEvent(new PressPreviews());
        table.setWidthPercentage(100f);
        table.getDefaultCell().setPadding(5);
        table.getDefaultCell().setBorder(PdfPCell.NO_BORDER);
        table.getDefaultCell().setCellEvent(new PressPreviews());
        for (int i = 0; i < 2; i++) {
            table.addCell("Location");
            table.addCell("Date/Time");
            table.addCell("Run Length");
            table.addCell("Title");
            table.addCell("Year");
        }
        table.getDefaultCell().setBackgroundColor(null);
        table.setHeaderRows(2);
        table.setFooterRows(1);
        List<Screening> screenings = PojoFactory.getPressPreviews(connection);
        Movie movie;
        for (Screening screening : screenings) {
            movie = screening.getMovie();
            table.addCell(screening.getLocation());
            table.addCell(String.format("%s   %2$tH:%2$tM",
                screening.getDate().toString(), screening.getTime()));
            table.addCell(String.format("%d '", movie.getDuration()));
            table.addCell(movie.getMovieTitle());
            table.addCell(String.valueOf(movie.getYear()));
        }
        return table;
    }
    

    【讨论】:

      【解决方案2】:

      您必须设置 Table 和 Cell 事件来实现单元格间距。 你可以这样做:

      table.setTableEvent()
      table.getDefaultCell().setCellEvent()
      

      所以您的最终代码可能如下所示:

      table.setTableEvent(new PdfPTableEvent() {
              @Override
              public void tableLayout(PdfPTable table, float[][] width, float[] height,
                      int headerRows, int rowStart, PdfContentByte[] canvas) {
                  float widths[] = width[0];
                  float x1 = widths[0];
                  float x2 = widths[widths.length - 1];
                  float y1 = height[0];
                  float y2 = height[height.length - 1];
                  PdfContentByte cb = canvas[PdfPTable.LINECANVAS];
                  cb.rectangle(x1, y1, x2 - x1, y2 - y1);
                  cb.stroke();
                  cb.resetRGBColorStroke();
              }
          });
      
      
      table.getDefaultCell().setCellEvent(new PdfPCellEvent() {
              @Override
              public void cellLayout(PdfPCell cell, Rectangle position,
                      PdfContentByte[] canvases) {
                  float x1 = position.getLeft() + 2;
                  float x2 = position.getRight() - 2;
                  float y1 = position.getTop() - 2;
                  float y2 = position.getBottom() + 2;
                  PdfContentByte canvas = canvases[PdfPTable.LINECANVAS];
                  canvas.rectangle(x1, y1, x2 - x1, y2 - y1);
                  canvas.stroke();
              }
          });
      

      根据需要改变宽度、高度和左、上、右、下的值。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-10-18
        • 2017-01-20
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多