<!-- https://mvnrepository.com/artifact/com.itextpdf/itextpdf --> <dependency> <groupId>com.itextpdf</groupId> <artifactId>itextpdf</artifactId> <version>5.5.11</version> </dependency> <!-- https://mvnrepository.com/artifact/com.itextpdf/layout --> <dependency> <groupId>com.itextpdf</groupId> <artifactId>layout</artifactId> <version>7.0.4</version> </dependency> <!-- https://mvnrepository.com/artifact/com.itextpdf/kernel --> <dependency> <groupId>com.itextpdf</groupId> <artifactId>kernel</artifactId> <version>7.0.4</version> </dependency>
这个是我使用的itext的依赖,废话不多说直接上代码吧。
public static final String DEST = "d://simple_table.pdf"; private static final int default_minimumHeight=45; public static void main(String[] args) throws Exception { ProductionManageRepairApplyPdfDTO repairApplyPdfDTO=new ProductionManageRepairApplyPdfDTO(); String testDate="2019-01-05"; repairApplyPdfDTO.setApplyDoneDate(testDate); repairApplyPdfDTO.setCallRepairDate(testDate); repairApplyPdfDTO.setCallRepairDepartmentName("销售部门"); repairApplyPdfDTO.setCallRepairEmployeeName("酒吧"); repairApplyPdfDTO.setEstimateDoneRepairDate(testDate); repairApplyPdfDTO.setOrderTakerDate(testDate); repairApplyPdfDTO.setOrderTakerName("周杰伦"); repairApplyPdfDTO.setRepairDepartmentName("维修部门-生产部门"); repairApplyPdfDTO.setRepairNo("201910171110"); repairApplyPdfDTO.setRepairContent("我的大提琴坏了 快点来给我修一修吧v"); List<ProductionManageConsumeStuffDTO> consumeStuffDTOList=new ArrayList<>(); ProductionManageConsumeStuffDTO consumeStuffDTO1=new ProductionManageConsumeStuffDTO(); consumeStuffDTO1.setStuffName("锤子"); consumeStuffDTO1.setSpecification("1/克"); consumeStuffDTO1.setStuffAmount(20); ProductionManageConsumeStuffDTO consumeStuffDTO2=new ProductionManageConsumeStuffDTO(); consumeStuffDTO2.setStuffName("飞机"); consumeStuffDTO2.setSpecification("1/驾"); consumeStuffDTO2.setStuffAmount(120); consumeStuffDTOList.add(consumeStuffDTO1); consumeStuffDTOList.add(consumeStuffDTO2); repairApplyPdfDTO.setConsumeStuffDTOList(consumeStuffDTOList); System.out.println(JSONObject.toJSONString(repairApplyPdfDTO)); exportPdf(repairApplyPdfDTO); } private static void exportPdf(ProductionManageRepairApplyPdfDTO repairApplyPdfDTO) throws Exception { BaseFont baseFontHei = BaseFont.createFont("simhei.ttf",BaseFont.IDENTITY_H,BaseFont.NOT_EMBEDDED); Document document = new Document(PageSize.A4, 70, 70, 20, 100);//上下左右页边距 Rectangle pageSize = document.getPageSize(); PdfWriter.getInstance(document, new FileOutputStream("d://sample.pdf")); //打开文本 document.open(); //标题 Paragraph paragraph = new Paragraph(80);//边距 //1 2 3 中右左 paragraph.setAlignment(1); //对齐方式 Font font = new Font(baseFontHei);//字体 font.setSize(14);//字体大小 paragraph.setFont(font);//设置段落字体 Chunk chunk = new Chunk("工程维修单"); chunk.setLineHeight(80); paragraph.add(chunk); paragraph.setSpacingAfter(20);//往下距离200 document.add(paragraph); Paragraph paragraph1 = new Paragraph(10); //1 2 3 中右左 paragraph1.setAlignment(2); //对齐方式 Font font1 = new Font(baseFontHei);//字体 font1.setSize(10); paragraph1.setFont(font1); Chunk chunk1 = new Chunk("编号:"+repairApplyPdfDTO.getRepairNo()); paragraph1.add(chunk1); // paragraph1.setSpacingBefore(-25); paragraph1.setSpacingAfter(5);//往下距离200 document.add(paragraph1); PdfPTable table = new PdfPTable(4); table.setTotalWidth(500); float[] columnWidth1={100,150,100,150}; table.setTotalWidth(columnWidth1); table.setLockedWidth(true);//宽度算正确 setTable(baseFontHei, table, columnWidth1,new String[]{"报修部门:",repairApplyPdfDTO.getCallRepairDepartmentName(),"保修人员:",repairApplyPdfDTO.getCallRepairEmployeeName()},default_minimumHeight); document.add(table); //画表头第二行 table = new PdfPTable(4); table.setTotalWidth(500); table.setTotalWidth(columnWidth1); table.setLockedWidth(true);//宽度算正确 setTable(baseFontHei, table, columnWidth1,new String[]{"报修时间:",repairApplyPdfDTO.getCallRepairDate(),"接单时间:",repairApplyPdfDTO.getOrderTakerDate()},default_minimumHeight); document.add(table); //画表头第三行 table = new PdfPTable(4); table.setTotalWidth(500); table.setTotalWidth(columnWidth1); table.setLockedWidth(true);//宽度算正确 setTable(baseFontHei, table, columnWidth1,new String[]{"维修部门:",repairApplyPdfDTO.getRepairDepartmentName(),"接单人员:",repairApplyPdfDTO.getOrderTakerName()},default_minimumHeight); document.add(table); //画表头第4行 table = new PdfPTable(3); table.setTotalWidth(500); float[] columnWidth4={300,100,100}; setTable(baseFontHei, table, columnWidth4,new String[]{"维修内容","申请完成时间","工程预计完成时间"},default_minimumHeight); document.add(table); //画表头第5行 table = new PdfPTable(3); table.setTotalWidth(500); float[] columnWidth5={300,100,100}; setTable(baseFontHei, table, columnWidth4,new String[]{repairApplyPdfDTO.getRepairContent(),repairApplyPdfDTO.getApplyDoneDate(),repairApplyPdfDTO.getEstimateDoneRepairDate()},default_minimumHeight); document.add(table); //画表头第6行 table = new PdfPTable(1); table.setTotalWidth(500); float[] columnWidth6={500}; setTable(baseFontHei, table, columnWidth6,new String[]{"消耗材料名称规格数量"},18); document.add(table); List<ProductionManageConsumeStuffDTO> consumeStuffDTOList = repairApplyPdfDTO.getConsumeStuffDTOList(); if (CollectionUtils.isNotEmpty(consumeStuffDTOList)){ //画表头第5行 table = new PdfPTable(3); table.setTotalWidth(500); float[] columnWidth={168,166,166}; setTable(baseFontHei, table, columnWidth,new String[]{"材料名称","规格","数量"},26); document.add(table); for (ProductionManageConsumeStuffDTO productionManageConsumeStuffDTO : consumeStuffDTOList) { String stuffName = productionManageConsumeStuffDTO.getStuffName(); String specification = productionManageConsumeStuffDTO.getSpecification(); Integer stuffAmount = productionManageConsumeStuffDTO.getStuffAmount(); table = new PdfPTable(3); table.setTotalWidth(500); float[] s_columnWidth={168,166,166}; setTable(baseFontHei, table, s_columnWidth,new String[]{stuffName,specification,stuffAmount+""},30); document.add(table); } } //画表头第7行 table = new PdfPTable(2); table.setTotalWidth(500); float[] columnWidth7={190,310}; setTable(baseFontHei, table, columnWidth7,new String[]{"报修部门确认:",""},default_minimumHeight); document.add(table); //画表头第7行 table = new PdfPTable(2); table.setTotalWidth(500); setTable(baseFontHei, table, columnWidth7,new String[]{"维修部门意见:",""},default_minimumHeight); document.add(table); //画表头第7行 table = new PdfPTable(2); table.setTotalWidth(500); setTable(baseFontHei, table, columnWidth7,new String[]{"报修部门主管领导审批:",""},default_minimumHeight); document.add(table); //画表头第7行 table = new PdfPTable(2); table.setTotalWidth(500); setTable(baseFontHei, table, columnWidth7,new String[]{"维修部门主管领导审批:",""},default_minimumHeight); document.add(table); //画表头第7行 table = new PdfPTable(2); table.setTotalWidth(500); float[] columnWidth8={250,250}; setTable(baseFontHei, table, columnWidth8,new String[]{"维修人员签字、完成日期、时间","报修部门跟进人员确认签字"},default_minimumHeight); document.add(table); //画表头第7行 table = new PdfPTable(2); table.setTotalWidth(500); float[] columnWidth9={250,250}; setTable(baseFontHei, table, columnWidth8,new String[]{"",""},default_minimumHeight); document.add(table); document.close(); } // private static void setTable(BaseFont baseFontHei, PdfPTable table, float[] columnWidth2,String[] cells,int minimumHeight) throws Exception { table.setTotalWidth(500); table.setTotalWidth(columnWidth2); table.setLockedWidth(true);//宽度算正确 for (String cell : cells) { table.addCell(drawPdfPCell(cell,baseFontHei,10,1,minimumHeight)); } } private static PdfPCell drawPdfPCell(String cellText, BaseFont baseFont, float size, int alignment, int minimumHeight ) throws Exception{ //为null会报错 防止报错 if(cellText==null){ cellText=" "; } //表格开始 Paragraph paragraph = new Paragraph(); paragraph.setAlignment(alignment); //对齐方式 Font font = new Font(baseFont);//字体 font.setSize(size);//字体大小 paragraph.setFont(font);//设置段落字体 Chunk chunk = new Chunk(cellText); paragraph.add(chunk); PdfPCell cell = new PdfPCell(); cell.setUseAscender(true); cell.setVerticalAlignment(cell.ALIGN_MIDDLE);//设置cell垂直居中 cell.setMinimumHeight(minimumHeight);//设置单元格最小高度,当前行最小高度 cell.addElement(paragraph); return cell; }
最后生成的效果是: