Package org.apache.pdfbox.multipdf
Class PDFMergerUtility
java.lang.Object
org.apache.pdfbox.multipdf.PDFMergerUtility
This class will take a list of pdf documents and merge them, saving the
result in a new document.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic enumThe mode to use when merging AcroForm between documents:PDFMergerUtility.AcroFormMergeMode.JOIN_FORM_FIELDS_MODEfields with the same fully qualified name will be merged into one with the widget annotations of the merged fields becoming part of the same field.static enumThe mode to use when merging documents:PDFMergerUtility.DocumentMergeMode.OPTIMIZE_RESOURCES_MODEOptimizes resource handling such as closing documents early. -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate PDDocumentInformationprivate Stringprivate PDMetadataprivate OutputStreamprivate booleanprivate static final org.apache.commons.logging.LogLog instance.private int -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprivate voidacroFormJoinFieldsMode(PDFCloneUtility cloner, PDAcroForm destAcroForm, PDAcroForm srcAcroForm) private voidacroFormLegacyMode(PDFCloneUtility cloner, PDAcroForm destAcroForm, PDAcroForm srcAcroForm) voidAdd a source file to the list of files to merge.voidAdd a source file to the list of files to merge.voidaddSource(RandomAccessRead source) Add a source to the list of documents to merge.voidaddSources(List<RandomAccessRead> sourcesList) Add a list of sources to the list of documents to merge.voidappendDocument(PDDocument destination, PDDocument source) append all pages from source to destination.Get the merge mode to be used for merging AcroForms between documentsPDFMergerUtility.AcroFormMergeModeGet the destination document information that is to be set inmergeDocuments(org.apache.pdfbox.io.RandomAccessStreamCache.StreamCacheCreateFunction).Get the name of the destination file.Set the destination metadata that is to be set inmergeDocuments(org.apache.pdfbox.io.RandomAccessStreamCache.StreamCacheCreateFunction).Get the destination OutputStream.Get the merge mode to be used for merging documentsPDFMergerUtility.DocumentMergeMode(package private) static Map<String, PDStructureElement> (package private) static Map<Integer, COSObjectable> private booleanhasOnlyDocumentsOrParts(COSArray kLevelOneArray) private booleanisDynamicXfa(PDAcroForm acroForm) Test for dynamic XFA content.booleanIndicates if acroform errors are ignored or not.private voidlegacyMergeDocuments(RandomAccessStreamCache.StreamCacheCreateFunction streamCacheCreateFunction, CompressParameters compressParameters) Merge the list of source documents, saving the result in the destination file.private voidmergeAcroForm(PDFCloneUtility cloner, PDDocumentCatalog destCatalog, PDDocumentCatalog srcCatalog) voidmergeDocuments(RandomAccessStreamCache.StreamCacheCreateFunction streamCacheCreateFunction) Merge the list of source documents, saving the result in the destination file.voidmergeDocuments(RandomAccessStreamCache.StreamCacheCreateFunction streamCacheCreateFunction, CompressParameters compressParameters) Merge the list of source documents, saving the result in the destination file.private voidmergeIDTree(PDFCloneUtility cloner, PDStructureTreeRoot srcStructTree, PDStructureTreeRoot destStructTree) private voidmergeInto(COSDictionary src, COSDictionary dst, 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.private voidmergeKEntries(PDFCloneUtility cloner, PDStructureTreeRoot srcStructTree, PDStructureTreeRoot destStructTree) private voidmergeLanguage(PDDocumentCatalog destCatalog, PDDocumentCatalog srcCatalog) private voidmergeMarkInfo(PDDocumentCatalog destCatalog, PDDocumentCatalog srcCatalog) private voidmergeOutputIntents(PDFCloneUtility cloner, PDDocumentCatalog srcCatalog, PDDocumentCatalog destCatalog) private voidmergeRoleMap(PDStructureTreeRoot srcStructTree, PDStructureTreeRoot destStructTree) private voidmergeViewerPreferences(PDDocumentCatalog destCatalog, PDDocumentCatalog srcCatalog) private voidoptimizedMergeDocuments(RandomAccessStreamCache.StreamCacheCreateFunction streamCacheCreateFunction, CompressParameters compressParameters) voidsetAcroFormMergeMode(PDFMergerUtility.AcroFormMergeMode theAcroFormMergeMode) Set the merge mode to be used for merging AcroForms between documentsPDFMergerUtility.AcroFormMergeModevoidSet the destination document information that is to be set inmergeDocuments(org.apache.pdfbox.io.RandomAccessStreamCache.StreamCacheCreateFunction).voidsetDestinationFileName(String destination) Set the name of the destination file.voidSet the destination metadata that is to be set inmergeDocuments(org.apache.pdfbox.io.RandomAccessStreamCache.StreamCacheCreateFunction).voidsetDestinationStream(OutputStream destStream) Set the destination OutputStream.voidsetDocumentMergeMode(PDFMergerUtility.DocumentMergeMode theDocumentMergeMode) Set the merge mode to be used for merging documentsPDFMergerUtility.DocumentMergeModevoidsetIgnoreAcroFormErrors(boolean ignoreAcroFormErrorsValue) Set to true to ignore acroform errors.private voidupdatePageReferences(PDFCloneUtility cloner, Map<Integer, COSObjectable> numberTreeAsMap, Map<COSDictionary, COSDictionary> objMapping) Update the Pg and Obj references to the new (merged) page.private voidupdatePageReferences(PDFCloneUtility cloner, COSArray parentTreeEntry, Map<COSDictionary, COSDictionary> objMapping) private voidupdatePageReferences(PDFCloneUtility cloner, COSDictionary parentTreeEntry, Map<COSDictionary, COSDictionary> objMapping) Update the Pg and Obj references to the new (merged) page.private voidupdateParentEntry(COSArray kArray, COSDictionary newParent, COSName newStructureType) Update the P reference to the new parent dictionary.private voidupdateStructParentEntries(PDPage page, int structParentOffset) Update the StructParents and StructParent values in a PDPage.
-
Field Details
-
LOG
private static final org.apache.commons.logging.Log LOGLog instance. -
sources
-
destinationFileName
-
destinationStream
-
ignoreAcroFormErrors
private boolean ignoreAcroFormErrors -
destinationDocumentInformation
-
destinationMetadata
-
documentMergeMode
-
acroFormMergeMode
-
nextFieldNum
private int nextFieldNum
-
-
Constructor Details
-
PDFMergerUtility
public PDFMergerUtility()Instantiate a new PDFMergerUtility.
-
-
Method Details
-
getAcroFormMergeMode
Get the merge mode to be used for merging AcroForms between documentsPDFMergerUtility.AcroFormMergeMode- Returns:
- the current AcroFormMergeMode
-
setAcroFormMergeMode
Set the merge mode to be used for merging AcroForms between documentsPDFMergerUtility.AcroFormMergeMode- Parameters:
theAcroFormMergeMode- AcroFormMergeMode to be used
-
getDocumentMergeMode
Get the merge mode to be used for merging documentsPDFMergerUtility.DocumentMergeMode- Returns:
- the current DocumentMergeMode
-
setDocumentMergeMode
Set the merge mode to be used for merging documentsPDFMergerUtility.DocumentMergeMode- Parameters:
theDocumentMergeMode- DocumentMergeMode to be used
-
getDestinationFileName
Get the name of the destination file.- Returns:
- Returns the destination.
-
setDestinationFileName
Set the name of the destination file.- Parameters:
destination- The destination to set.
-
getDestinationStream
Get the destination OutputStream.- Returns:
- Returns the destination OutputStream.
-
setDestinationStream
Set the destination OutputStream.- Parameters:
destStream- The destination to set.
-
getDestinationDocumentInformation
Get the destination document information that is to be set inmergeDocuments(org.apache.pdfbox.io.RandomAccessStreamCache.StreamCacheCreateFunction). The default is null, which means that it is ignored.- Returns:
- The destination document information.
-
setDestinationDocumentInformation
Set the destination document information that is to be set inmergeDocuments(org.apache.pdfbox.io.RandomAccessStreamCache.StreamCacheCreateFunction). The default is null, which means that it is ignored.- Parameters:
info- The destination document information.
-
getDestinationMetadata
Set the destination metadata that is to be set inmergeDocuments(org.apache.pdfbox.io.RandomAccessStreamCache.StreamCacheCreateFunction). The default is null, which means that it is ignored.- Returns:
- The destination metadata.
-
setDestinationMetadata
Set the destination metadata that is to be set inmergeDocuments(org.apache.pdfbox.io.RandomAccessStreamCache.StreamCacheCreateFunction). The default is null, which means that it is ignored.- Parameters:
meta- The destination metadata.
-
addSource
Add a source file to the list of files to merge.- Parameters:
source- Full path and file name of source document.- Throws:
FileNotFoundException- If the file doesn't exist
-
addSource
Add a source file to the list of files to merge.- Parameters:
source- File representing source document- Throws:
FileNotFoundException- If the file doesn't exist
-
addSource
Add a source to the list of documents to merge.- Parameters:
source- RandomAccessRead representing source document
-
addSources
Add a list of sources to the list of documents to merge.- Parameters:
sourcesList- List of RandomAccessRead objects representing source documents
-
mergeDocuments
public void mergeDocuments(RandomAccessStreamCache.StreamCacheCreateFunction streamCacheCreateFunction) throws IOException Merge the list of source documents, saving the result in the destination file. The source list is not reset after merge. If you want to merge one document at a time, then it's better to useappendDocument(org.apache.pdfbox.pdmodel.PDDocument, org.apache.pdfbox.pdmodel.PDDocument).- Parameters:
streamCacheCreateFunction- a function to create an instance of a stream cache; in case ofnullunrestricted main memory is used- Throws:
IOException- If there is an error saving the document.
-
mergeDocuments
public void mergeDocuments(RandomAccessStreamCache.StreamCacheCreateFunction streamCacheCreateFunction, CompressParameters compressParameters) throws IOException Merge the list of source documents, saving the result in the destination file. The source list is not reset after merge. If you want to merge one document at a time, then it's better to useappendDocument(org.apache.pdfbox.pdmodel.PDDocument, org.apache.pdfbox.pdmodel.PDDocument).- Parameters:
streamCacheCreateFunction- a function to create an instance of a stream cache; in case ofnullunrestricted main memory is usedcompressParameters- defines if compressed object streams are enabled- Throws:
IOException- If there is an error saving the document.
-
optimizedMergeDocuments
private void optimizedMergeDocuments(RandomAccessStreamCache.StreamCacheCreateFunction streamCacheCreateFunction, CompressParameters compressParameters) throws IOException - Throws:
IOException
-
legacyMergeDocuments
private void legacyMergeDocuments(RandomAccessStreamCache.StreamCacheCreateFunction streamCacheCreateFunction, CompressParameters compressParameters) throws IOException Merge the list of source documents, saving the result in the destination file.- Parameters:
streamCacheCreateFunction- a function to create an instance of a stream cache; in case ofnullunrestricted main memory is used- Throws:
IOException- If there is an error saving the document.
-
appendDocument
append all pages from source to destination.- Parameters:
destination- the document to receive the pagessource- the document originating the new pages- Throws:
IOException- If there is an error accessing data from either document.
-
mergeViewerPreferences
-
mergeLanguage
-
mergeMarkInfo
-
mergeKEntries
private void mergeKEntries(PDFCloneUtility cloner, PDStructureTreeRoot srcStructTree, PDStructureTreeRoot destStructTree) throws IOException - Throws:
IOException
-
hasOnlyDocumentsOrParts
-
updateParentEntry
Update the P reference to the new parent dictionary.- Parameters:
kArray- the kids arraynewParent- the new parentnewStructureType- the new structure type in /S or null so it doesn't get replaced
-
mergeIDTree
private void mergeIDTree(PDFCloneUtility cloner, PDStructureTreeRoot srcStructTree, PDStructureTreeRoot destStructTree) throws IOException - Throws:
IOException
-
getIDTreeAsMap
static Map<String,PDStructureElement> getIDTreeAsMap(PDNameTreeNode<PDStructureElement> idTree) throws IOException - Throws:
IOException
-
getNumberTreeAsMap
- Throws:
IOException
-
mergeRoleMap
-
mergeAcroForm
private void mergeAcroForm(PDFCloneUtility cloner, PDDocumentCatalog destCatalog, PDDocumentCatalog srcCatalog) throws IOException - Throws:
IOException
-
acroFormJoinFieldsMode
private void acroFormJoinFieldsMode(PDFCloneUtility cloner, PDAcroForm destAcroForm, PDAcroForm srcAcroForm) throws IOException - Throws:
IOException
-
acroFormLegacyMode
private void acroFormLegacyMode(PDFCloneUtility cloner, PDAcroForm destAcroForm, PDAcroForm srcAcroForm) throws IOException - Throws:
IOException
-
mergeOutputIntents
private void mergeOutputIntents(PDFCloneUtility cloner, PDDocumentCatalog srcCatalog, PDDocumentCatalog destCatalog) throws IOException - Throws:
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, Map<Integer, COSObjectable> numberTreeAsMap, Map<COSDictionary, throws IOExceptionCOSDictionary> objMapping) Update the Pg and Obj references to the new (merged) page.- Throws:
IOException
-
updatePageReferences
private void updatePageReferences(PDFCloneUtility cloner, COSDictionary parentTreeEntry, Map<COSDictionary, COSDictionary> objMapping) throws IOExceptionUpdate the Pg and Obj references to the new (merged) page.- Parameters:
parentTreeEntry-objMapping- mapping between old and new references- Throws:
IOException
-
updatePageReferences
private void updatePageReferences(PDFCloneUtility cloner, COSArray parentTreeEntry, Map<COSDictionary, COSDictionary> objMapping) throws IOException- Throws:
IOException
-
updateStructParentEntries
Update the StructParents and StructParent values in a PDPage.- Parameters:
page- the new pagestructParentOffset- the offset which should be applied- Throws:
IOException
-
isDynamicXfa
Test for dynamic XFA content.- Parameters:
acroForm- the AcroForm- Returns:
- true if there is a dynamic XFA form.
-
mergeInto
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.
-