根据已有PDf,在第一页前新增一页,生成新的PDF文件

使用java生成pdf文件
springboot依赖

<dependency>
            <groupId>com.itextpdf</groupId>
            <artifactId>itextpdf</artifactId>
            <version>5.4.3</version>
        </dependency>
        <dependency>
            <groupId>com.itextpdf</groupId>
            <artifactId>itext-asian</artifactId>
            <version>5.2.0</version>
        </dependency>

已存在一个PDF,在PDF的第一页前增加一页空白,在空白处添加一个表格,表格内数据为自己需要的内容。于是,我就想,直接重新创建一个新的PDF,然后把之前的PDF追加到新的PDF中生成一个新的PDF。

outputStream为已存在的PDF文件出入的输出流。

public static Document copyPdf(HttpServletResponse response, String path, **** ****) {
        //创建输出PDF
        Document document = new Document(PageSize.A4);
        PdfWriter writer = null;
        OutputStream outputStream = null;
        try {
        	outputStream = response.getOutputStream();
            writer = PdfWriter.getInstance(document, outputStream);
            document.open();
            PdfContentByte cb = writer.getDirectContent();
            BaseFont baseFont = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H",
                    BaseFont.NOT_EMBEDDED);
            Font font1 = new Font(baseFont, 16, Font.BOLD);
            Font font2 = new Font(baseFont, 12, Font.NORMAL);
            //生成空白页
            document.newPage();

            // 添加表格,5列
            PdfPTable table = new PdfPTable(5);
            float[] widths = new float[]{200, 200, 250, 200, 250};
            table.setWidths(widths);
            //设置表格宽度比例为%100
            table.setWidthPercentage(100);
            // 设置表格的宽度
            table.setTotalWidth(500);
            // 设置表格默认边框
            table.getDefaultCell().setBorder(1);
            //PdfContentByte cb = writer.getDirectContent();

            // 构建第一个单元格
            PdfPCell cell1 = new PdfPCell(new Paragraph("****", font1));
            // 边框颜色
            cell1.setBorderColor(BaseColor.BLACK);
            // 设置距左边的距离
            cell1.setPaddingLeft(10);
            // 设置高度
            cell1.setFixedHeight(20);

            // 设置内容水平居中显示
            cell1.setHorizontalAlignment(Element.ALIGN_CENTER);
            // 设置垂直居中
            cell1.setVerticalAlignment(Element.ALIGN_MIDDLE);
            table.addCell(cell1);

            // 构建第二个标题
            PdfPCell cell2 = new PdfPCell(new Paragraph("****", font1));
            // 边框颜色
            cell2.setBorderColor(BaseColor.BLACK);
            // 设置距左边的距离
            cell2.setPaddingLeft(10);
            // 设置高度
            cell2.setFixedHeight(20);
            // 设置内容水平居中显示
            cell2.setHorizontalAlignment(Element.ALIGN_CENTER);
            // 设置垂直居中
            cell2.setVerticalAlignment(Element.ALIGN_MIDDLE);
            table.addCell(cell2);

            // 构建第三个单元格
            PdfPCell cell3 = new PdfPCell(new Paragraph("****", font1));
            // 边框颜色
            cell3.setBorderColor(BaseColor.BLACK);
            // 设置距左边的距离
            cell3.setPaddingLeft(10);
            // 设置高度
            cell3.setFixedHeight(20);
            // 设置内容水平居中显示
            cell3.setHorizontalAlignment(Element.ALIGN_CENTER);
            // 设置垂直居中
            cell3.setVerticalAlignment(Element.ALIGN_MIDDLE);
            table.addCell(cell3);


            // 构建第四个单元格
            PdfPCell cell4 = new PdfPCell(new Paragraph("****", font1));
            // 边框颜色
            cell4.setBorderColor(BaseColor.BLACK);
            // 设置距左边的距离
            cell4.setPaddingLeft(10);
            // 设置高度
            cell4.setFixedHeight(20);
            // 设置内容水平居中显示
            cell4.setHorizontalAlignment(Element.ALIGN_CENTER);
            // 设置垂直居中
            cell4.setVerticalAlignment(Element.ALIGN_MIDDLE);
            table.addCell(cell4);

            // 构建第五个单元格
            PdfPCell cell5 = new PdfPCell(new Paragraph("****", font1));
            // 边框颜色
            cell5.setBorderColor(BaseColor.BLACK);
            // 设置距左边的距离
            cell5.setPaddingLeft(10);
            // 设置高度
            cell5.setFixedHeight(20);
            // 设置内容水平居中显示
            cell5.setHorizontalAlignment(Element.ALIGN_CENTER);
            // 设置垂直居中
            cell5.setVerticalAlignment(Element.ALIGN_MIDDLE);
            table.addCell(cell5);

            PdfPCell cell = new PdfPCell(new Paragraph(memberCheck.getMemberCode(), font2));
            // 设置距左边的距离
            cell.setPaddingLeft(10);
            // 设置高度
            cell.setFixedHeight(20);
            // 设置内容水平居中显示
            cell.setHorizontalAlignment(Element.ALIGN_CENTER);
            // 设置垂直居中
            cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
            table.addCell(cell);

            cell = new PdfPCell(new Paragraph(memberCheck.getMemberName(), font2));
            // 设置距左边的距离
            cell.setPaddingLeft(10);
            // 设置高度
            cell.setFixedHeight(20);
            // 设置内容水平居中显示
            cell.setHorizontalAlignment(Element.ALIGN_CENTER);
            // 设置垂直居中
            cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
            table.addCell(cell);

            cell = new PdfPCell(new Paragraph(ApplyTypeEnum.getValueBycode(memberCheck.getType()), font2));
            // 设置距左边的距离
            cell.setPaddingLeft(10);
            // 设置高度
            cell.setFixedHeight(20);
            // 设置内容水平居中显示
            cell.setHorizontalAlignment(Element.ALIGN_CENTER);
            // 设置垂直居中
            cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
            table.addCell(cell);

            cell = new PdfPCell(new Paragraph(ApprovalStatusEnum.getValueByCode(memberCheck.getStatus()), font2));
            // 设置距左边的距离
            cell.setPaddingLeft(10);
            // 设置高度
            cell.setFixedHeight(20);
            // 设置内容水平居中显示
            cell.setHorizontalAlignment(Element.ALIGN_CENTER);
            // 设置垂直居中
            cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
            table.addCell(cell);


            cell = new PdfPCell(new Paragraph(DateUtils.dateFormat(memberCheck.getCheckTime())));
            // 设置距左边的距离
            cell.setPaddingLeft(10);
            // 设置高度
            cell.setFixedHeight(20);
            // 设置内容水平居中显示
            cell.setHorizontalAlignment(Element.ALIGN_CENTER);
            // 设置垂直居中
            cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
            table.addCell(cell);

            document.add(table);

            //此时就创建了一个新的PDF页面,只用了一次document.newPage,所以只有一页。后面开始读取已存在的PDF,并追加到这个PDF中,加载现有PDF

            PdfReader reader = new PdfReader(path);
            PdfImportedPage page;
            for (int i = 1; i <= reader.getNumberOfPages(); i++) {
                page = writer.getImportedPage(reader, i);
                //将现有PDF的第一页追加到新PDF中
                document.newPage();
                cb.addTemplate(page, 0, 0);
            }
            document.close();
        } catch (DocumentException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return document;
    }

前台接收Document之后,也可以使用blob进行下载

let parme = {
                applyId: parseInt(this.downloadInfo.applyId),
                type: parseInt(this.downloadInfo.type),
                status: parseInt(this.search.status)
              };
 this.$axios
        .post("/exportDetails", parme, {
          responseType: "blob",
        })
        .then((res) => {
          const content = res.data;
          const blob = new Blob([content]);
          const fileName = "导出详情.pdf";
          if ("download" in document.createElement("a")) {
            // 非IE下载
            const elink = document.createElement("a");
            elink.download = fileName;
            elink.style.display = "none";
            elink.href = URL.createObjectURL(blob);
            document.body.appendChild(elink);
            elink.click();
            URL.revokeObjectURL(elink.href);
            document.body.removeChild(elink);
          }
        });

参考
https://blog.csdn.net/lingbo89/article/details/76187582

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值