Top Description Inners Fields Constructors Methods
org.apache.pdfbox.multipdf

public Class PDFMergerUtility

extends Object
Class Inheritance
Imports
java.io.File, .FileNotFoundException, .IOException, .OutputStream, java.util.ArrayList, .Arrays, .Collections, .HashMap, .HashSet, .LinkedHashMap, .List, .Map, .Set, org.apache.commons.logging.Log, .LogFactory, org.apache.pdfbox.Loader, org.apache.pdfbox.cos.COSArray, .COSBase, .COSDictionary, .COSInteger, .COSName, .COSNumber, .COSObject, .COSStream, org.apache.pdfbox.io.IOUtils, .RandomAccessRead, .RandomAccessReadBuffer, .RandomAccessStreamCache.StreamCacheCreateFunction, org.apache.pdfbox.pdfwriter.compress.CompressParameters, org.apache.pdfbox.pdmodel.PDDocument, .PDDocumentCatalog, .PDDocumentInformation, .PDDocumentNameDestinationDictionary, .PDDocumentNameDictionary, .PDPage, .PDPageTree, .PDResources, .PDStructureElementNameTreeNode, .PageMode, org.apache.pdfbox.pdmodel.common.COSObjectable, .PDDestinationOrAction, .PDMetadata, .PDNameTreeNode, .PDNumberTreeNode, .PDStream, org.apache.pdfbox.pdmodel.documentinterchange.logicalstructure.PDMarkInfo, .PDParentTreeValue, .PDStructureElement, .PDStructureTreeRoot, org.apache.pdfbox.pdmodel.graphics.color.PDOutputIntent, org.apache.pdfbox.pdmodel.interactive.action.PDActionGoTo, org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotation, org.apache.pdfbox.pdmodel.interactive.documentnavigation.destination.PDDestination, .PDPageDestination, org.apache.pdfbox.pdmodel.interactive.documentnavigation.outline.PDDocumentOutline, .PDOutlineItem, org.apache.pdfbox.pdmodel.interactive.form.PDAcroForm, .PDField, org.apache.pdfbox.pdmodel.interactive.viewerpreferences.PDViewerPreferences

This class will take a list of pdf documents and merge them, saving the result in a new document.
Author
Ben Litchfield

Nested and Inner Type Summary

Modifier and TypeClass and Description
public static enum
PDFMergerUtility.AcroFormMergeMode

The mode to use when merging AcroForm between documents:

public static enum
PDFMergerUtility.DocumentMergeMode

The mode to use when merging documents:

  • DocumentMergeMode#OPTIMIZE_RESOURCES_MODE Optimizes resource handling such as closing documents early. Not all document elements are merged compared to the PDFBOX_LEGACY_MODE. Currently supported are:
    • Page content and resources

Field Summary

Modifier and TypeField and Description
private PDFMergerUtility.AcroFormMergeMode
private PDDocumentInformation
private String
private PDMetadata
private OutputStream
private PDFMergerUtility.DocumentMergeMode
private boolean
private static final Log
LOG

Log instance.

private int
private final List<Object>

Constructor Summary

AccessConstructor and Description
public
PDFMergerUtility()

Instantiate a new PDFMergerUtility.

Method Summary

Modifier and TypeMethod and Description
private void
acroFormJoinFieldsMode(PDFCloneUtility cloner, PDAcroForm destAcroForm, PDAcroForm srcAcroForm)

private void
acroFormLegacyMode(PDFCloneUtility cloner, PDAcroForm destAcroForm, PDAcroForm srcAcroForm)

public void
addSource(String
Full path and file name of source document.
source
)

Add a source file to the list of files to merge.

public void
addSource(File
File representing source document
source
)

Add a source file to the list of files to merge.

public void
addSource(RandomAccessRead
RandomAccessRead representing source document. To pass an InputStream, wrap it into a RandomAccessReadBuffer.
source
)

