Skip Navigation Links.
In this demo you can learn how to merge multiple PDF document into a single PDF document. Initially is created an empty document which will become the final document. Then the two PDF files are loaded into two PdfDocument objects which are added to the empty document.
Server Settings
IP: Port: Password:
Open the First PDF Document to Merge Open the Second PDF Document to Merge
Skip Navigation Links
C# Code
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HiQPdfClient;

namespace HiQPdf_Demo
{
    public partial class MergePdf : System.Web.UI.Page
    {
        protected void buttonCreatePdf_Click(object sender, EventArgs e)
        {
            string serverIP = textBoxServerIP.Text;
            uint serverPort = uint.Parse(textBoxServerPort.Text);
            string serverPassword = textBoxServerPassword.Text;

            // create an empty document which will become the final document after merge
            PdfDocument resultDocument = new PdfDocument(serverIP, serverPort);

            // use server password if necessary
            if (serverPassword.Length > 0)
                resultDocument.ServerPassword = serverPassword;

            // set a demo serial number
            resultDocument.SerialNumber = "YCgJMTAE-BiwJAhIB-EhlWTlBA-UEBRQFBA-U1FOUVJO-WVlZWQ==";

            // load the first document to be merged from a file
            string pdfFile1 = Server.MapPath("~") + @"\DemoFiles\Pdf\WikiHtml.pdf";

            // The second document to be merged will be loaded from a memory buffer
            string pdfFile2 = Server.MapPath("~") + @"\DemoFiles\Pdf\WikiPdf.pdf";
            byte[] pdfData2 = System.IO.File.ReadAllBytes(pdfFile2);

            // add the two documents to the result document
            resultDocument.AddDocument(pdfFile1);
            resultDocument.AddDocument(pdfData2);

            try
            {
                // write the PDF document to a memory buffer
                byte[] pdfBuffer = resultDocument.WriteToMemory();

                // inform the browser about the binary data format
                HttpContext.Current.Response.AddHeader("Content-Type", "application/pdf");

                // let the browser know how to open the PDF document and the file name
                HttpContext.Current.Response.AddHeader("Content-Disposition", String.Format("attachment; filename=MergePdf.pdf; size={0}",
                            pdfBuffer.Length.ToString()));

                // write the PDF buffer to HTTP response
                HttpContext.Current.Response.BinaryWrite(pdfBuffer);

                // call End() method of HTTP response to stop ASP.NET page processing
                HttpContext.Current.Response.End();
            }
            finally
            {
                // close the result document
                resultDocument.Close();
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string pageUri = HttpContext.Current.Request.Url.AbsoluteUri;
                hyperLinkOpenPdf1.NavigateUrl = pageUri.Substring(0, pageUri.LastIndexOf('/')) + @"/DemoFiles/Pdf/WikiHtml.pdf";
                hyperLinkOpenPdf2.NavigateUrl = pageUri.Substring(0, pageUri.LastIndexOf('/')) + @"/DemoFiles/Pdf/WikiPdf.pdf";

                Master.SelectNode("mergePdf");

                Master.LoadCodeSample("MergePdf");
            }
        }
    }
}