PDF Merge in C#, VB.NET

  • Demo
  • C# source
  • VB.Net source

The sample demonstrates how to merge multiple PDF documents to one PDF document.

(NO screenshot)

using System;
using Spire.Pdf;

namespace MergeDocuments
{
    class Program
    {
        static void Main(string[] args)
        {
            //pdf document list
            String[] files = new String[]
            {
                @"Sample3.pdf",
                @"Sample2.pdf",
                @"Sample1.pdf"
            };
            //open pdf documents            
            PdfDocument[] docs = new PdfDocument[files.Length];
            for (int i = 0; i < files.Length; i++)
            {
                docs[i] = new PdfDocument(files[i]);
            }

            //append document
            docs[0].AppendPage(docs[1]);

            //import page
            for (int i = 0; i < docs[2].Pages.Count; i = i + 2)
            {
                docs[0].InsertPage(docs[2], i);
            }

            //Save pdf file.
            docs[0].SaveToFile("MergeDocuments.pdf");

            //close
            foreach (PdfDocument doc in docs)
            {
                doc.Close();
            }

            //Launching the Pdf file.
            System.Diagnostics.Process.Start("MergeDocuments.pdf");
        }
    }
}

Imports Spire.Pdf

Namespace MergeDocuments
    Friend Class Program
        Shared Sub Main(ByVal args() As String)
            'pdf document list
            Dim files() As String = {"Sample3.pdf", "Sample2.pdf", "Sample1.pdf"}
            'open pdf documents            
            Dim docs(files.Length - 1) As PdfDocument
            For i As Integer = 0 To files.Length - 1
                docs(i) = New PdfDocument(files(i))
            Next i

            'append document
            docs(0).AppendPage(docs(1))

            'import page
            For i As Integer = 0 To docs(2).Pages.Count - 1 Step 2
                docs(0).InsertPage(docs(2), i)
            Next i

            'Save pdf file.
            docs(0).SaveToFile("MergeDocuments.pdf")

            'close
            For Each doc As PdfDocument In docs
                doc.Close()
            Next doc

            'Launching the Pdf file.
            Process.Start("MergeDocuments.pdf")
        End Sub

    End Class
End Namespace