Add a source to the list of documents to merge.

public void
addSources(List<RandomAccessRead>
List of RandomAccessRead objects representing source documents
sourcesList
)

Add a list of sources to the list of documents to merge.

public void
appendDocument(PDDocument
the document to receive the pages
destination
,
PDDocument
the document originating the new pages
source
)

append all pages from source to destination.

public PDFMergerUtility.AcroFormMergeMode

Returns:

the current AcroFormMergeMode
getAcroFormMergeMode
()

Get the merge mode to be used for merging AcroForms between documents AcroFormMergeMode

public PDDocumentInformation

Returns:

The destination document information.
getDestinationDocumentInformation
()

Get the destination document information that is to be set in mergeDocuments(org.apache.pdfbox.io.RandomAccessStreamCache.StreamCacheCreateFunction).

public String

Returns:

Returns the destination.
getDestinationFileName
()

Get the name of the destination file.

public PDMetadata

Returns:

The destination metadata.
getDestinationMetadata
()

Set the destination metadata that is to be set in mergeDocuments(org.apache.pdfbox.io.RandomAccessStreamCache.StreamCacheCreateFunction).

public OutputStream

Returns:

Returns the destination OutputStream.
getDestinationStream
()

Get the destination OutputStream.

public PDFMergerUtility.DocumentMergeMode

Returns:

the current DocumentMergeMode
getDocumentMergeMode
()

Get the merge mode to be used for merging documents DocumentMergeMode

pack-priv static Map<String, PDStructureElement>
pack-priv static Map<Integer, COSObjectable>
private boolean
private boolean

Returns:

true if there is a dynamic XFA form.
isDynamicXfa
(PDAcroForm
the AcroForm
acroForm
)

Test for dynamic XFA content.

public boolean

Returns:

true if acroform errors are ignored
isIgnoreAcroFormErrors
()

Indicates if acroform errors are ignored or not.

private void
legacyMergeDocuments(RandomAccessStreamCache.StreamCacheCreateFunction
a function to create an instance of a stream cache; in case of null unrestricted main memory is used
streamCacheCreateFunction
,
CompressParameters compressParameters)

Merge the list of source documents, saving the result in the destination file.

private void
public void
mergeDocuments(RandomAccessStreamCache.StreamCacheCreateFunction
a function to create an instance of a stream cache; in case of null unrestricted main memory is used
streamCacheCreateFunction
)

Merge the list of source documents, saving the result in the destination file.

public void
mergeDocuments(RandomAccessStreamCache.StreamCacheCreateFunction
a function to create an instance of a stream cache; in case of null unrestricted main memory is used
streamCacheCreateFunction
,
CompressParameters
defines if compressed object streams are enabled
compressParameters
)

Merge the list of source documents, saving the result in the destination file.

private void
mergeIDTree(PDFCloneUtility cloner, PDStructureTreeRoot srcStructTree, PDStructureTreeRoot destStructTree)

