iText Java Table cellspacing

Deadly 提交于 2019-12-13 08:01:47

问题


i am using a java library iText for generating pdfs. The question is : How can i define a cellspacing in an itext table like this.

Thank you in advance!

P.S.:Thats part of my code:

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

回答1:


See the offical documentation:

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:


You will have to set Table and Cell events to achieve cellspacing. And you can do that like :

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

So your final code might look like :

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

Vary the values of width, height and left,top,right,bottom as required.



来源:https://stackoverflow.com/questions/23574020/itext-java-table-cellspacing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!