Append two or more existing PDF files

Append two or more existing PDF files

The following code sample shows how to append PDF files.

// create the target document
Document documentOut = new Document();

// append 2 documents
using (FileStream file1 = new FileStream("1.pdf", FileMode.Open, FileAccess.Read))
using (FileStream file2 = new FileStream("2.pdf", FileMode.Open, FileAccess.Read))
   Document documentIn = new Document(file1);
   foreach (Page page in documentIn.Pages)
      // append each page to the target document
      // it is required to clone the page
   documentIn = new Document(file2);
   // append all pages to the target document
' create the target document
Dim documentOut As New Document()

' append 2 documents
Using inFile1 As New FileStream("..\..\..\inputDocuments\PackingLightBrochure.pdf", FileMode.Open, FileAccess.Read)
    Using inFile2 As New FileStream("..\..\..\inputDocuments\TheresMoreToAcrobat.pdf", FileMode.Open, FileAccess.Read)
        Dim documentIn As Document

        documentIn = New Document(inFile1)
        For Each page As Page In documentIn.Pages
            ' append page to the target document, we don't want to change the documentIn, so Clone the page.

        documentIn = New Document(inFile2)

        ' append all pages to the target document

        ' write the target document to disk
        ' note that when the target document is written, the source streams
        ' must still be open. for efficiency reasons, all source page content 
        ' is extracted as late as possible
        Using outFile As New FileStream("..\..\append.pdf", FileMode.Create, FileAccess.Write)
        End Using
    End Using
End Using