1. Home
  2. /
  3. GroupDocs.Merger.LowCode
  4. /
  5. Developer Guide
  6. /
  7. Merge PDF (JoinPdf)

Using JoinPdf to Merge PDF Documents

This topic shows how to use the JoinPdf plugin to merge PDF documents. JoinPdf takes a source PDF document and appends other PDF documents to it, in the order you list them.

Prerequisites

Refer to the Developer Guide to learn how to set up your environment and run the code examples in this section.

The Loading Source Documents and Saving Output Documents topics explain how to specify input and output documents.

Basic Example

The following example appends a one-page cost analysis to a business plan. The documents are loaded from the current folder, and the merged document is saved to the output folder.

using System;
using GroupDocs.Merger.Domain.Options;
using GroupDocs.Merger.LowCode;

// Load license keys
var publicKey = Environment.GetEnvironmentVariable("GD_PUBLIC_KEY");
var privateKey = Environment.GetEnvironmentVariable("GD_PRIVATE_KEY");

// Apply the license
License.Set(publicKey, privateKey);

// Append the documents to the source document
var merger = new JoinPdf("business-plan.pdf", new[] { "cost-analysis.pdf" }, new JoinOptions());

// Save the merged document
merger.Save("output/business-plan-with-costs.pdf");
Imports System
Imports GroupDocs.Merger.Domain.Options
Imports GroupDocs.Merger.LowCode

Module Program
    Sub Main()
        ' Load license keys
        Dim publicKey = Environment.GetEnvironmentVariable("GD_PUBLIC_KEY")
        Dim privateKey = Environment.GetEnvironmentVariable("GD_PRIVATE_KEY")

        ' Apply the license
        License.Set(publicKey, privateKey)

        ' Append the documents to the source document
        Dim merger As New JoinPdf("business-plan.pdf", {"cost-analysis.pdf"}, New JoinOptions())

        ' Save the merged document
        merger.Save("output/business-plan-with-costs.pdf")
    End Sub
End Module
open System
open GroupDocs.Merger.Domain.Options
open GroupDocs.Merger.LowCode

[<EntryPoint>]
let main _ =
    // Load license keys
    let publicKey = Environment.GetEnvironmentVariable("GD_PUBLIC_KEY")
    let privateKey = Environment.GetEnvironmentVariable("GD_PRIVATE_KEY")

    // Apply the license
    License.Set(publicKey, privateKey)

    // Append the documents to the source document
    let merger = JoinPdf("business-plan.pdf", [ "cost-analysis.pdf" ], JoinOptions())

    // Save the merged document
    merger.Save("output/business-plan-with-costs.pdf")
    0

Expected output: Download business-plan-with-costs.pdf (19 pages)

Merge Multiple PDF Documents

Pass as many documents as you need. They are appended to the source document in the order of the list. The following example merges three chapters into one document.

using System;
using GroupDocs.Merger.Domain.Options;
using GroupDocs.Merger.LowCode;

// Load license keys
var publicKey = Environment.GetEnvironmentVariable("GD_PUBLIC_KEY");
var privateKey = Environment.GetEnvironmentVariable("GD_PRIVATE_KEY");

// Apply the license
License.Set(publicKey, privateKey);

// Append the documents to the source document
var merger = new JoinPdf("chapter-1.pdf", new[] { "chapter-2.pdf", "chapter-3.pdf" }, new JoinOptions());

// Save the merged document
merger.Save("output/merged-chapters.pdf");
Imports System
Imports GroupDocs.Merger.Domain.Options
Imports GroupDocs.Merger.LowCode

Module Program
    Sub Main()
        ' Load license keys
        Dim publicKey = Environment.GetEnvironmentVariable("GD_PUBLIC_KEY")
        Dim privateKey = Environment.GetEnvironmentVariable("GD_PRIVATE_KEY")

        ' Apply the license
        License.Set(publicKey, privateKey)

        ' Append the documents to the source document
        Dim merger As New JoinPdf("chapter-1.pdf", {"chapter-2.pdf", "chapter-3.pdf"}, New JoinOptions())

        ' Save the merged document
        merger.Save("output/merged-chapters.pdf")
    End Sub
End Module
open System
open GroupDocs.Merger.Domain.Options
open GroupDocs.Merger.LowCode

[<EntryPoint>]
let main _ =
    // Load license keys
    let publicKey = Environment.GetEnvironmentVariable("GD_PUBLIC_KEY")
    let privateKey = Environment.GetEnvironmentVariable("GD_PRIVATE_KEY")

    // Apply the license
    License.Set(publicKey, privateKey)

    // Append the documents to the source document
    let merger = JoinPdf("chapter-1.pdf", [ "chapter-2.pdf"; "chapter-3.pdf" ], JoinOptions())

    // Save the merged document
    merger.Save("output/merged-chapters.pdf")
    0

Expected output: Download merged-chapters.pdf (18 pages)

Merge PDF Documents from a Stream

The source document can also be a Stream; the documents to append are always file paths. The stream is read from the beginning, and it stays open after Save.

using System;
using System.IO;
using GroupDocs.Merger.Domain.Options;
using GroupDocs.Merger.LowCode;

// Load license keys
var publicKey = Environment.GetEnvironmentVariable("GD_PUBLIC_KEY");
var privateKey = Environment.GetEnvironmentVariable("GD_PRIVATE_KEY");

// Apply the license
License.Set(publicKey, privateKey);

// Open the source document as a stream
using var stream = File.OpenRead("business-plan.pdf");

// Append the documents to the source document
var merger = new JoinPdf(stream, new[] { "cost-analysis.pdf" }, new JoinOptions());

// Save the merged document
merger.Save("output/business-plan-with-costs.pdf");
Imports System
Imports System.IO
Imports GroupDocs.Merger.Domain.Options
Imports GroupDocs.Merger.LowCode

Module Program
    Sub Main()
        ' Load license keys
        Dim publicKey = Environment.GetEnvironmentVariable("GD_PUBLIC_KEY")
        Dim privateKey = Environment.GetEnvironmentVariable("GD_PRIVATE_KEY")

        ' Apply the license
        License.Set(publicKey, privateKey)

        ' Open the source document as a stream
        Using stream = File.OpenRead("business-plan.pdf")
            ' Append the documents to the source document
            Dim merger As New JoinPdf(stream, {"cost-analysis.pdf"}, New JoinOptions())

            ' Save the merged document
            merger.Save("output/business-plan-with-costs.pdf")
        End Using
    End Sub
End Module
open System
open System.IO
open GroupDocs.Merger.Domain.Options
open GroupDocs.Merger.LowCode

[<EntryPoint>]
let main _ =
    // Load license keys
    let publicKey = Environment.GetEnvironmentVariable("GD_PUBLIC_KEY")
    let privateKey = Environment.GetEnvironmentVariable("GD_PRIVATE_KEY")

    // Apply the license
    License.Set(publicKey, privateKey)

    // Open the source document as a stream
    use stream = File.OpenRead("business-plan.pdf")

    // Append the documents to the source document
    let merger = JoinPdf(stream, [ "cost-analysis.pdf" ], JoinOptions())

    // Save the merged document
    merger.Save("output/business-plan-with-costs.pdf")
    0

Expected output: Download business-plan-with-costs.pdf (19 pages)