SelectPdf for .NET - Pdf Merge - VB.NET / ASP.NET MVC Sample

This sample shows how to use SelectPdf Library for .NET to merge 2 different pdf documents into a new pdf document.

Here are the 2 individual pdf documents:
File 1
File 2

Click on the "Create PDF" button below to see the merge result.


Sample Code VB.NET



Imports SelectPdf

Namespace Controllers
    Public Class PdfMergeController
        Inherits Controller

        ' GET: PdfMerge
        Public Function Index() As ActionResult
            Return View()
        End Function

        <HttpPost> _
        Public Function SubmitAction(collection As FormCollection) As ActionResult
            ' the 2 initial files
            Dim file1 As String = Server.MapPath("~/files/doc1.pdf")
            Dim file2 As String = Server.MapPath("~/files/doc2.pdf")

            ' load the 2 pdf documents
            Dim doc1 As New PdfDocument(file1)
            Dim doc2 As New PdfDocument(file2)

            ' create a new pdf document
            Dim doc As New PdfDocument()

            ' add the pages of those 2 documents to the new document
            doc.Append(doc1)
            doc.Append(doc2)

            ' save pdf document
            Dim pdf As Byte() = doc.Save()

            ' close pdf document
            doc.Close()
            doc1.Close()
            doc2.Close()

            ' return resulted pdf document
            Dim fileResult As FileResult = New FileContentResult(pdf, "application/pdf")
            fileResult.FileDownloadName = "Document.pdf"
            Return fileResult
        End Function
    End Class
End Namespace