iText Java Table cellspacing

时间:2014-05-09 21:10:32

标签: java itext cellspacing

我使用java库iText生成pdfs。问题是:我如何在像this这样的迭代表中定义一个cellspacing。

提前谢谢!

P.S.:Thats我的代码的一部分:

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;
                }
            }

2 个答案:

答案 0 :(得分: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;
}

答案 1 :(得分:0)

您必须设置Table和Cell事件才能实现cellspacing。 你可以这样做:

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();
        }
    });

根据需要改变宽度,高度以及左,上,右,下的值。