package com.bbd; import com.itextpdf.text.*; import com.itextpdf.text.pdf.BaseFont; import com.itextpdf.text.pdf.PdfPCell; import com.itextpdf.text.pdf.PdfPTable; import com.itextpdf.text.pdf.PdfWriter; import java.io.FileOutputStream; import java.text.SimpleDateFormat; import java.util.Date; public class test { /** * 第一行的内边距 */ private static final int CELL_PADDING_1 = 10; /** * 第二行的内边距 */ private static final int CELL_PADDING_2 = 15; /** * 第三行的内边距 */ private static final int CELL_PADDING_3 = 8; /** * 第四行的内边距 */ private static final int CELL_PADDING_11 = 8; /** * 第十一行右缩进大小 */ private static final int INDENT_RIGHT_11 = 60; /** * 第十二行右缩进大小 */ private static final int INDENT_RIGHT_12 = 90; /** * 第4-10行的固定高度 */ private static final int CELL_HEIGHT = 60; private static BaseFont bfChinese; static { try { bfChinese = BaseFont.createFont("STSongStd-Light", "UniGB-UCS2-H", false); } catch (Exception e) { e.printStackTrace(); } } /** * 默认字体 */ private static Font fontChinese_default = new Font(bfChinese, 12); /** * 表格第一行字体 */ private static Font fontChinese_1 = new Font(bfChinese, 17, Font.BOLD); /** * 表格第三行字体 */ private static Font fontChinese_3 = new Font(bfChinese, 12, Font.BOLD); /** * 表格第四-七行字体 */ private static Font fontChinese_4_7 = new Font(bfChinese, 9); private static SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); public static void main(String[] args) { Document document = new Document(PageSize.LETTER, 36.0f, 36.0f, 36.0f, 36.0f); try { PdfWriter.getInstance(document, new FileOutputStream("e:/HelloWorld-Table.pdf")); document.open(); PdfPTable table = new PdfPTable(1); table.setWidthPercentage(100); int totalRowNum = 12; for (int i = 1; i <= totalRowNum; i++) { table.addCell(getTable(i)); } document.add(table); document.close(); } catch (Exception e) { } } /** * 根据行号生成不同cell * * @param rowNum 行号(从1开始) * @return * @throws Exception */ public static PdfPCell getTable(int rowNum) throws Exception { PdfPCell cell = new PdfPCell(); switch (rowNum) { case 1: Paragraph paragraph = new Paragraph("运维工作确认表", fontChinese_1); //水平居中 paragraph.setAlignment(Element.ALIGN_CENTER); //内边距 cell.setPadding(CELL_PADDING_1); cell.setUseAscender(true); cell.addElement(paragraph); break; case 2: //cell内部嵌套table PdfPTable table = new PdfPTable(6); //设置table的宽度为100% table.setWidthPercentage(100); //设置不同列的宽度 float[] columnWidths = {1.6f, 2f, 1f, 2f, 1.1f, 2f}; table.setWidths(columnWidths); PdfPCell cell1 = new PdfPCell(); cell1.setUseAscender(true); cell1.setPaddingTop(CELL_PADDING_2); cell1.setPaddingBottom(CELL_PADDING_2); Paragraph paragraph1 = new Paragraph("软件名称", fontChinese_default); paragraph1.setAlignment(Element.ALIGN_LEFT); cell1.addElement(paragraph1); PdfPCell cell2 = new PdfPCell(); cell2.setUseAscender(true); cell2.setPaddingTop(CELL_PADDING_2); cell2.setPaddingBottom(CELL_PADDING_2); Paragraph paragraph2 = new Paragraph("", fontChinese_default); paragraph2.setAlignment(Element.ALIGN_LEFT); cell2.addElement(paragraph2); PdfPCell cell3 = new PdfPCell(); cell3.setUseAscender(true); cell3.setPaddingTop(CELL_PADDING_2); cell3.setPaddingBottom(CELL_PADDING_2); Paragraph paragraph3 = new Paragraph("版本号", fontChinese_default); paragraph3.setAlignment(Element.ALIGN_LEFT); cell3.addElement(paragraph3); PdfPCell cell4 = new PdfPCell(new Paragraph("", fontChinese_default)); cell4.setUseAscender(true); cell4.setPaddingTop(CELL_PADDING_2); cell4.setPaddingBottom(CELL_PADDING_2); Paragraph paragraph4 = new Paragraph("", fontChinese_default); paragraph4.setAlignment(Element.ALIGN_LEFT); cell4.addElement(paragraph4); PdfPCell cell5 = new PdfPCell(); cell5.setUseAscender(true); cell5.setPaddingTop(CELL_PADDING_2); cell5.setPaddingBottom(CELL_PADDING_2); Paragraph paragraph5 = new Paragraph("检查日期", fontChinese_default); paragraph5.setAlignment(Element.ALIGN_LEFT); cell5.addElement(paragraph5); PdfPCell cell6 = new PdfPCell(new Paragraph("", fontChinese_default)); cell6.setUseAscender(true); cell6.setPaddingTop(CELL_PADDING_2); cell6.setPaddingBottom(CELL_PADDING_2); Paragraph paragraph6 = new Paragraph("" + getDate(), fontChinese_default); paragraph6.setAlignment(Element.ALIGN_LEFT); cell6.addElement(paragraph6); table.addCell(cell1); table.addCell(cell2); table.addCell(cell3); table.addCell(cell4); table.addCell(cell5); table.addCell(cell6); //去掉cell和table之间的间隙 cell.setPadding(0); cell.addElement(table); break; case 3: paragraph = new Paragraph("运维工作准备情况和发布意见", fontChinese_3); paragraph.setAlignment(Element.ALIGN_LEFT); cell.setPadding(CELL_PADDING_3); cell.setUseAscender(true); cell.addElement(paragraph); break; case 4: paragraph = new Paragraph("1:检查分类情况:", fontChinese_4_7); paragraph.setAlignment(Element.ALIGN_LEFT); cell.setFixedHeight(CELL_HEIGHT); cell.addElement(paragraph); break; case 5: paragraph = new Paragraph("2:检查DLL情况:", fontChinese_4_7); paragraph.setAlignment(Element.ALIGN_LEFT); cell.setFixedHeight(CELL_HEIGHT); cell.addElement(paragraph); break; case 6: paragraph = new Paragraph("3:检查规则情况:", fontChinese_4_7); paragraph.setAlignment(Element.ALIGN_LEFT); cell.setFixedHeight(CELL_HEIGHT); cell.addElement(paragraph); break; case 7: paragraph = new Paragraph("4:检查模板情况:", fontChinese_4_7); paragraph.setAlignment(Element.ALIGN_LEFT); cell.setFixedHeight(CELL_HEIGHT); cell.addElement(paragraph); break; case 8: paragraph = new Paragraph("5:触发点情况:", fontChinese_4_7); paragraph.setAlignment(Element.ALIGN_LEFT); cell.setFixedHeight(CELL_HEIGHT); cell.addElement(paragraph); break; case 9: paragraph = new Paragraph("6:应用DLL情况:", fontChinese_4_7); paragraph.setAlignment(Element.ALIGN_LEFT); cell.setFixedHeight(CELL_HEIGHT); cell.addElement(paragraph); break; case 10: paragraph = new Paragraph("7:应用DLL情况:", fontChinese_4_7); paragraph.setAlignment(Element.ALIGN_LEFT); cell.setFixedHeight(CELL_HEIGHT); cell.addElement(paragraph); break; case 11: //cell内部嵌套table table = new PdfPTable(3); //设置table的宽度为100% table.setWidthPercentage(100); //开始第一行 PdfPCell cell_11 = new PdfPCell(); paragraph = new Paragraph("产品:", fontChinese_default); paragraph.setAlignment(Element.ALIGN_LEFT); cell_11.setPadding(CELL_PADDING_11); cell_11.setUseAscender(true); cell_11.addElement(paragraph); table.addCell(cell_11); cell_11 = new PdfPCell(); paragraph = new Paragraph("测试:", fontChinese_default); paragraph.setAlignment(Element.ALIGN_LEFT); cell_11.setPadding(CELL_PADDING_11); cell_11.setUseAscender(true); cell_11.addElement(paragraph); table.addCell(cell_11); cell_11 = new PdfPCell(); paragraph = new Paragraph("程序:", fontChinese_default); paragraph.setAlignment(Element.ALIGN_LEFT); cell_11.setPadding(CELL_PADDING_11); cell_11.setUseAscender(true); cell_11.addElement(paragraph); table.addCell(cell_11); //开始第二行 cell_11 = new PdfPCell(); paragraph = new Paragraph("签名:", fontChinese_default); paragraph.setAlignment(Element.ALIGN_RIGHT); paragraph.setIndentationRight(INDENT_RIGHT_11);//右缩进 cell_11.setPadding(CELL_PADDING_11); cell_11.setUseAscender(true); cell_11.addElement(paragraph); table.addCell(cell_11); cell_11 = new PdfPCell(); paragraph = new Paragraph("签名:", fontChinese_default); paragraph.setAlignment(Element.ALIGN_RIGHT); paragraph.setIndentationRight(INDENT_RIGHT_11);//右缩进 cell_11.setPadding(CELL_PADDING_11); cell_11.setUseAscender(true); cell_11.addElement(paragraph); table.addCell(cell_11); cell_11 = new PdfPCell(); paragraph = new Paragraph("签名:", fontChinese_default); paragraph.setAlignment(Element.ALIGN_RIGHT); paragraph.setIndentationRight(INDENT_RIGHT_11);//右缩进 cell_11.setPadding(CELL_PADDING_11); cell_11.setUseAscender(true); cell_11.addElement(paragraph); table.addCell(cell_11); //开始第三行 cell_11 = new PdfPCell(); paragraph = new Paragraph("日期:", fontChinese_default); paragraph.setAlignment(Element.ALIGN_RIGHT); paragraph.setIndentationRight(INDENT_RIGHT_11);//右缩进 cell_11.setPadding(CELL_PADDING_11); cell_11.setUseAscender(true); cell_11.addElement(paragraph); table.addCell(cell_11); cell_11 = new PdfPCell(); paragraph = new Paragraph("日期:", fontChinese_default); paragraph.setAlignment(Element.ALIGN_RIGHT); paragraph.setIndentationRight(INDENT_RIGHT_11);//右缩进 cell_11.setPadding(CELL_PADDING_11); cell_11.setUseAscender(true); cell_11.addElement(paragraph); table.addCell(cell_11); cell_11 = new PdfPCell(); paragraph = new Paragraph("日期:", fontChinese_default); paragraph.setAlignment(Element.ALIGN_RIGHT); paragraph.setIndentationRight(INDENT_RIGHT_11);//右缩进 cell_11.setPadding(CELL_PADDING_11); cell_11.setUseAscender(true); cell_11.addElement(paragraph); table.addCell(cell_11); //去掉cell和table之间的间隙 cell.setPadding(0); cell.addElement(table); break; case 12: //cell内部嵌套table table = new PdfPTable(2); //设置table的宽度为100% table.setWidthPercentage(100); //开始第一行 PdfPCell cell_12 = new PdfPCell(); paragraph = new Paragraph("研发经理:", fontChinese_default); paragraph.setAlignment(Element.ALIGN_LEFT); cell_12.setPadding(CELL_PADDING_11); cell_12.setUseAscender(true); cell_12.addElement(paragraph); table.addCell(cell_12); cell_12 = new PdfPCell(); paragraph = new Paragraph("部门经理:", fontChinese_default); paragraph.setAlignment(Element.ALIGN_LEFT); cell_12.setPadding(CELL_PADDING_11); cell_12.setUseAscender(true); cell_12.addElement(paragraph); table.addCell(cell_12); //开始第二行 cell_12 = new PdfPCell(); paragraph = new Paragraph("签名:", fontChinese_default); paragraph.setAlignment(Element.ALIGN_RIGHT); paragraph.setIndentationRight(INDENT_RIGHT_12);//右缩进 cell_12.setPadding(CELL_PADDING_11); cell_12.setUseAscender(true); cell_12.addElement(paragraph); table.addCell(cell_12); cell_12 = new PdfPCell(); paragraph = new Paragraph("签名:", fontChinese_default); paragraph.setAlignment(Element.ALIGN_RIGHT); paragraph.setIndentationRight(INDENT_RIGHT_12);//右缩进 cell_12.setPadding(CELL_PADDING_11); cell_12.setUseAscender(true); cell_12.addElement(paragraph); table.addCell(cell_12); //开始第三行 cell_12 = new PdfPCell(); paragraph = new Paragraph("日期:", fontChinese_default); paragraph.setAlignment(Element.ALIGN_RIGHT); paragraph.setIndentationRight(INDENT_RIGHT_12);//右缩进 cell_12.setPadding(CELL_PADDING_11); cell_12.setUseAscender(true); cell_12.addElement(paragraph); table.addCell(cell_12); cell_12 = new PdfPCell(); paragraph = new Paragraph("日期:", fontChinese_default); paragraph.setAlignment(Element.ALIGN_RIGHT); paragraph.setIndentationRight(INDENT_RIGHT_12);//右缩进 cell_12.setPadding(CELL_PADDING_11); cell_12.setUseAscender(true); cell_12.addElement(paragraph); table.addCell(cell_12); //去掉cell和table之间的间隙 cell.setPadding(0); cell.addElement(table); break; } return cell; } public static String getDate() { return format.format(new Date()); } }
效果: