Skip to main content

Document Merge

Version: 7.0

Document Merge

Merging Two Documents​

The following code will merge two PDF documents together.

MergeDocument document = MergeDocument.Merge( @"C:\DocumentA.pdf", @"C:\DocumentB.pdf" );
document.Draw( @"C:\MyDocument.pdf" );
Dim MyDocument As MergeDocument = MergeDocument.Merge( "C:\DocumentA.pdf", "C:\DocumentB.pdf" )
MyDocument.Draw( "C:\MyDocument.pdf" )

Appending Documents​

The following code will load a PDF document, append a range of pages from one document to it, and then append another entire document to it.

MergeDocument document = new MergeDocument( @"C:\DocumentA.pdf" );
document.Append( @"C:\DocumentB.pdf", 1, 2 );
document.Append( @"C:\DocumentC.pdf" );
document.Draw( @"C:\MyDocument.pdf" );
Dim MyDocument As MergeDocument = New MergeDocument( "C:\DocumentA.pdf" )
MyDocument.Append( "C:\DocumentB.pdf", 1, 2 )
MyDocument.Append( "C:\DocumentC.pdf" )
MyDocument.Draw( "C:\MyDocument.pdf" )

Importing From Streams​

The following code shows how to perform the previous example using stream objects instead of files.

// streamX varialbe are stream object.
MergeDocument document = new MergeDocument( new PdfDocument( streamA ) );
document.Append( new PdfDocument( streamB ), 1, 2 );
document.Append( new PdfDocument( streamC ) );
document.Draw( @"C:\MyDocument.pdf" );
' MyStreamX varialbe 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( "C:\MyDocument.pdf" )

Importing From a Byte Array​

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

// dataX varialbe are byte arrays.
MergeDocument document = new MergeDocument( new PdfDocument( dataA ) );
document.Append( new PdfDocument( dataB ), 1, 2 );
document.Append( new PdfDocument( dataC ) );
document.Draw( @"C:\MyDocument.pdf" );
' MyDataX varialbe 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( "C:\MyDocument.pdf" )

NOTE: 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.