日期:2014-05-16 浏览次数:20555 次
package com.fruitking.testpdf.util; import java.awt.Color; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; import com.lowagie.text.Document; import com.lowagie.text.DocumentException; import com.lowagie.text.Font; import com.lowagie.text.PageSize; import com.lowagie.text.Paragraph; import com.lowagie.text.pdf.BaseFont; import com.lowagie.text.pdf.PdfPCell; import com.lowagie.text.pdf.PdfPTable; import com.lowagie.text.pdf.PdfWriter; public class PDFCreate2Table { private String[][] normalTableContent = new String[][]{ {"序号","姓名","年龄","职业","籍贯","学历","单位名称","联系电话","联系地址","备注"}, {"1","许果","31","软件工程师","浙江杭州","大学本科","浙江水果大王信息技术有限公司","18905710571","浙江省杭州市西湖区三墩镇三墩街188号","他是一名优秀的IT工程师,日常爱好旅游,运动"} }; private String[][] columnTooMuchTableContent = new String[][]{ {"序号","姓名","年龄","职业","籍贯","学历","单位名称","联系电话","联系地址", "语文","数学","英语","物理","化学","生物","政治","历史","地理","音乐","美术","体育","课外实践","学校名称","备注"}, {"1","许果","31","软件工程师","浙江杭州","大学本科","浙江水果大王信息技术有限公司","18905710571","浙江省杭州市西湖区三墩镇三墩街188号", "85","95","75","90","90","85","80","90","90","75","65","75","80","石城中学","他是一名优秀的IT工程师,日常爱好旅游,运动"} }; /** * 创建一份普通表格的PDF文件 * @param fullFilePath * @return */ public boolean createNormalTable(String fullFilePath,int rowsNumber){ Document pdfDocument = new Document(); try { //构建一个PDF文档输出流程 OutputStream pdfFileOutputStream = new FileOutputStream(new File(fullFilePath)); PdfWriter.getInstance(pdfDocument,pdfFileOutputStream); //设置中文字体和字体样式 BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED); Font f8 = new Font(bfChinese, 8, Font.NORMAL); //打开PDF文件流 pdfDocument.open(); //创建一个N列的表格控件 PdfPTable pdfTable = new PdfPTable(normalTableContent[0].length); //设置表格占PDF文档100%宽度 pdfTable.setWidthPercentage(100); //水平方向表格控件左对齐 pdfTable.setHorizontalAlignment(PdfPTable.ALIGN_LEFT); //创建一个表格的表头单元格 PdfPCell pdfTableHeaderCell = new PdfPCell(); //设置表格的表头单元格颜色 pdfTableHeaderCell.setBackgroundColor(new Color(213, 141, 69)); pdfTableHeaderCell.setHorizontalAlignment(PdfPCell.ALIGN_CENTER); for(String tableHeaderInfo : normalTableContent[0]){ pdfTableHeaderCell.setPhrase(new Paragraph(tableHeaderInfo, f8)); pdfTable.addCell(pdfTableHeaderCell); } //创建一个表格的正文内容单元格 PdfPCell pdfTableContentCell = new PdfPCell(); pdfTable