How to Merge Several PDF Docouments into One with C#/VB.NET

Sometimes, we may need to merge one document into one, or inset some pages of one PDF to another one.  For example, we download two PDF documents. One shows the first part of one story and the other shows the next part. In order to have one whole story document, we can merge them into one. In this post, I will introduce a method about how to merge PDF by using C#/VB.NET.

In my example, I prepare three documents. What I will do is to merge document 1 and document 2 into one and insert even pages of document 3 into the merged document.

I use a .NET PDF component, Spire.PDF for .NET in this method, so I add its dll file as reference in my project at the beginning. Then, we can start with coding.

Step:

  1. Load PDF documents. Because there are three documents, I create a list and put them in an array. The documents are put in doc[0], doc[1], doc[2] respectively.
  2. Merge doc[0] and doc[1]. Append page in doc[0], the page is the whole doc[1] document.
  3. Insert even page of doc[2] in doc[0]. Use a for loop to get even pages in doc[2] and insert these pages.
  4. Save the merged file and launch.

Coding:

C#

using System;
using Spire.Pdf;

namespace MergePDF
{
    class Merge
    {
        static void Main(string[] args)
        {
            //PDF Document List
            String[] files = new String[]
            {
                @”D:\work\My Documents\A Thousand Suns.pdf”,
                @”D:\work\My Documents\easily cook.pdf”,
                @”D:\work\My Documents\Troubleshooting Guide Template.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 and Launch
            docs[0].SaveToFile(“MergeDocuments.pdf”);
            foreach (PdfDocument doc in docs)
            {
                doc.Close();
            }
            System.Diagnostics.Process.Start(“MergeDocuments.pdf”);
        }
    }
}

VB.NET

Imports System
Imports Spire.Pdf

Namespace MergePDF
       Friend Class Merge
              Shared Sub Main(ByVal args() As String)
                     ‘PDF Document List
                     Dim files() As String = { “D:\work\My Documents\A Thousand Suns.pdf”, “D:\work\My Documents\easily cook.pdf”, “D:\work\My Documents\Troubleshooting Guide Template.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 and Launch
                     docs(0).SaveToFile(“MergeDocuments.pdf”)
                     For Each doc As PdfDocument In docs
                           doc.Close()
                     Next doc
                     System.Diagnostics.Process.Start(“MergeDocuments.pdf”)
              End Sub
       End Class
End Namespace

Result Shown by Following:

Freely Download Spire.PDF for .NET

About these ads

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s