private void
mergeInto(COSDictionary
The source dictionary to get the keys/values from.
src
,
COSDictionary
The destination dictionary to merge the keys/values into.
dst
,
Set<COSName>
Names of keys that shall be skipped.
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 void
private void
private void
private void
private void
mergeRoleMap(PDStructureTreeRoot srcStructTree, PDStructureTreeRoot destStructTree)

private void
private void
public void
setAcroFormMergeMode(PDFMergerUtility.AcroFormMergeMode
AcroFormMergeMode to be used
theAcroFormMergeMode
)

Set the merge mode to be used for merging AcroForms between documents AcroFormMergeMode

public void
setDestinationDocumentInformation(PDDocumentInformation
The destination document information.
info
)

Set the destination document information that is to be set in mergeDocuments(org.apache.pdfbox.io.RandomAccessStreamCache.StreamCacheCreateFunction).

public void
setDestinationFileName(String
The destination to set.
destination
)

Set the name of the destination file.

public void
setDestinationMetadata(PDMetadata
The destination metadata.
meta
)

Set the destination metadata that is to be set in mergeDocuments(org.apache.pdfbox.io.RandomAccessStreamCache.StreamCacheCreateFunction).

public void
setDestinationStream(OutputStream
The destination to set.
destStream
)

Set the destination OutputStream.

public void
setDocumentMergeMode(PDFMergerUtility.DocumentMergeMode
DocumentMergeMode to be used
theDocumentMergeMode
)

Set the merge mode to be used for merging documents DocumentMergeMode

public void
setIgnoreAcroFormErrors(boolean
true if acroform errors should be ignored
ignoreAcroFormErrorsValue
)

Set to true to ignore acroform errors.

private void
updatePageReferences(PDFCloneUtility cloner, Map<Integer, COSObjectable> numberTreeAsMap, Map<COSDictionary, COSDictionary> objMapping)

Update the Pg and Obj references to the new (merged) page.

private void
updatePageReferences(PDFCloneUtility cloner, COSDictionary parentTreeEntry, Map<COSDictionary, COSDictionary>
mapping between old and new references
objMapping
)

Update the Pg and Obj references to the new (merged) page.

private void
private void
updateParentEntry(COSArray
the kids array
kArray
,
COSDictionary
the new parent
newParent
,
COSName
the new structure type in /S or null so it doesn't get replaced
newStructureType
)

Update the P reference to the new parent dictionary.

private void
updateStructParentEntries(PDPage
the new page
page
,
int
the offset which should be applied
structParentOffset
)

Update the StructParents and StructParent values in a PDPage.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

acroFormMergeModeback to summary
private PDFMergerUtility.AcroFormMergeMode acroFormMergeMode
destinationDocumentInformationback to summary
private PDDocumentInformation destinationDocumentInformation
destinationFileNameback to summary
private String destinationFileName
destinationMetadataback to summary
private PDMetadata destinationMetadata
destinationStreamback to summary
private OutputStream destinationStream
documentMergeModeback to summary
private PDFMergerUtility.DocumentMergeMode documentMergeMode
ignoreAcroFormErrorsback to summary
private boolean ignoreAcroFormErrors
LOGback to summary
private static final Log LOG

Log instance.

nextFieldNumback to summary
private int nextFieldNum
sourcesback to summary
private final List<Object> sources

Constructor Detail

PDFMergerUtilityback to summary
public PDFMergerUtility()

Instantiate a new PDFMergerUtility.

Method Detail

acroFormJoinFieldsModeback to summary
private void acroFormJoinFieldsMode(PDFCloneUtility cloner, PDAcroForm destAcroForm, PDAcroForm srcAcroForm) throws IOException
acroFormLegacyModeback to summary
private void acroFormLegacyMode(PDFCloneUtility cloner, PDAcroForm destAcroForm, PDAcroForm srcAcroForm) throws IOException
addSourceback to summary
public void addSource(String source) throws FileNotFoundException

Add a source file to the list of files to merge.

Parameters
source:String

Full path and file name of source document.

Exceptions
FileNotFoundException:
If the file doesn't exist
addSourceback to summary
public void addSource(File source) throws FileNotFoundException

Add a source file to the list of files to merge.

Parameters
source:File

File representing source document

Exceptions
FileNotFoundException:
If the file doesn't exist
addSourceback to summary
public void addSource(RandomAccessRead source)

Add a source to the list of documents to merge.

Parameters
source:RandomAccessRead

RandomAccessRead representing source document. To pass an InputStream, wrap it into a RandomAccessReadBuffer.

addSourcesback to summary
public void addSources(List<RandomAccessRead> sourcesList)

Add a list of sources to the list of documents to merge.

Parameters
sourcesList:List<RandomAccessRead>

List of RandomAccessRead objects representing source documents

appendDocumentback to summary
public void appendDocument(PDDocument destination, PDDocument source) throws IOException

append all pages from source to destination.

Parameters
destination:PDDocument

the document to receive the pages

source:PDDocument

the document originating the new pages

Exceptions
IOException:
If there is an error accessing data from either document.
getAcroFormMergeModeback to summary
public PDFMergerUtility.AcroFormMergeMode getAcroFormMergeMode()

Get the merge mode to be used for merging AcroForms between documents AcroFormMergeMode

Returns:PDFMergerUtility.AcroFormMergeMode

the current AcroFormMergeMode

getDestinationDocumentInformationback to summary
public PDDocumentInformation getDestinationDocumentInformation()

Get the destination document information that is to be set in mergeDocuments(org.apache.pdfbox.io.RandomAccessStreamCache.StreamCacheCreateFunction). The default is null, which means that it is ignored.

Returns:PDDocumentInformation

The destination document information.

getDestinationFileNameback to summary
public String getDestinationFileName()

Get the name of the destination file.

Returns:String

Returns the destination.

getDestinationMetadataback to summary
public PDMetadata getDestinationMetadata()

Set the destination metadata that is to be set in mergeDocuments(org.apache.pdfbox.io.RandomAccessStreamCache.StreamCacheCreateFunction). The default is null, which means that it is ignored.

Returns:PDMetadata

The destination metadata.

getDestinationStreamback to summary
public OutputStream getDestinationStream()

Get the destination OutputStream.

Returns:OutputStream

Returns the destination OutputStream.

getDocumentMergeModeback to summary
public PDFMergerUtility.DocumentMergeMode getDocumentMergeMode()

Get the merge mode to be used for merging documents DocumentMergeMode

Returns:PDFMergerUtility.DocumentMergeMode

the current DocumentMergeMode

getIDTreeAsMapback to summary
pack-priv static Map<String, PDStructureElement> getIDTreeAsMap(PDNameTreeNode<PDStructureElement> idTree) throws IOException
getNumberTreeAsMapback to summary
pack-priv static Map<Integer, COSObjectable> getNumberTreeAsMap(PDNumberTreeNode tree) throws IOException
hasOnlyDocumentsOrPartsback to summary
private boolean hasOnlyDocumentsOrParts(COSArray kLevelOneArray)
isDynamicXfaback to summary
private boolean isDynamicXfa(PDAcroForm acroForm)

Test for dynamic XFA content.

Parameters
acroForm:PDAcroForm

the AcroForm

Returns:boolean

true if there is a dynamic XFA form.

isIgnoreAcroFormErrorsback to summary
public boolean isIgnoreAcroFormErrors()

Indicates if acroform errors are ignored or not.

Returns:boolean

true if acroform errors are ignored

legacyMergeDocumentsback to summary
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:RandomAccessStreamCache.StreamCacheCreateFunction

a function to create an instance of a stream cache; in case of null unrestricted main memory is used

Exceptions
IOException:
If there is an error saving the document.
mergeAcroFormback to summary
private void mergeAcroForm(PDFCloneUtility cloner, PDDocumentCatalog destCatalog, PDDocumentCatalog srcCatalog) throws IOException
mergeDocumentsback to summary
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 use appendDocument(org.apache.pdfbox.pdmodel.PDDocument, org.apache.pdfbox.pdmodel.PDDocument).

Parameters
streamCacheCreateFunction:RandomAccessStreamCache.StreamCacheCreateFunction

a function to create an instance of a stream cache; in case of null unrestricted main memory is used

Exceptions
IOException:
If there is an error saving the document.
mergeDocumentsback to summary
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 use appendDocument(org.apache.pdfbox.pdmodel.PDDocument, org.apache.pdfbox.pdmodel.PDDocument).

Parameters
streamCacheCreateFunction:RandomAccessStreamCache.StreamCacheCreateFunction

a function to create an instance of a stream cache; in case of null unrestricted main memory is used

compressParameters:CompressParameters

defines if compressed object streams are enabled

Exceptions
IOException:
If there is an error saving the document.
mergeIDTreeback to summary
private void mergeIDTree(PDFCloneUtility cloner, PDStructureTreeRoot srcStructTree, PDStructureTreeRoot destStructTree) throws IOException
mergeIntoback to summary
private void mergeInto(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. If a key already exists in this dictionary then nothing is changed.

Parameters
src:COSDictionary

The source dictionary to get the keys/values from.

dst:COSDictionary

The destination dictionary to merge the keys/values into.

exclude:Set<COSName>

Names of keys that shall be skipped.

mergeKEntriesback to summary
private void mergeKEntries(PDFCloneUtility cloner, PDStructureTreeRoot srcStructTree, PDStructureTreeRoot destStructTree) throws IOException
mergeLanguageback to summary
private void mergeLanguage(PDDocumentCatalog destCatalog, PDDocumentCatalog srcCatalog)
mergeMarkInfoback to summary
private void mergeMarkInfo(PDDocumentCatalog destCatalog, PDDocumentCatalog srcCatalog)
mergeOutputIntentsback to summary
private void mergeOutputIntents(PDFCloneUtility cloner, PDDocumentCatalog srcCatalog, PDDocumentCatalog destCatalog) throws IOException
mergeRoleMapback to summary
private void mergeRoleMap(PDStructureTreeRoot srcStructTree, PDStructureTreeRoot destStructTree)
mergeViewerPreferencesback to summary
private void mergeViewerPreferences(PDDocumentCatalog destCatalog, PDDocumentCatalog srcCatalog)
optimizedMergeDocumentsback to summary
private void optimizedMergeDocuments(RandomAccessStreamCache.StreamCacheCreateFunction streamCacheCreateFunction, CompressParameters compressParameters) throws IOException
setAcroFormMergeModeback to summary
public void setAcroFormMergeMode(PDFMergerUtility.AcroFormMergeMode theAcroFormMergeMode)

Set the merge mode to be used for merging AcroForms between documents AcroFormMergeMode

Parameters
theAcroFormMergeMode:PDFMergerUtility.AcroFormMergeMode

AcroFormMergeMode to be used

setDestinationDocumentInformationback to summary
public void setDestinationDocumentInformation(PDDocumentInformation info)

Set the destination document information that is to be set in mergeDocuments(org.apache.pdfbox.io.RandomAccessStreamCache.StreamCacheCreateFunction). The default is null, which means that it is ignored.

Parameters
info:PDDocumentInformation

The destination document information.

setDestinationFileNameback to summary
public void setDestinationFileName(String destination)

Set the name of the destination file.

Parameters
destination:String

The destination to set.

setDestinationMetadataback to summary
public void setDestinationMetadata(PDMetadata meta)

Set the destination metadata that is to be set in mergeDocuments(org.apache.pdfbox.io.RandomAccessStreamCache.StreamCacheCreateFunction). The default is null, which means that it is ignored.

Parameters
meta:PDMetadata

The destination metadata.

setDestinationStreamback to summary
public void setDestinationStream(OutputStream destStream)

Set the destination OutputStream.

Parameters
destStream:OutputStream

The destination to set.

setDocumentMergeModeback to summary
public void setDocumentMergeMode(PDFMergerUtility.DocumentMergeMode theDocumentMergeMode)

Set the merge mode to be used for merging documents DocumentMergeMode

Parameters
theDocumentMergeMode:PDFMergerUtility.DocumentMergeMode

DocumentMergeMode to be used

setIgnoreAcroFormErrorsback to summary
public void setIgnoreAcroFormErrors(boolean ignoreAcroFormErrorsValue)

Set to true to ignore acroform errors.

Parameters
ignoreAcroFormErrorsValue:boolean

true if acroform errors should be ignored

updatePageReferencesback to summary
private void updatePageReferences(PDFCloneUtility cloner, Map<Integer, COSObjectable> numberTreeAsMap, Map<COSDictionary, COSDictionary> objMapping) throws IOException

Update the Pg and Obj references to the new (merged) page.

updatePageReferencesback to summary
private void updatePageReferences(PDFCloneUtility cloner, COSDictionary parentTreeEntry, Map<COSDictionary, COSDictionary> objMapping) throws IOException

Update the Pg and Obj references to the new (merged) page.

Parameters
objMapping:Map<COSDictionary, COSDictionary>

mapping between old and new references

updatePageReferencesback to summary
private void updatePageReferences(PDFCloneUtility cloner, COSArray parentTreeEntry, Map<COSDictionary, COSDictionary> objMapping) throws IOException
updateParentEntryback to summary
private void updateParentEntry(COSArray kArray, COSDictionary newParent, COSName newStructureType)

Update the P reference to the new parent dictionary.

Parameters
kArray:COSArray

the kids array

newParent:COSDictionary

the new parent

newStructureType:COSName

the new structure type in /S or null so it doesn't get replaced

updateStructParentEntriesback to summary
private void updateStructParentEntries(PDPage page, int structParentOffset) throws IOException

Update the StructParents and StructParent values in a PDPage.

Parameters
page:PDPage

the new page

structParentOffset:int

the offset which should be applied

org.apache.pdfbox.multipdf back to summary

public final Enum PDFMergerUtility.AcroFormMergeMode

extends Enum<PDFMergerUtility.AcroFormMergeMode>
Class Inheritance

The mode to use when merging AcroForm between documents:

Field Summary

Modifier and TypeField and Description
public static final PDFMergerUtility.AcroFormMergeMode
public static final PDFMergerUtility.AcroFormMergeMode

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public static PDFMergerUtility.AcroFormMergeMode
public static PDFMergerUtility.AcroFormMergeMode[]
Inherited from java.lang.Enum:
clonecompareTodescribeConstableequalsfinalizegetDeclaringClasshashCodenameordinaltoStringvalueOf

Field Detail

JOIN_FORM_FIELDS_MODEback to summary
public static final PDFMergerUtility.AcroFormMergeMode JOIN_FORM_FIELDS_MODE
PDFBOX_LEGACY_MODEback to summary
public static final PDFMergerUtility.AcroFormMergeMode PDFBOX_LEGACY_MODE

Constructor Detail

AcroFormMergeModeback to summary
private AcroFormMergeMode()

Method Detail

valueOfback to summary
public static PDFMergerUtility.AcroFormMergeMode valueOf(String name)
valuesback to summary
public static PDFMergerUtility.AcroFormMergeMode[] values()
org.apache.pdfbox.multipdf back to summary

public final Enum PDFMergerUtility.DocumentMergeMode

extends Enum<PDFMergerUtility.DocumentMergeMode>
Class Inheritance

The mode to use when merging documents:

Field Summary

Modifier and TypeField and Description
public static final PDFMergerUtility.DocumentMergeMode
public static final PDFMergerUtility.DocumentMergeMode

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public static PDFMergerUtility.DocumentMergeMode
public static PDFMergerUtility.DocumentMergeMode[]
Inherited from java.lang.Enum:
clonecompareTodescribeConstableequalsfinalizegetDeclaringClasshashCodenameordinaltoStringvalueOf

Field Detail

OPTIMIZE_RESOURCES_MODEback to summary
public static final PDFMergerUtility.DocumentMergeMode OPTIMIZE_RESOURCES_MODE
PDFBOX_LEGACY_MODEback to summary
public static final PDFMergerUtility.DocumentMergeMode PDFBOX_LEGACY_MODE

Constructor Detail

DocumentMergeModeback to summary
private DocumentMergeMode()

Method Detail

valueOfback to summary
public static PDFMergerUtility.DocumentMergeMode valueOf(String name)
valuesback to summary
public static PDFMergerUtility.DocumentMergeMode[] values()