org.apache.pdfbox.multipdf

## Class PDFMergerUtility

- java.lang.Object
- - org.apache.pdfbox.multipdf.PDFMergerUtility

    public class PDFMergerUtility
    extends Object

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

Author:
  Ben Litchfield

### Nested Class Summary

Nested Classes

`static class`

`PDFMergerUtility.AcroFormMergeMode`
The mode to use when merging AcroForm between documents: `PDFMergerUtility.AcroFormMergeMode.JOIN_FORM_FIELDS_MODE` fields 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.
Although the API is finalized processing of different form field types is still in development. Currently only (nested) text fields do work with intermediate nodes being existent.

`static class` `PDFMergerUtility.DocumentMergeMode` The mode to use when merging documents: `PDFMergerUtility.DocumentMergeMode.OPTIMIZE_RESOURCES_MODE` Optimizes resource handling such as closing documents early.

### Constructor Summary

Constructors
Constructor and Description
`PDFMergerUtility()` Instantiate a new PDFMergerUtility.

### Method Summary

All Methods Instance Methods Concrete Methods Deprecated Methods
`void` `addSource(File source)` Add a source file to the list of files to merge.
`void` `addSource(InputStream source)` Add a source to the list of documents to merge.
`void` `addSource(String source)` Add a source file to the list of files to merge.
`void` `addSources(List<InputStream> sourcesList)` Add a list of sources to the list of documents to merge.
`void` `appendDocument(PDDocument destination, PDDocument source)` append all pages from source to destination.
`PDFMergerUtility.AcroFormMergeMode` `getAcroFormMergeMode()` Get the merge mode to be used for merging AcroForms between documents `PDFMergerUtility.AcroFormMergeMode`
`PDDocumentInformation` `getDestinationDocumentInformation()` Get the destination document information that is to be set in `mergeDocuments(org.apache.pdfbox.io.MemoryUsageSetting)`.
`String` `getDestinationFileName()` Get the name of the destination file.
`PDMetadata` `getDestinationMetadata()` Set the destination metadata that is to be set in `mergeDocuments(org.apache.pdfbox.io.MemoryUsageSetting)`.
`OutputStream` `getDestinationStream()` Get the destination OutputStream.
`PDFMergerUtility.DocumentMergeMode` `getDocumentMergeMode()` Get the merge mode to be used for merging documents `PDFMergerUtility.DocumentMergeMode`
`boolean` `isIgnoreAcroFormErrors()` Indicates if acroform errors are ignored or not.
`void` `mergeDocuments()` Deprecated. use `mergeDocuments(org.apache.pdfbox.io.MemoryUsageSetting)`
`void` `mergeDocuments(MemoryUsageSetting memUsageSetting)` Merge the list of source documents, saving the result in the destination file.
`void` `setAcroFormMergeMode(PDFMergerUtility.AcroFormMergeMode theAcroFormMergeMode)` Set the merge mode to be used for merging AcroForms between documents `PDFMergerUtility.AcroFormMergeMode`
`void` `setDestinationDocumentInformation(PDDocumentInformation info)` Set the destination document information that is to be set in `mergeDocuments(org.apache.pdfbox.io.MemoryUsageSetting)`.
`void` `setDestinationFileName(String destination)` Set the name of the destination file.
`void` `setDestinationMetadata(PDMetadata meta)` Set the destination metadata that is to be set in `mergeDocuments(org.apache.pdfbox.io.MemoryUsageSetting)`.
`void` `setDestinationStream(OutputStream destStream)` Set the destination OutputStream.
`void` `setDocumentMergeMode(PDFMergerUtility.DocumentMergeMode theDocumentMergeMode)` Set the merge mode to be used for merging documents `PDFMergerUtility.DocumentMergeMode`
`void` `setIgnoreAcroFormErrors(boolean ignoreAcroFormErrorsValue)` Set to true to ignore acroform errors.

### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

### Constructor Detail

#### PDFMergerUtility

    public PDFMergerUtility()

Instantiate a new PDFMergerUtility.

### Method Detail

#### getAcroFormMergeMode

    public PDFMergerUtility.AcroFormMergeMode getAcroFormMergeMode()

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

#### setAcroFormMergeMode

    public void setAcroFormMergeMode(PDFMergerUtility.AcroFormMergeMode theAcroFormMergeMode)

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

#### setDocumentMergeMode

    public void setDocumentMergeMode(PDFMergerUtility.DocumentMergeMode theDocumentMergeMode)

Set the merge mode to be used for merging documents `PDFMergerUtility.DocumentMergeMode`

#### getDocumentMergeMode

    public PDFMergerUtility.DocumentMergeMode getDocumentMergeMode()

Get the merge mode to be used for merging documents `PDFMergerUtility.DocumentMergeMode`

#### getDestinationFileName

    public String getDestinationFileName()

Get the name of the destination file.

Returns:
  Returns the destination.

#### setDestinationFileName

    public void setDestinationFileName(String destination)

Set the name of the destination file.

Parameters:
  `destination` - The destination to set.

#### getDestinationStream

    public OutputStream getDestinationStream()

Get the destination OutputStream.

Returns:
  Returns the destination OutputStream.

#### setDestinationStream

    public void setDestinationStream(OutputStream destStream)

Set the destination OutputStream.

Parameters:
  `destStream` - The destination to set.

#### getDestinationDocumentInformation

    public PDDocumentInformation getDestinationDocumentInformation()

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

Returns:
  The destination document information.

#### setDestinationDocumentInformation

    public void setDestinationDocumentInformation(PDDocumentInformation info)

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

Parameters:
  `info` - The destination document information.

#### getDestinationMetadata

    public PDMetadata getDestinationMetadata()

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

Returns:
  The destination metadata.

#### setDestinationMetadata

    public void setDestinationMetadata(PDMetadata meta)

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

Parameters:
  `meta` - The destination metadata.

#### addSource

    public void addSource(String source)
                   throws FileNotFoundException

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

    public void addSource(File source)
                   throws FileNotFoundException

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

    public void addSource(InputStream source)

Add a source to the list of documents to merge.

Parameters:
  `source` - InputStream representing source document

#### addSources

    public void addSources(List<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 IOException

Deprecated. use `mergeDocuments(org.apache.pdfbox.io.MemoryUsageSetting)`
Merge the list of source documents, saving the result in the destination file.

Throws:
  `IOException` - If there is an error saving the document.

#### mergeDocuments

    public void mergeDocuments(MemoryUsageSetting memUsageSetting)
                        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:
  `memUsageSetting` - defines how memory is used for buffering PDF streams; in case of `null` unrestricted main memory is used
Throws:
  `IOException` - If there is an error saving the document.

#### appendDocument

    public void appendDocument(PDDocument destination,
                               PDDocument source)
                        throws IOException

append all pages from source to destination.

Parameters:
  `destination` - the document to receive the pages
  `source` - the document originating the new pages
Throws:
  `IOException` - If there is an error accessing data from either document.

#### 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