SpringBoot--整合itextpdf
2021-06-05 14:40
246 查看
SpringBoot--整合itextpdf
Dependency
<!--SpringBoot:PDF--> <!-- https://mvnrepository.com/artifact/com.itextpdf/itextpdf --> <dependency> <groupId>com.itextpdf</groupId> <artifactId>itextpdf</artifactId> <version>5.5.13</version> </dependency> <!-- https://mvnrepository.com/artifact/com.itextpdf/itext-asian --> <dependency> <groupId>com.itextpdf</groupId> <artifactId>itext-asian</artifactId> <version>5.2.0</version> </dependency>
/** *@Description: 生成pdf *@Author: HuYuQiao */ private static final SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); private static final Integer size = 20; private static final String formatTimeStr = format.format(new Date()); @Test public void testPDF(){ Document document = new Document(PageSize.A4); try{ PdfWriter.getInstance(document, new FileOutputStream("src/main/resources/hyq.pdf")); document.addTitle("PDF Title"); document.open(); //PDF--添加table PdfPTable pdfPTable = generatePDFTable(); document.add(pdfPTable); } catch (Exception e) { e.printStackTrace(); } finally { document.close(); } } /** *@Description: 生成pdf的talble *@Author: HuYuQiao */ public PdfPTable generatePDFTable() throws IOException, DocumentException { Font font = new Font(BaseFont.createFont( "STSongStd-Light" ,"UniGB-UCS2-H",BaseFont.NOT_EMBEDDED)); font.setSize(7); PdfPTable table = new PdfPTable(10); //PDF--第一行 PdfPCell pdfTitle= new PdfPCell(new Phrase("PDF导出模板", font)); pdfTitle.setColspan(10); //所占列数 setStyle(pdfTitle); table.addCell(pdfTitle); PdfPCell officialSeal = new PdfPCell(new Phrase("申请单位公章:", font)); officialSeal.setColspan(2); setStyle(officialSeal); table.addCell(officialSeal); // 空格 第二行 PdfPCell cell3 = new PdfPCell(new Phrase(" ")); cell3.setColspan(2); setStyle(cell3); table.addCell(cell3); // 空格 第二行 PdfPCell cell4 = new PdfPCell(new Phrase(" ")); cell4.setColspan(1); setStyle(cell4); table.addCell(cell4); // 申请日期: 第二行 PdfPCell cell5 = new PdfPCell(new Phrase("申请日期:" + formatTimeStr, font)); cell5.setColspan(2); setStyle(cell5); table.addCell(cell5); // 空格 第二行 PdfPCel 56c l cell6 = new PdfPCell(new Phrase(" ")); cell6.setColspan(1); setStyle(cell6); table.addCell(cell6); // 单位:元 第二行 PdfPCell cell7 = new PdfPCell(new Phrase("单位:元", font)); cell7.setColspan(2); setStyle(cell7); table.addCell(cell7); // 序号 第三,四行 PdfPCell cell8 = new PdfPCell(new Phrase("序号", font)); cell8.setColspan(1); // 占几列 cell8.setRowspan(2); // 占几行 setStyle(cell8); table.addCell(cell8); // 资金性质 第三,四行 PdfPCell cell9 = new PdfPCell(new Phrase("资金性质", font)); cell9.setColspan(1); // 占几列 cell9.setRowspan(2); // 占几行 setStyle(cell9); table.addCell(cell9); // 编码 第三,四行 PdfPCell cell11 = new PdfPCell(new Phrase("编码", font)); cell11.setColspan(1); // 占几列 cell11.setRowspan(2); // 占几行 setStyle(cell11); table.addCell(cell11); // 名称 第三,四行 PdfPCell cell12 = new PdfPCell(new Phrase("名称", font)); cell12.setColspan(1); // 占几列 cell12.setRowspan(2); // 占几行 setStyle(cell12); table.addCell(cell12); // 项目名称 第三,四行 PdfPCell cell13 = new PdfP 1e09 Cell(new Phrase("项目名称", font)); cell13.setColspan(1); // 占几列 cell13.setRowspan(2); // 占几行 setStyle(cell13); table.addCell(cell13); // 姓名 第三,四行 PdfPCell cell14 = new PdfPCell(new Phrase("姓名", font)); cell14.setColspan(1); // 占几列 cell14.setRowspan(2); // 占几行 setStyle(cell14); table.addCell(cell14); // 银行账号 第三,四行 PdfPCell cell15 = new PdfPCell(new Phrase("银行账号", font)); cell15.setColspan(1); // 占几列 cell15.setRowspan(2); // 占几行 setStyle(cell15); table.addCell(cell15); // 开户银行 第三,四行 PdfPCell cell16 = new PdfPCell(new Phrase("开户银行", font)); cell16.setColspan(1); // 占几列 cell16.setRowspan(2); // 占几行 setStyle(cell16); table.addCell(cell16); // 申请金额 第三,四行 PdfPCell cell17 = new PdfPCell(new Phrase("申请金额", font)); cell17.setColspan(1); // 占几列 cell17.setRowspan(2); // 占几行 setStyle(cell17); table.addCell(cell17); // 核定金额 第三,四行 PdfPCell cell18 = new PdfPCell(new Phrase("核定金额", font)); cell18.setColspan(1); // 占几列 cell18.setRowspan(2); // 占几行 setStyle(cell18); table.addCell(cell18); // 根据自己业务封装数据 for (int i = 0; i < 8; i++) { for (int j = 0; j < 10; j++) { PdfPCell cell19 = new PdfPCell(new Phrase("坑位置"+i+j, font)); cell19.setColspan(1); // 占几列 cell19.setRowspan(1); // 占几行 setStyle(cell19); table.addCell(cell19); } } return table; } /** * 设置样式 * @param cell */ private void setStyle(PdfPCell cell){ cell.setFixedHeight(size); cell.setHorizontalAlignment(Element.ALIGN_CENTER);//设置水平居中 cell.setVerticalAlignment(Element.ALIGN_MIDDLE);//设置垂直居中 }
Map遍历与List排序
@Test public void testSort(){ HashMap<String, Integer> items = new HashMap<>(); items.put("coins", 5); items.put("pens", 2); items.put("chairs", 7); items.forEach((key, value) -> System.out.println(key + " " + value)); for (Map.Entry<String, Integer> item : items.entrySet()){ System.out.println(item.getKey() + " " + item.getValue()); } List<Content> contentList = new ArrayList<>(); contentList.add(new Content("A", "A", "A")); contentList.add(new Content("B", "B", "B")); contentList.add(new Content("C", "C", "C")); contentList.sort(Comparator.comparing(Content::getPrice).reversed()); contentList.stream().forEach(content -> { System.out.println(content); }); }
相关文章推荐
- springboot整合itextpdf导出pdf
- springboot整合pdf.js用文件流预览本地磁盘pdf文件
- spring-boot使用itext与jfree导出pdf报表
- springboot itext下载pdf
- itext-asian 生成pdf 之spring boot
- Spring Boot 菜鸟教程 18 itext导出pdf下载
- SpringBoot整合OSS上传图片
- spring boot 整合caffeine
- springBoot整合swagger,开启x-frame-options,跨域访问,解决ifrom嵌套问题
- Springboot学习笔记1之Springboot整合Swagger2
- spring boot整合shiro安全框架过程解析
- SpringBoot整合Netty并使用Protobuf进行数据传输(附工程)
- SpringBoot 2 快速整合 Filter过程解析
- springboot整合mybatis将sql打印到日志
- 基于springboot实现整合shiro实现登录认证以及授权过程解析
- ES7SpringDataElasticsearch配置 最完整的springboot2.2.x.RELEASE整合springDataElasticsearch Elasticsea
- springBoot整合mybatis-plus关闭自动转换小驼峰命名规则
- SpringBoot学习:整合shiro(验证码功能和登录次数限制功能)
- spring boot 2.x 系列 —— spring boot 整合 servlet 3.0
- springboot整合mybatis增删改查(二):springboot热部署