Skip to content

Instantly share code, notes, and snippets.

@GroupDocsGists
Last active June 25, 2025 13:14
Show Gist options
  • Save GroupDocsGists/28e3ffc383abee89dadfd4dec9bf2881 to your computer and use it in GitHub Desktop.
Save GroupDocsGists/28e3ffc383abee89dadfd4dec9bf2881 to your computer and use it in GitHub Desktop.
How to merge PDF files in C#

Merge PDF Files in C#

If you need to merge PDF files, this example shows how, complete with commentary and best practices. Using GroupDocs.Merger for .NET, you can efficiently combine multiple PDF files into a single document, which can be useful for various applications like documentation, reports, and more.

πŸ“¦ Prerequisites

  • Install the GroupDocs.Merger library via NuGet to get started.
  • You may download a free trial of GroupDocs.Merger and use a temporary license for unrestricted usage of our library.
  • This works for various file types including PDF, DOCX, and PPTX.

🧩 Key Capabilities

  • Effortlessly combine multiple PDF files into a single document for easy sharing and management.
  • Utilize bookmarks in your merged PDF for added navigational functionality.
  • Maintain the original format and quality of each PDF when merging.
  • Easily handle file paths and output directories programmatically with C#.
  • Support for batch merging, allowing multiple file types to be processed in one operation.

πŸ’» Code Examples

See the following example #1: MergePdf.cs
See the following example #2: MergePdfUseBookmarks.cs

βœ… How to Use in .NET

  1. Install the GroupDocs.Merger package through NuGet in your project.
  2. Create an instance of Merger class with the first PDF file's path.
  3. If using bookmarks, initialize PdfJoinOptions and set the UseBookmarks flag to true.
  4. Add additional PDF files using the Join method to combine them.
  5. Save the merged PDF file to your desired output directory.
  6. Verify your output to ensure the merging was completed successfully.

πŸ“Ž Related Resources

πŸ“œ Final Thoughts

By following these instructions, you can easily merge PDF files for your documentation needs. This capability is essential for streamlining document management and ensuring presentable files. For more details, visit our full docs or request a trial license.

using System;
using System.IO;
namespace GroupDocs.Merger.Examples.CSharp.BasicUsage
{
/// <summary>
/// This example demonstrates how to merge multiple PDF files into single file.
/// For more details about merging Portable Document (.pdf) files please check this documentation article
/// https://docs.groupdocs.com/merger/net/merge/pdf
/// </summary>
internal static class MergePdf
{
public static void Run()
{
Console.WriteLine("=======================================================================");
Console.WriteLine();
Console.WriteLine("Example Basic Usage: MergePdf");
Console.WriteLine();
string outputFolder = Constants.GetOutputDirectoryPath();
string outputFile = Path.Combine(outputFolder, "merged.pdf");
// Load the source PDF file
using (var merger = new GroupDocs.Merger.Merger(Constants.SAMPLE_PDF))
{
// Add another PDF file to merge
merger.Join(Constants.SAMPLE_PDF_2);
// Merge PDF files and save result
merger.Save(outputFile);
}
Console.WriteLine("\nPDF files merge completed successfully. \nCheck output in {0}", outputFolder);
}
}
}
using GroupDocs.Merger.Domain.Options;
using System;
using System.IO;
namespace GroupDocs.Merger.Examples.CSharp.BasicUsage
{
/// <summary>
/// This example demonstrates how to merge multiple PDF files into single file using bookmarks.
/// For more details about merging Portable Document (.pdf) files please check this documentation article
/// https://docs.groupdocs.com/merger/net/merge/pdf
/// </summary>
internal static class MergePdfUseBookmarks
{
public static void Run()
{
Console.WriteLine("=======================================================================");
Console.WriteLine();
Console.WriteLine("Example Basic Usage: MergePdfUseBookmarks");
Console.WriteLine();
string outputFolder = Constants.GetOutputDirectoryPath();
string outputFile = Path.Combine(outputFolder, "merged.pdf");
// Load the source PDF file
using (var merger = new GroupDocs.Merger.Merger(Constants.SAMPLE_PDF))
{
// Init PdfJoinOptions with UseBookmarks flag
PdfJoinOptions pdfJoinOptions = new PdfJoinOptions();
pdfJoinOptions.UseBookmarks = true;
// Add another PDF file to merge
merger.Join(Constants.SAMPLE_PDF_BOOKMARKS, pdfJoinOptions);
// Merge PDF files and save result
merger.Save(outputFile);
}
Console.WriteLine("\nPDF files merge completed successfully. \nCheck output in {0}", outputFolder);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment