Itext使用记录

//创建文件

File file = null;

FileOutputStream outputStream = null;
Document document = null;
try {


File dir = new File(filePath);
if (!dir.exists()) {
dir.mkdirs();

}


//创建纸张指定大小

                document = new Document(PageSize.A2, 120, 120, 0, 0);

        document.setPageSize(new Rectangle(0, 0, 1220, 2200));


        file = new File(filePath + orgCj.getOrgName() + ".pdf");
outputStream = new FileOutputStream(file);
PdfWriter.getInstance(document, outputStream);

document.open(); 


//在Itext里面一般渲染表格都是逐渐行渲染 中间涉及到合并单元格 每一个单元格都可以设置不同样式不同边框,所以需要自己重写很多方法 但是最初的方法是 summaryTable.addCell()

                Table fundNameTable = new Table("表格的列数")

                summaryTable.setBorderWidth(0);//边框宽度

                summaryTable.setWidth(120);//宽

summaryTable.setPadding(8f);//每单元格的大小

summaryTable.setBorderWidth(0);//去表格边框

                summaryTable.addCell();//添加单元格 可以自己设置样式设置内容 如:

/**

* 渲染表格用 cell 单元格 公用格式 
*
* @param par
* @param borderPosition 1 左 2 右 3上 4下 可同时存在
* @param color
* @return

* @author majc
* @date 2017年1月4日 下午2:55:25
*/

public static Cell textCell(Paragraph par,String borderPosition,Color color){
if(color == null) {
color = decode("#e1e1e1");
}
Cell cell = PdfUtil.newCell(null, null, Element.ALIGN_LEFT, Element.ALIGN_MIDDLE, Cell.NO_BORDER, 1f, null, null,par , color);
if(StringUtils.isNotBlank(borderPosition)) {
if(borderPosition.indexOf("1")>-1){
cell.setBorderColorLeft(color);
cell.setBorderWidthLeft(1f);
}
if(borderPosition.indexOf("2")>-1) {
cell.setBorderColorRight(color);
cell.setBorderWidthRight(1f);
}
if(borderPosition.indexOf("3")>-1){
cell.setBorderColorTop(color);
cell.setBorderWidthTop(1f);
}
if(borderPosition.indexOf("4")>-1) {
cell.setBorderColorBottom(color);
cell.setBorderWidthBottom(1f);
}


}
return cell;
}

       // 最后

        document.add(summaryTable);

        document.newPage();
document.close();

        outputStream.close();


        return file;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值