//创建文件
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;