java实现导出pdf文件(模板+纯代码)

时间:2025-04-03 09:21:00
public static void download(List<Map<String,String>> list, Map<String, String> mapData, HttpServletResponse response) throws IOException { Document document = new Document(); document.setMargins(70, 70, 20, 20); try { Font content = content = getFont("10"); PdfWriter.getInstance(document,response.getOutputStream()); //打开生成的pdf文件 document.open(); //设置内容 Paragraph paragraph = new Paragraph("一周工作安排", getFont("4")); paragraph.setAlignment(1); //引用字体 document.add(paragraph); document.add(new Paragraph("\n", getFont("4"))); Paragraph paragraph0 = new Paragraph(mapData.get("term"), content); paragraph0.setAlignment(Element.ALIGN_RIGHT); document.add(paragraph0); //设置每页大小 document.setPageSize(new RectangleReadOnly(595.0F, 600.0F)); PdfPCell cell = null; // 设置表格的列宽和列数 float[] widths2 = {15f,15f,45f,30f,30f}; PdfPTable table = new PdfPTable(widths2); table.setSpacingBefore(10f); // 设置表格宽度为100% table.setWidthPercentage(100.0F); table.setHeaderRows(1); table.getDefaultCell().setHorizontalAlignment(1); //表头 cell = new PdfPCell(new Paragraph("星期",content)); cell.setVerticalAlignment(Element.ALIGN_MIDDLE); cell.setHorizontalAlignment(Element.ALIGN_CENTER); cell.setFixedHeight(50); table.addCell(cell); cell = new PdfPCell(new Paragraph("日期",content)); cell.setVerticalAlignment(Element.ALIGN_MIDDLE); cell.setHorizontalAlignment(Element.ALIGN_CENTER); table.addCell(cell); cell = new PdfPCell(new Paragraph("工作内容",content)); cell.setVerticalAlignment(Element.ALIGN_MIDDLE); cell.setHorizontalAlignment(Element.ALIGN_CENTER); table.addCell(cell); cell = new PdfPCell(new Paragraph("地点",content)); cell.setVerticalAlignment(Element.ALIGN_MIDDLE); cell.setHorizontalAlignment(Element.ALIGN_CENTER); table.addCell(cell); cell = new PdfPCell(new Paragraph("负责部门",content)); cell.setVerticalAlignment(Element.ALIGN_MIDDLE); cell.setHorizontalAlignment(Element.ALIGN_CENTER); table.addCell(cell); //遍历一周安排数据 for (Map<String,String> data : list) { PdfPCell cell1 = new PdfPCell(new Paragraph(data.get("week"), content)); PdfPCell cell2 = new PdfPCell(new Paragraph(data.get("date"), content)); PdfPCell cell3 = new PdfPCell(); ///n为自定义换行符号-没有特殊含义 if(!(("content"))&&("content").contains("/n")){ for(String s:data.get("content").split("/n")){ cell3.addElement(new Chunk(s,content)); } } PdfPCell cell4 =new PdfPCell(); if(!EmptyUtil.isEmpty(data.get("address"))&&data.get("address").contains("/n")){ for(String s:data.get("address").split("/n")){ cell4.addElement(new Chunk(s,content)); } } PdfPCell cell5 =new PdfPCell(); if(!EmptyUtil.isEmpty(data.get("dept"))&&data.get("dept").contains("/n")){ for(String s:data.get("dept").split("/n")){ cell5.addElement(new Chunk(s,content)); } } //单元格对齐方式 cell1.setHorizontalAlignment(Element.ALIGN_CENTER); cell1.setVerticalAlignment(Element.ALIGN_MIDDLE); cell1.setFixedHeight(40); //单元格垂直对齐方式 cell2.setHorizontalAlignment(Element.ALIGN_CENTER); cell2.setVerticalAlignment(Element.ALIGN_MIDDLE); cell3.setHorizontalAlignment(Element.ALIGN_CENTER); cell3.setVerticalAlignment(Element.ALIGN_MIDDLE); cell4.setHorizontalAlignment(Element.ALIGN_CENTER); cell4.setVerticalAlignment(Element.ALIGN_MIDDLE); cell5.setHorizontalAlignment(Element.ALIGN_CENTER); cell5.setVerticalAlignment(Element.ALIGN_MIDDLE); table.addCell(cell1); table.addCell(cell2); table.addCell(cell3); table.addCell(cell4); table.addCell(cell5); } PdfPCell cellLeft = new PdfPCell(new Paragraph("备注", content)); cellLeft.setFixedHeight(40); cellLeft.setVerticalAlignment(Element.ALIGN_MIDDLE); cellLeft.setHorizontalAlignment(Element.ALIGN_CENTER); PdfPCell cellRight = new PdfPCell(new Paragraph(mapData.get("remark"), content)); cellRight.setVerticalAlignment(Element.ALIGN_MIDDLE); cellRight.setHorizontalAlignment(Element.ALIGN_CENTER); cellRight.setColspan(4); table.addCell(cellLeft); table.addCell(cellRight); // (new Paragraph("\n")); // (new Paragraph("▋ 信息",content)); // (new Paragraph("\n")); // document.add(table); //关闭文档 document.close(); } catch (DocumentException e) { e.printStackTrace(); } }