Class PDFMergerUtility


  • public class PDFMergerUtility
    extends java.lang.Object
    This class will take a list of pdf documents and merge them, saving the result in a new document.
    • Field Detail

      • LOG

        private static final org.apache.commons.logging.Log LOG
        Log instance.
      • sources

        private final java.util.List<java.lang.Object> sources
      • destinationFileName

        private java.lang.String destinationFileName
      • destinationStream

        private java.io.OutputStream destinationStream
      • ignoreAcroFormErrors

        private boolean ignoreAcroFormErrors
      • destinationMetadata

        private PDMetadata destinationMetadata
      • nextFieldNum

        private int nextFieldNum
    • Constructor Detail

      • PDFMergerUtility

        public PDFMergerUtility()
        Instantiate a new PDFMergerUtility.
    • Method Detail

      • getDestinationFileName

        public java.lang.String getDestinationFileName()
        Get the name of the destination file.
        Returns:
        Returns the destination.
      • setDestinationFileName

        public void setDestinationFileName​(java.lang.String destination)
        Set the name of the destination file.
        Parameters:
        destination - The destination to set.
      • getDestinationStream

        public java.io.OutputStream getDestinationStream()
        Get the destination OutputStream.
        Returns:
        Returns the destination OutputStream.
      • setDestinationStream

        public void setDestinationStream​(java.io.OutputStream destStream)
        Set the destination OutputStream.
        Parameters:
        destStream - The destination to set.
      • addSource

        public void addSource​(java.lang.String source)
                       throws java.io.FileNotFoundException
        Add a source file to the list of files to merge.
        Parameters:
        source - Full path and file name of source document.
        Throws:
        java.io.FileNotFoundException - If the file doesn't exist
      • addSource

        public void addSource​(java.io.File source)
                       throws java.io.FileNotFoundException
        Add a source file to the list of files to merge.
        Parameters:
        source - File representing source document
        Throws:
        java.io.FileNotFoundException - If the file doesn't exist
      • addSource

        public void addSource​(java.io.InputStream source)
        Add a source to the list of documents to merge.
        Parameters:
        source - InputStream representing source document
      • addSources

        public void addSources​(java.util.List<java.io.InputStream> sourcesList)
        Add a list of sources to the list of documents to merge.
        Parameters:
        sourcesList - List of InputStream objects representing source documents
      • mergeDocuments

        @Deprecated
        public void mergeDocuments()
                            throws java.io.IOException
        Merge the list of source documents, saving the result in the destination file.
        Throws:
        java.io.IOException - If there is an error saving the document.
      • mergeDocuments

        public void mergeDocuments​(MemoryUsageSetting memUsageSetting)
                            throws java.io.IOException
        Merge the list of source documents, saving the result in the destination file.
        Parameters:
        memUsageSetting - defines how memory is used for buffering PDF streams; in case of null unrestricted main memory is used
        Throws:
        java.io.IOException - If there is an error saving the document.
      • optimizedMergeDocuments

        private void optimizedMergeDocuments​(MemoryUsageSetting memUsageSetting)
                                      throws java.io.IOException
        Throws:
        java.io.IOException
      • legacyMergeDocuments

        private void legacyMergeDocuments​(MemoryUsageSetting memUsageSetting)
                                   throws java.io.IOException
        Merge the list of source documents, saving the result in the destination file.
        Parameters:
        memUsageSetting - defines how memory is used for buffering PDF streams; in case of null unrestricted main memory is used
        Throws:
        java.io.IOException - If there is an error saving the document.
      • appendDocument

        public void appendDocument​(PDDocument destination,
                                   PDDocument source)
                            throws java.io.IOException
        append all pages from source to destination.
        Parameters:
        destination - the document to receive the pages
        source - the document originating the new pages
        Throws:
        java.io.IOException - If there is an error accessing data from either document.
      • getNumberTreeAsMap

        static java.util.Map<java.lang.Integer,​COSObjectable> getNumberTreeAsMap​(PDNumberTreeNode tree)
                                                                                throws java.io.IOException
        Throws:
        java.io.IOException
      • mergeAcroForm

        private void mergeAcroForm​(PDFCloneUtility cloner,
                                   PDDocumentCatalog destCatalog,
                                   PDDocumentCatalog srcCatalog)
                            throws java.io.IOException
        Merge the contents of the source form into the destination form for the destination file.
        Parameters:
        cloner - the object cloner for the destination document
        destAcroForm - the destination form
        srcAcroForm - the source form
        Throws:
        java.io.IOException - If an error occurs while adding the field.
      • acroFormJoinFieldsMode

        private void acroFormJoinFieldsMode​(PDFCloneUtility cloner,
                                            PDAcroForm destAcroForm,
                                            PDAcroForm srcAcroForm)
                                     throws java.io.IOException
        Throws:
        java.io.IOException
      • cleanupFieldCOSDictionary

        private void cleanupFieldCOSDictionary​(COSDictionary fieldCos)
      • cleanupWidgetCOSDictionary

        private void cleanupWidgetCOSDictionary​(COSDictionary widgetCos,
                                                boolean removeDAEntry)
      • acroFormLegacyMode

        private void acroFormLegacyMode​(PDFCloneUtility cloner,
                                        PDAcroForm destAcroForm,
                                        PDAcroForm srcAcroForm)
                                 throws java.io.IOException
        Throws:
        java.io.IOException
      • isIgnoreAcroFormErrors

        public boolean isIgnoreAcroFormErrors()
        Indicates if acroform errors are ignored or not.
        Returns:
        true if acroform errors are ignored
      • setIgnoreAcroFormErrors

        public void setIgnoreAcroFormErrors​(boolean ignoreAcroFormErrorsValue)
        Set to true to ignore acroform errors.
        Parameters:
        ignoreAcroFormErrorsValue - true if acroform errors should be ignored
      • updatePageReferences

        private void updatePageReferences​(PDFCloneUtility cloner,
                                          java.util.Map<java.lang.Integer,​COSObjectable> numberTreeAsMap,
                                          java.util.Map<COSDictionary,​COSDictionary> objMapping)
                                   throws java.io.IOException
        Update the Pg and Obj references to the new (merged) page.
        Throws:
        java.io.IOException
      • updatePageReferences

        private void updatePageReferences​(PDFCloneUtility cloner,
                                          COSDictionary parentTreeEntry,
                                          java.util.Map<COSDictionary,​COSDictionary> objMapping)
                                   throws java.io.IOException
        Update the Pg and Obj references to the new (merged) page.
        Parameters:
        parentTreeEntry -
        objMapping - mapping between old and new references
        Throws:
        java.io.IOException
      • updateParentEntry

        private void updateParentEntry​(COSArray kArray,
                                       COSDictionary newParent)
        Update the P reference to the new parent dictionary.
        Parameters:
        kArray - the kids array
        newParent - the new parent
      • updateStructParentEntries

        private void updateStructParentEntries​(PDPage page,
                                               int structParentOffset)
                                        throws java.io.IOException
        Update the StructParents and StructParent values in a PDPage.
        Parameters:
        page - the new page
        structParentOffset - the offset which should be applied
        Throws:
        java.io.IOException
      • isDynamicXfa

        private boolean isDynamicXfa​(PDAcroForm acroForm)
        Test for dynamic XFA content.
        Parameters:
        acroForm - the AcroForm
        Returns:
        true if there is a dynamic XFA form.
      • mergeInto

        private void mergeInto​(COSDictionary src,
                               COSDictionary dst,
                               java.util.Set<COSName> exclude)
        This will add all of the dictionaries keys/values to this dictionary, but only if they are not in an exclusion list and if they don't already exist. If a key already exists in this dictionary then nothing is changed.
        Parameters:
        src - The source dictionary to get the keys/values from.
        dst - The destination dictionary to merge the keys/values into.
        exclude - Names of keys that shall be skipped.