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