为了账号安全,请及时绑定邮箱和手机立即绑定

PDFBox 将 2 个 pdf 文件与 java 并排合并

PDFBox 将 2 个 pdf 文件与 java 并排合并

GCT1015 2021-10-28 17:14:12
我比较了 2 个 pdf 文件并在它们上标记突出显示。当我使用 pdfbox 合并它进行比较时。它有错误缺少突出显示。我使用此功能: 将 2 个文件 pdf 与它们的所有页面并排合并的功能。function void generateSideBySidePDF() {    File pdf1File = new File(FILE1_PATH);    File pdf2File = new File(FILE2_PATH);    File outPdfFile = new File(OUTFILE_PATH);    PDDocument pdf1 = null;    PDDocument pdf2 = null;    PDDocument outPdf = null;    try {        pdf1 = PDDocument.load(pdf1File);        pdf2 = PDDocument.load(pdf2File);        outPdf = new PDDocument();        for(int pageNum = 0; pageNum < pdf1.getNumberOfPages(); pageNum++) {            // Create output PDF frame            PDRectangle pdf1Frame = pdf1.getPage(pageNum).getCropBox();            PDRectangle pdf2Frame = pdf2.getPage(pageNum).getCropBox();            PDRectangle outPdfFrame = new PDRectangle(pdf1Frame.getWidth()+pdf2Frame.getWidth(), Math.max(pdf1Frame.getHeight(), pdf2Frame.getHeight()));            // Create output page with calculated frame and add it to the document            COSDictionary dict = new COSDictionary();            dict.setItem(COSName.TYPE, COSName.PAGE);            dict.setItem(COSName.MEDIA_BOX, outPdfFrame);            dict.setItem(COSName.CROP_BOX, outPdfFrame);            dict.setItem(COSName.ART_BOX, outPdfFrame);            PDPage outPdfPage = new PDPage(dict);            outPdf.addPage(outPdfPage);            // Source PDF pages has to be imported as form XObjects to be able to insert them at a specific point in the output page            LayerUtility layerUtility = new LayerUtility(outPdf);            PDFormXObject formPdf1 = layerUtility.importPageAsForm(pdf1, pageNum);            PDFormXObject formPdf2 = layerUtility.importPageAsForm(pdf2, pageNum);    }}
查看完整描述

1 回答

  • 1 回答
  • 0 关注
  • 283 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信