OpenPDF icon indicating copy to clipboard operation
OpenPDF copied to clipboard

Merging pdfs doesn't merge the acro form fields

Open TheSuperBeaver opened this issue 3 years ago • 1 comments

Describe the bug Using PdfSmartCopy, i'm trying to merge multiple pdfs into one. The generated pdf seems ok, but the acro fields are not recognized.

To Reproduce

    public static byte[] merge(final byte[]... byteArrays) {
        try {
            ByteArrayOutputStream pdfResult = new ByteArrayOutputStream();
            Document document = new Document();            
            PdfSmartCopy pdfCopy = new PdfSmartCopy(document, pdfResult);
            PdfDictionary pdfDictionary = new PdfDictionary();

            document.open();
            for (byte[] bytez : byteArrays) {
                if (bytez != null) {
                    try (PdfReader originalPdfReader = new PdfReader(bytez)) {
                        originalPdfReader.getNumberOfPages();

                        // Copy pages
                        for (int i = 1; i <= originalPdfReader.getNumberOfPages(); i++) {
                            PdfImportedPage importedPage = pdfCopy.getImportedPage(originalPdfReader, i);
                            pdfCopy.addPage(importedPage);
                        }

                        // Copy acro forms
                        PRAcroForm acroForm = originalPdfReader.getAcroForm();
                        if (acroForm != null) {
                            pdfCopy.getAcroForm().merge(acroForm);
                           // Also tried this : 
                            pdfCopy.copyAcroForm(originalPdfReader);
                        }
                        pdfCopy.flush();
                    }
                }
            }

            document.close();

            return pdfResult.toByteArray();

Expected behavior I expect that the fields are in the final pdf, and recognized as such by reader (like acrobat)

Screenshots

System (please complete the following information):

Additional context

In iText, there is the concept of ".setMergeFields();" that fixes this issue, but it's not in openPdf

TheSuperBeaver avatar Oct 05 '22 11:10 TheSuperBeaver

as i know this utit does not support merging of AcroForm from multiple source documents. consider to use PdfCopyFields or PdfCopyForms utils

sa-sh avatar Mar 29 '24 04:03 sa-sh