猿问

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

我比较了 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);

    }

}



GCT1015
浏览 284回答 1
1回答
随时随地看视频慕课网APP

相关分类

Java
我要回答