EVO HTML to PDF Converter

Merge PDF Documents

EVO HTML to PDF Converter for .NET Documentation

EVO HTML to PDF Converter allows you to merge a set of PDF documents into another PDF document. You can merge entire PDF documents or only a subset of PDF pages. The methods of the EvoPdfDocument class you can use to merge PDF documents are DocumentAppendDocument(Document), DocumentAppendDocument(Document, Int32, Int32), DocumentInsertDocument(Int32, Document) and DocumentInsertDocument(Int32, Document, Int32, Int32). An appended document must stay opened until the merged document is closed otherwise the document resulted after merge will be corrupted. You can set the DocumentAutoCloseAppendedDocs property on true to automatically close the merged documents when the document resulted after merge is closed.

Code Sample - Merge PDF Documents

protected void mergePdfButton_Click(object sender, EventArgs e)
{
    // Create the merge result PDF document
    Document mergeResultPdfDocument = new Document();

    // Automatically close the merged documents when the document resulted after merge is closed
    mergeResultPdfDocument.AutoCloseAppendedDocs = true;

    // Set license key received after purchase to use the converter in licensed mode
    // Leave it not set to use the converter in demo mode
    mergeResultPdfDocument.LicenseKey = "4W9+bn19bn5ue2B+bn1/YH98YHd3d3c=";

    try
    {
        // The documents to merge must remain opened until the PDF document resulted after merge is saved
        // The merged documents can be automatically closed when the document resulted after merge is closed
        // if the AutoCloseAppendedDocs property of the PDF document resulted after merge is set on true like
        // in this demo applcation

        // Load the first PDF document to merge
        string firstPdfFilePath = Server.MapPath("~/DemoAppFiles/Input/PDF_Files/Merge_Before_Conversion.pdf");
        Document firstPdfDocumentToMerge = new Document(firstPdfFilePath);
        // Merge the first PDF document
        mergeResultPdfDocument.AppendDocument(firstPdfDocumentToMerge);

        // Load the second PDF document to merge
        string secondPdfFilePath = Server.MapPath("~/DemoAppFiles/Input/PDF_Files/Merge_After_Conversion.pdf");
        Document secondPdfDocumentToMerge = new Document(secondPdfFilePath);
        // Merge the second PDF document
        mergeResultPdfDocument.AppendDocument(secondPdfDocumentToMerge);

        // Save the merge result PDF document in a memory buffer
        byte[] outPdfBuffer = mergeResultPdfDocument.Save();

        // Send the PDF as response to browser

        // Set response content type
        Response.AddHeader("Content-Type", "application/pdf");

        // Instruct the browser to open the PDF file as an attachment or inline
        Response.AddHeader("Content-Disposition", String.Format("attachment; filename=Merge_PDF.pdf; size={0}", outPdfBuffer.Length.ToString()));

        // Write the PDF document buffer to HTTP response
        Response.BinaryWrite(outPdfBuffer);

        // End the HTTP response and stop the current page processing
        Response.End();
    }
    finally
    {
        // Close the PDF document resulted after merge
        // When the AutoCloseAppendedDocs property is true the merged PDF documents will also be closed
        mergeResultPdfDocument.Close();
    }
}