SelectPdf for .NET - Pdf Merge - C# / ASP.NET 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 C#



using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SelectPdf;

namespace SelectPdf.Samples
{
    public partial class pdf_merge : System.Web.UI.Page
    {
        protected void BtnCreatePdf_Click(object sender, EventArgs e)
        {
            // the 2 initial files
            string file1 = Server.MapPath("~/files/doc1.pdf");
            string file2 = Server.MapPath("~/files/doc2.pdf");

            // load the 2 pdf documents
            PdfDocument doc1 = new PdfDocument(file1);
            PdfDocument doc2 = new PdfDocument(file2);

            // create a new pdf document
            PdfDocument doc = new PdfDocument();

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

            // save pdf document
            doc.Save(Response, false, "Sample.pdf");

            // close pdf document
            doc.Close();

            // close the original pdf documents
            doc1.Close();
            doc2.Close();
        }
    }
}