LibrePDF / OpenPDF

OpenPDF is a free Java library for creating and editing PDF files, with a LGPL and MPL open source license. OpenPDF is based on a fork of iText. We welcome contributions from other developers. Please feel free to submit pull-requests and bugreports to this GitHub repository.
Other
3.57k stars 587 forks source link

Merging pdfs doesn't merge the acro form fields #832

Open TheSuperBeaver opened 2 years ago

TheSuperBeaver commented 2 years ago

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

sa-sh commented 7 months ago

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