1、引入依赖(最后一个支持java8的版本)

<dependency>
     <groupId>com.github.librepdf</groupId>
     <artifactId>openpdf</artifactId>
     <version>1.3.34</version>
</dependency>
<dependency>
     <groupId>com.github.librepdf</groupId>
     <artifactId>openpdf-fonts-extra</artifactId>
     <version>1.3.34</version>
</dependency>

2、编码

 String outputPath =  "d:\\pdf" + File.separator;
        File file = new File(outputPath);
        if (!file.exists()) {
            file.mkdir();
        }
        outputPath = outputPath + "hello.pdf";

        FileOutputStream fileOutputStream = new FileOutputStream(outputPath);

        // 设置字体
        BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
        Font titleFont = new Font(bfChinese, 13, Font.BOLD, Color.BLACK);
        Font docFont = new Font(bfChinese, 10, Font.UNDEFINED, Color.BLACK);

        Document document = new Document(PageSize.A4);
        PdfWriter.getInstance(document, fileOutputStream);
        document.open();

        // 初始化一个4列的表格,超过自动换行
        PdfPTable table = new PdfPTable(4);
        table.setWidthPercentage(100f);
        table.setSpacingAfter(20f);

        Paragraph paragraph = new Paragraph("检查表名称", titleFont);
        PdfPCell cell = new PdfPCell(paragraph);
        cell.setFixedHeight(20F);
        cell.setHorizontalAlignment(Element.ALIGN_CENTER); // 设置内容水平居中显示
        cell.setVerticalAlignment(Element.ALIGN_CENTER); // 设置垂直对齐
        cell.setColspan(4);
        table.addCell(cell);

        paragraph = new Paragraph("检查人", docFont);
        cell = new PdfPCell(paragraph);
        cell.setFixedHeight(20F);
        cell.setHorizontalAlignment(Element.ALIGN_CENTER); // 设置内容水平居中显示
        cell.setVerticalAlignment(Element.ALIGN_CENTER); // 设置垂直对齐
        table.addCell(cell);

        cell = new PdfPCell(new Paragraph(taskResp.getOrgUserName(), docFont));
        cell.setFixedHeight(20F);
        cell.setHorizontalAlignment(Element.ALIGN_CENTER); // 设置内容水平居中显示
        cell.setVerticalAlignment(Element.ALIGN_CENTER); // 设置垂直对齐
        table.addCell(cell);

        cell = new PdfPCell(new Paragraph("检查时间", docFont));
        cell.setFixedHeight(20F);
        cell.setHorizontalAlignment(Element.ALIGN_CENTER); // 设置内容水平居中显示
        cell.setVerticalAlignment(Element.ALIGN_CENTER); // 设置垂直对齐
        table.addCell(cell);

        cell = new PdfPCell(new Paragraph(TimeUtil.get_yyyyMMddHHmmss(taskResp.getCompleteTime()), docFont));
        cell.setFixedHeight(20F);
        cell.setHorizontalAlignment(Element.ALIGN_CENTER); // 设置内容水平居中显示
        cell.setVerticalAlignment(Element.ALIGN_CENTER); // 设置垂直对齐
        table.addCell(cell);


        cell = new PdfPCell(new Paragraph("检查事项", docFont));
        cell.setFixedHeight(20F);
        cell.setHorizontalAlignment(Element.ALIGN_CENTER); // 设置内容水平居中显示
        cell.setVerticalAlignment(Element.ALIGN_CENTER); // 设置垂直对齐
        table.addCell(cell);

        cell = new PdfPCell(new Paragraph("检查内容", docFont));
        cell.setFixedHeight(20F);
        cell.setColspan(2);
        cell.setHorizontalAlignment(Element.ALIGN_CENTER); // 设置内容水平居中显示
        cell.setVerticalAlignment(Element.ALIGN_CENTER); // 设置垂直对齐
        table.addCell(cell);

        cell = new PdfPCell(new Paragraph("检查意见", docFont));
        cell.setFixedHeight(20F);
        cell.setHorizontalAlignment(Element.ALIGN_CENTER); // 设置内容水平居中显示
        cell.setVerticalAlignment(Element.ALIGN_CENTER); // 设置垂直对齐
        table.addCell(cell);

         //  循环处理数据,可删除
        for (InspectItemMoldResp inspectItemMoldResp : inspectItemResp.getMoldList()) {
            for (int i = 0; i < inspectItemMoldResp.getPatternList().size(); i++) {
                if (i == 0) {
                    cell = new PdfPCell(new Paragraph(inspectItemMoldResp.getName(), docFont));
                    // cell.setFixedHeight(inspectItemMoldResp.getPatternList().size() * 20F);
                    cell.setRowspan(inspectItemMoldResp.getPatternList().size());
                    cell.setHorizontalAlignment(Element.ALIGN_CENTER); // 设置内容水平居中显示
                    cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // 设置居中对齐
                    table.addCell(cell);

                    cell = new PdfPCell(new Paragraph(inspectItemMoldResp.getPatternList().get(i).getName(), docFont));
                    cell.setFixedHeight(20F);
                    cell.setHorizontalAlignment(Element.ALIGN_LEFT);
                    cell.setColspan(2);
                    table.addCell(cell);

                    cell = new PdfPCell(new Paragraph(
                            StringUtils.join(inspectItemMoldResp.getPatternList().get(i).getDeployList()
                                    .stream().filter(f -> EnumDict.Judge.YES.getKey() == f.getOptionStatus())
                                    .map(InspectItemDeployResp::getName).collect(Collectors.toList()), ","), docFont));
                    cell.setFixedHeight(20F);
                    cell.setHorizontalAlignment(Element.ALIGN_LEFT);
                    table.addCell(cell);

                } else {
                    cell = new PdfPCell(new Paragraph(inspectItemMoldResp.getPatternList().get(i).getName(), docFont));
                    cell.setFixedHeight(20F);
                    cell.setHorizontalAlignment(Element.ALIGN_LEFT);
                    cell.setColspan(2);
                    table.addCell(cell);

                    cell = new PdfPCell(new Paragraph(
                            StringUtils.join(inspectItemMoldResp.getPatternList().get(i).getDeployList()
                                    .stream().filter(f -> EnumDict.Judge.YES.getKey() == f.getOptionStatus())
                                    .map(InspectItemDeployResp::getName).collect(Collectors.toList()), ","), docFont));
                    cell.setFixedHeight(20F);
                    cell.setHorizontalAlignment(Element.ALIGN_LEFT);
                    table.addCell(cell);
                }
            }
        }

        document.add(table);
        document.close();// 关闭文档
        fileOutputStream.flush();
        fileOutputStream.close();

点赞(0) 打赏

评论列表 共有 0 条评论

暂无评论

微信公众账号

微信扫一扫加关注

发表
评论
返回
顶部