MergeDocument Class | Append Method | PdfDocument Class | Performance Considerations | Programming with Merger for .NET

Language

Visual Basic

C#

Show All

Merging Two Documents

The following code will merge two PDF documents together.

[Visual Basic]
Dim MyDocument As MergeDocument = MergeDocument.Merge( "C:\DocumentA.pdf", "C:\DocumentB.pdf" )
MyDocument.Draw( "C:\MyDocument.pdf" )
[C#]
MergeDocument document = MergeDocument.Merge( @"C:\DocumentA.pdf", @"C:\DocumentB.pdf" );
document.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.

[Visual Basic]
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" )
[C#]
MergeDocument document = new MergeDocument( @"C:\DocumentA.pdf" );
document.Append( @"C:\DocumentB.pdf", 1, 2 );
document.Append( @"C:\DocumentC.pdf" );
document.Draw( @"C:\MyDocument.pdf" );

Importing From Streams

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

[Visual Basic]
' 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" )
[C#]
// 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" );

Importing From a Byte Array

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

[Visual Basic]
' 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" )
[C#]
' 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" );

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.

See Also 

MergeDocument Class | Append Method | PdfDocument Class | Performance Considerations | Programming with Merger for .NET