1 中文字体的添加
BaseFont bfChinese = BaseFont.createFont("STSong-Light","UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
注意:一定要确定导入四个jar包(缺一个就可能导致中文字体添不上):itextpdf-5.5.10.jar itext-asian-5.2.0.jar 2.1.7\itext-2.1.7.jar itextasian-1.0.jar
下面是pom工程中导入包的maven依赖
<!--生成pdf -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.5</version>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.5.10</version>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itext-asian</artifactId>
<version>5.2.0</version>
</dependency>
<dependency>
<groupId>com.lowagie</groupId>
<artifactId>itext</artifactId>
<version>2.1.7</version>
</dependency>
<dependency>
<groupId>com.lowagie</groupId>
<artifactId>itextasian</artifactId>
<version>1.0</version>
</dependency>
2 字体样式的生成
Font textFont = new Font(bfChinese, 14, Font.NORMAL); // 正常
Font redTextFont = new Font(bfChinese, 11, Font.NORMAL, Color.RED); // 正常,红色
Font boldFont = new Font(bfChinese, 11, Font.BOLD); // 加粗
Font redBoldFont = new Font(bfChinese, 11, Font.BOLD, Color.RED); // 加粗,红色
Font firsetTitleFont = new Font(bfChinese, 22); // 一级标题
Font secondTitleFont = new Font(bfChinese, 15, Font.BOLD); // 二级标题
3 表格的生成
PdfPTable table = new PdfPTable(8);
table.setTotalWidth(new float[] { 50, 50, 50, 50, 50, 50, 50, 130 }); // 设置列宽
table.setLockedWidth(true); // 锁定列宽
PdfPCell cell; //生成单元格
cell.disableBorderSide(1); // 隐藏单元格周边的上边框
cell.disableBorderSide(2);// 隐藏单元格周边的下边框
cell.disableBorderSide(4); // 隐藏单元格周边的左边框
cell.disableBorderSide(8);// 隐藏单元格周边的右框
cell.setBorderWidthLeft(3);//设置边框的宽度
cell.setBorderWidthTop(3);//设置边框的宽度
cell = new PdfPCell(new Phrase("报批工程建筑面积明细表", boldFont));//向单元格中插入数据
cell.setColspan(8);//跨列合并单元格
cell.setRowspan(2);//跨行合并单元格
cell.setMinimumHeight(40); // 设置单元格高度
cell.setUseAscender(true); // 设置可以居中
cell.setHorizontalAlignment(Cell.ALIGN_CENTER); // 设置水平居中
cell.setVerticalAlignment(Cell.ALIGN_MIDDLE); // 设置垂直居中
table.addCell(cell);
4 Phrase Paragraph Chrunk List 的细节方法
详见http://blog.youkuaiyun.com/asdfsadfasdfsa/article/details/75456965