1

I'm trying to merge documents side by side with PDFBox, using the following code:

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();

        // Create output PDF frame
        PDRectangle pdf1Frame = pdf1.getPage(0).getCropBox();
        PDRectangle pdf2Frame = pdf2.getPage(0).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, 0);
        PDFormXObject formPdf2 = layerUtility.importPageAsForm(pdf2, 0);

        // Add form objects to output page
        AffineTransform afLeft = new AffineTransform();
        layerUtility.appendFormAsLayer(outPdfPage, formPdf1, afLeft, "left");
        AffineTransform afRight = AffineTransform.getTranslateInstance(pdf1Frame.getWidth(), 0.0);
        layerUtility.appendFormAsLayer(outPdfPage, formPdf2, afRight, "right");

        outPdf.save(outPdfFile);
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            if (pdf1 != null) pdf1.close();
            if (pdf2 != null) pdf2.close();
            if (outPdf != null) outPdf.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

However the form fields contained in the original documents are not displayed in the final PDF. I also tried to set the acroform on the final document, doing:

outDoc.getDocumentCatalog().setAcroForm(acroForm); 

but it doesn't work.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
  • I haven't really understood the question, but "forms" can mean two different things in PDF, and yes, this is confusing. AcroForm is about fields that can be filled. XObject Forms are PDF segments with a content stream, resources, etc. (but not a page). The LayerUtility has nothing to do with AcroForm. Please include your files and mention what PDFBox version you're using. – Tilman Hausherr Feb 01 '21 at 10:33
  • I try to explain the problem better: I want to merge documents side by side and to do that I'm using the code i posted. It works for almost all PDFs, however I have problems with those including AcroForm, because the fields are not displayed. I'm using version 2.0.5. Here the documents I'm trying to merge: https://gofile.io/d/6cVceP – Luca Nassano Feb 01 '21 at 11:06
  • 1
    Using `layerUtility.importPageAsForm` ignores all interactive features. – mkl Feb 01 '21 at 11:35
  • 1
    Is there a way to merge two documents side by side, including also the interactive features ? – Luca Nassano Feb 01 '21 at 14:42
  • Not out of the box. You would have to add all the fields into the acroform, then reposition all the fields. Also add the annotations to the page. And also rename fields if they already exist. Maybe even more. – Tilman Hausherr Feb 02 '21 at 10:46

0 Answers0