MergePDFs.vb
''
'' This code is part of GrapeCity Documents for PDF samples.
'' Copyright (c) GrapeCity, Inc. All rights reserved.
''
Imports System.IO
Imports System.Drawing
Imports GrapeCity.Documents.Pdf
Imports GrapeCity.Documents.Pdf.Annotations

'' This sample shows how to merge two existing PDFs into a single document.
'' The method GcPdfDocument.MergeWithDocument() provides this feature,
'' And allows to insert all Or some pages from another PDF into the current
'' document. The simplest form of this method Is demonstrated in this sample,
'' appending a whole PDF to the current document.
Public Class MergePDFs
    Function CreatePDF(ByVal stream As Stream) As Integer
        Dim doc = New GcPdfDocument()
        Using fs0 = New FileStream(Path.Combine("Resources", "PDFs", "The-Rich-History-of-JavaScript.pdf"), FileMode.Open, FileAccess.Read)
            Using fs1 = New FileStream(Path.Combine("Resources", "PDFs", "CompleteJavaScriptBook.pdf"), FileMode.Open, FileAccess.Read)
                doc.Load(fs0)
                '' Save page count for the navigaion link added below:
                Dim pgNo = doc.Pages.Count
                Dim doc1 = New GcPdfDocument()
                doc1.Load(fs1)
                doc.MergeWithDocument(doc1, New MergeDocumentOptions())
                '' Insert a note at the beginning of the document:
                Dim page = doc.Pages.Insert(0)
                Dim rc = Util.AddNote(
                    "GcPdfDocument.MergeWithDocument() method allows to add to the current document all or some pages " +
                    "from another document." + vbLf +
                    "In this sample we load one PDF, append another whole PDF to it, and save the result." + vbLf +
                    "Click this note to jump directly to the first page of the 2nd document.",
                    page)
                '' Link the note to the first page of the second document
                page.Annotations.Add(New LinkAnnotation(rc, New DestinationFit(pgNo + 1)))
                '' Done (target document must be saved BEFORE the source is disposed):
                doc.Save(stream)
            End Using
            Return doc.Pages.Count
        End Using
        Return doc.Pages.Count
    End Function
End Class