Merge PDF files in C# and VB.NET

With GemBox.Pdf, you can merge content of several PDF files into one PDF file.

The following example demonstrates how to merge (concatenate) PDF files.

Screenshot
PDF Library for C# / VB.NET Screenshot

See the full code below, use Run Example to execute.

using System;
using System.IO;
using GemBox.Pdf;

class Sample
{
    [STAThread]
    static void Main(string[] args)
    {
        // If using Professional version, put your serial key below.
        ComponentInfo.SetLicense("FREE-LIMITED-KEY");

        PdfDocument document = new PdfDocument();

        string pathToResources = "Resources";

        // List of source file names.
        string[] fileNames = new string[] {
            "MergeFile01.pdf",
            "MergeFile02.pdf",
            "MergeFile03.pdf"
        };

        foreach (string fileName in fileNames)
            // Load a source document from the specified path.
            using (PdfDocument source = PdfDocument.Load(Path.Combine(pathToResources, fileName)))
                // Clone all pages from the source document and add them to the destination document.
                foreach (PdfPage page in source.Pages)
                    document.Pages.AddClone(page);

        document.Save("Merge Files.pdf");
        document.Close();
    }
}
Imports System.IO
Imports GemBox.Pdf

Module Samples

    Sub Main()

        ' If using Professional version, put your serial key below.
        ComponentInfo.SetLicense("FREE-LIMITED-KEY")

        Dim document As PdfDocument = New PdfDocument()

        Dim pathToResources As String = "Resources"

        ' List of source file names.
        Dim fileNames As String() = New String() {
            "MergeFile01.pdf",
            "MergeFile02.pdf",
            "MergeFile03.pdf"
        }

        For Each fileName As String In fileNames
            ' Load a source document from the specified path.
            Using source As PdfDocument = PdfDocument.Load(Path.Combine(pathToResources, fileName))
                ' Clone all pages from the source document and add them to the destination document.
                For Each page As PdfPage In source.Pages
                    document.Pages.AddClone(page)
                Next
            End Using
        Next

        document.Save("Merge Files.pdf")
        document.Close()

    End Sub

End Module