itext使用小结

Itext生成table小结

17.07.28日写

需要的jar包

    <!-- itextpdfJar包 -->
    <dependency>
        <groupId>com.itextpdf</groupId>
        <artifactId>itextpdf</artifactId>
        <version>5.2.1</version>
    </dependency>
    <!-- 解决中文显示问题的Jar包 -->
    <dependency>
        <groupId>com.itextpdf</groupId>
        <artifactId>itext-asian</artifactId>
        <version>5.2.0</version>
    </dependency>

生成一个PDF文档

//创建PDF文档
Document document = new Document();
//创建 PdfWriter instance.
//PdfWriter.getInstance(document, new FileOutputStream(“D:\” + “createSamplePDF.pdf”)); //该方法写入到磁盘
response.setHeader(“Content-disposition”, “attachment; filename=jx.pdf”);
response.setContentType(“application/pdf”);
PdfWriter.getInstance(document, response.getOutputStream()); //变成一个下载流
//打开Document.
document.open();
//设置中文字体
BaseFont baseFont = BaseFont.createFont(“STSong-Light”, “UniGB-UCS2-H”,BaseFont.NOT_EMBEDDED);
Font font = new Font(baseFont);
//加入第一行content.
//document.add(new Paragraph(new String(“你好世界!”.getBytes(),”GBK”) ,font));
document.add(new Paragraph(“你好世界!”,font));

创建一个table和其中的cell

/**
* 开始第二张表格
*/
PdfPTable table2=new PdfPTable(42); //设置表格所占列数
PdfPCell [][] cell2=new PdfPCell[20][10];//cell数组
/**
* 第一行
*/
cell2[1][1]=createCell(“课程性质”,2,25);
cell2[1][2]=createCell(“小计”,1,6);
cell2[1][3]=createCell(“小计”,1,6);
cell2[1][4]=createCell(“备注”,1,5);
/**
* 第二行
*/
cell2[2][2]=createCell(“学时”,1,3);
cell2[2][3]=createCell(“比例”,1,3);
cell2[2][4]=createCell(“学分”,1,3);
cell2[2][5]=createCell(“比例”,1,3);
cell2[2][6]=createCell(” “,1,5);

        for(int i=0;i<20;i++){
            for(int j=0;j<10;j++){
                if(cell2[i][j]!=null){
                    table2.addCell(cell2[i][j]);
                }
            }
        }

        table2.setWidthPercentage(60); //设置表格宽度(百分比)
        table2.setHorizontalAlignment(0);//设置表格居左
        table.setSpacingAfter(15f);  //设置表格后间距
        document.add(table2);
        /**
 * 创建一个单元格的cell
 * @param cellName
 * @return
 * @throws DocumentException
 * @throws IOException
 */
public PdfPCell createCell(String cellName) throws DocumentException, IOException{
    BaseFont title1 = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H",BaseFont.NOT_EMBEDDED);
    // 设置字体
    Font mainFont = new Font(title1,7,Font.NORMAL);
    float f = 0.5f;
    float lineHeight = 18f;
    PdfPCell cell = new PdfPCell(new Phrase(cellName, mainFont));
    cell.setUseBorderPadding(true);
    cell.setBorderWidth(f);
    cell.setHorizontalAlignment(Element.ALIGN_CENTER);
    cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
    return cell;
}
/**
 * 创建占据多个单元格的cell
 * @param cellName
 * @param row
 * @param col
 * @return
 * @throws DocumentException
 * @throws IOException
 */
public PdfPCell createCell(String cellName,Integer row,Integer col) throws DocumentException, IOException{
    BaseFont title1 = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H",BaseFont.NOT_EMBEDDED);
    Font titleFont = new Font(title1, 25, Font.NORMAL);
    // 设置字体
    Font mainFont = new Font(title1,7, Font.NORMAL);
    float f = 0.5f;
    float lineHeight = 18f;
    PdfPCell cell = new PdfPCell(new Phrase(cellName, mainFont));
    cell.setUseBorderPadding(true);
    cell.setBorderWidth(f);
    cell.setHorizontalAlignment(Element.ALIGN_CENTER);//设置左右居中
    cell.setVerticalAlignment(Element.ALIGN_MIDDLE);//设置上下居中
    //设置cell占据的行列数
    if(row!=null){
        cell.setRowspan(row);//设置cell占据行数
    }
    if(col!=null){
        cell.setColspan(col);//设置cell占据列数
    }
    return cell;
}

常用方法

        table.setWidthPercentage(100);      //表格宽度定义100%
        table.setSpacingAfter(15f);         //设置表格后间距

复杂表格生成的代码示例

// 标题字体
        BaseFont title1 = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H",BaseFont.NOT_EMBEDDED);
        Font titleFont = new Font(title1, 25, Font.NORMAL);
        // 设置字体
        Font mainFont = new Font(title1, 10, Font.NORMAL);
        float f = 0.5f;
        Document doc = null;
        float lineHeight = 18f;
        PdfPCell[][] cell = new PdfPCell[100][20];
 
  • 21
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值