using System.Web.Mvc;
using System.Configuration;
using SelectPdf;
using System;

namespace SelectPdf.Samples.Controllers
{
    public class PdfMergeController : Controller
    {
        // GET: PdfMerge
        public ActionResult Index()
        {
            return View();
        }

        [HttpPost]
        public ActionResult SubmitAction(FormCollection collection)
        {
            // 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
            byte[] pdf = doc.Save();

            // close pdf document
            doc.Close();
            doc1.Close();
            doc2.Close();

            // return resulted pdf document
            FileResult fileResult = new FileContentResult(pdf, "application/pdf");
            fileResult.FileDownloadName = "Document.pdf";
            return fileResult;
        }
    }
}