itextpdf生成表格的基本用法和常用方法

1.基础建表


public static void createTable() throws IOException, DocumentException {

    Document document = new Document();

    PdfWriter.getInstance(document, new FileOutputStream(DEST));

    document.open();

    //新建表格,参数为列数
    PdfPTable table = new PdfPTable(5);

    for (int i = 0; i < 10; i++) {

        // 构建每一格

        table.addCell("cell");

    }

    document.add(table);

    document.close();

}

结果如图

2.常用方法

public static void createTablePdf() throws IOException, DocumentException {

    Document document = new Document();

    // 创建PdfWriter对象

    PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(DEST));

    // 打开文档

    document.open();



    // 添加表格,4列

    PdfPTable table = new PdfPTable(4);

     设置表格宽度比例为%100

    table.setWidthPercentage(100);

    // 设置表格的宽度

    table.setTotalWidth(500);

    // 也可以每列分别设置宽度

    table.setTotalWidth(new float[] { 160, 70, 130, 100 });

    // 锁住宽度

    table.setLockedWidth(true);

    // 设置表格上面空白宽度

    table.setSpacingBefore(10f);

    // 设置表格下面空白宽度

    table.setSpacingAfter(10f);

    // 设置表格默认为无边框

    table.getDefaultCell().setBorder(0);

    PdfContentByte cb = writer.getDirectContent();



    // 构建每个单元格

    PdfPCell cell1 = new PdfPCell(new Paragraph("Cell 1"));

    // 边框颜色

    cell1.setBorderColor(BaseColor.BLUE);

    // 设置背景颜色

    cell1.setBackgroundColor(BaseColor.ORANGE);

    // 设置跨两行

    cell1.setRowspan(2);

    // 设置距左边的距离

    cell1.setPaddingLeft(10);

    // 设置高度

    cell1.setFixedHeight(20);

    // 设置内容水平居中显示

    cell1.setHorizontalAlignment(Element.ALIGN_CENTER);

    // 设置垂直居中

    cell1.setVerticalAlignment(Element.ALIGN_MIDDLE);

    table.addCell(cell1);



    PdfPCell cell2 = new PdfPCell(new Paragraph("Cell 2"));

    cell2.setBorderColor(BaseColor.GREEN);

    cell2.setPaddingLeft(10);

    cell2.setHorizontalAlignment(Element.ALIGN_CENTER);

    cell2.setVerticalAlignment(Element.ALIGN_MIDDLE);

    table.addCell(cell2);



    PdfPCell cell3 = new PdfPCell(new Paragraph("Cell 3"));

    cell3.setBorderColor(BaseColor.RED);

    cell3.setPaddingLeft(10);

    // 设置无边框

    cell3.setBorder(Rectangle.NO_BORDER);

    cell3.setHorizontalAlignment(Element.ALIGN_CENTER);

    cell3.setVerticalAlignment(Element.ALIGN_MIDDLE);

    table.addCell(cell3);



    // 在表格添加图片

    Image cellimg = Image.getInstance(IMG1);

    PdfPCell cell4 = new PdfPCell(cellimg, true);

    cell4.setBorderColor(BaseColor.RED);

    cell4.setPaddingLeft(10);

    cell4.setFixedHeight(30);

    cell4.setHorizontalAlignment(Element.ALIGN_CENTER);

    cell4.setVerticalAlignment(Element.ALIGN_MIDDLE);

    table.addCell(cell4);



    // 增加一个条形码到表格

    Barcode128 code128 = new Barcode128();

    code128.setCode("14785236987541");

    code128.setCodeType(Barcode128.CODE128);

    // 生成条形码图片

    Image code128Image = code128.createImageWithBarcode(cb, null, null);

    // 加入到表格

    PdfPCell cellcode = new PdfPCell(code128Image, true);

    cellcode.setHorizontalAlignment(Element.ALIGN_CENTER);

    cellcode.setVerticalAlignment(Element.ALIGN_MIDDLE);

    cellcode.setFixedHeight(30);

    table.addCell(cellcode);



    PdfPCell cell5 = new PdfPCell(new Paragraph("Cell 5"));

    cell5.setPaddingLeft(10);

    // 设置占用列数

    cell5.setColspan(2);

    cell5.setHorizontalAlignment(Element.ALIGN_CENTER);

    cell5.setVerticalAlignment(Element.ALIGN_MIDDLE);

    table.addCell(cell5);

    document.add(table);

    // 关闭文档

    document.close();

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值