Document Merge

Merge two or more PDF documents using the MergeDocument class. Also use the MergeDocument class to append a document to an existing MergeDocument instance. When merging, the MergeDocument class can use a file path, stream, or byte array to import the existing PDF.

MergeDocument Class

The MergeDocument class merges a pre-existing PDF with another PDF to create a new merged PDF.

Merging Two Documents

The following code illustrates merging two PDF documents.

MergeDocument document = MergeDocument.Merge( pdfFilePath, pdfFilePath );
document.Draw(pdfFilePath);        
Dim MyDocument As MergeDocument = MergeDocument.Merge( pdfFilePath, pdfFilePath )
MyDocument.Draw(pdfFilePath)   

Appending Documents

The following code loads a PDF document, appends a range of pages from one document to it, and then appends another document to the PDF.

MergeDocument document = new MergeDocument(pdfFilePath);
document.Append(pdfFilePath, 1, 2 );
document.Append(pdfFilePath);
document.Draw(pdfFilePath);        
Dim MyDocument As MergeDocument = New MergeDocument( pdfFilePath )
MyDocument.Append(pdfFilePath, 1, 2 )
MyDocument.Append(pdfFilePath)
MyDocument.Draw(pdfFilePath)   

Importing From Streams

The following code shows the previous example reworked to use stream objects rather than files.

// StreamX variable are stream object.
MergeDocument document = new MergeDocument( new PdfDocument( streamA ) );
document.Append( new PdfDocument( streamB ), 1, 2 );
document.Append( new PdfDocument( streamC ) );
document.Draw(pdfFilePath);        
' MyStreamX variable are stream object.
Dim MyDocument As MergeDocument = New MergeDocument( New PdfDocument( MyStreamA ) )
MyDocument.Append( New PdfDocument( MyStreamB ), 1, 2 )
MyDocument.Append( New PdfDocument( MyStreamC ) )
MyDocument.Draw(pdfFilePath)   

Importing From a Byte Array

The following code shows how to perform the previous example using byte arrays instead of files.

// DataX variable are byte arrays.
MergeDocument document = new MergeDocument( new PdfDocument( dataA ) );
document.Append( new PdfDocument( dataB ), 1, 2 );
document.Append( new PdfDocument( dataC ) );
document.Draw(pdfFilePath);        
' MyDataX variable are byte arrays.
Dim MyDocument As MergeDocument = New MergeDocument( New PdfDocument( MyDataA ) )
MyDocument.Append( New PdfDocument( MyDataB ), 1, 2 )
MyDocument.Append( New PdfDocument( MyDataC ) )
MyDocument.Draw(pdfFilePath)  

The examples in this topic show how to merge PDF documents that are not going to be reused multiple times. For details on how to most efficiently handle PDF documents that will be used multiple times, see the Performance Considerations topic.

In this topic