iText Java Table cellspacing [英] iText Java Table cellspacing

查看:241
本文介绍了iText Java Table cellspacing的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

提前谢谢!

PS:这是我的部分代码:

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


推荐答案

参见官方文件:

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

这篇关于iText Java Table cellspacing的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