Merge PDF Files - C#, VB, and Java Code

Posted on 2021-07-13 Halah Villalobos

merge-docs-300

We have done several posts that uses the LEADTOOLS Document Converter to convert and merge multiple images and documents, including PDF. However, if all you need is to merge multiple PDF files into a single PDF file, LEADTOOLS has a PDFFile class that can combine PDF files with one-line of code.

The PDFFile class is available for .NET FX, .NET Core, and Java. In addition to PDF file merge, the PDFFile class provides much more including getting information about the file, getting PDF metadata, encrypting and decrypting PDFs, distilling PostScript to PDF, digital signing, bookmarking, annotating, and web optimizing.

Below are some C#, VB, and Java code snippets that can be used to combine PDF files into one PDF file.

Merge PDF Files C# Code


private static void Main(string[] args)
{
   // Check to make sure number of args is correct, files exist, etc...
   if (FailsPreFlight(args)) return;

   try
   {
      MergePdfFiles(args.Skip(1).ToArray(), args[0]);
   }
   catch (Exception ex)
   {
      Console.WriteLine(ex.Message);
   }
}

private static void MergePdfFiles(string[] sourceFilePaths, string destinationFilePath)
{
   // create the destination file
   File.Copy(sourceFilePaths.First(), destinationFilePath, true);
   new PDFFile(destinationFilePath).MergeWith(
      sourceFilePaths.Skip(1).ToArray(), 
      destinationFilePath);
}

Merge PDF Files VB Code


New PDFFile(destinationFilePath).MergeWith( _ 
   sourceFilePaths.Skip(1).ToArray(), destinationFilePath)

Merge PDF Files Java Code


new PDFFile(destinationFilePathString).mergeWith(
    Arrays.copyOfRange(sourceFilePathStrings, 1, sourceFilePathStrings.length), 
    destinationFilePathString);

The LEADTOOLS PDFFile class makes merging PDF files quick work for applications that will be running on Windows, ASP.NET, macOS, iOS, Android, and Linux. The library provides complete control over the processfor times you need to do more than just merge.

See For Yourself - Free Evaluation

Download the LEADTOOLS SDK for free. It’s fully-functional for 60 days and comes with free chat and email support.

Stay Tuned For More Conversion Samples

Did you see our previous post, “Convert Multi-page TIFF to Searchable PDF in C#, Java, and C”? Stay tuned for more conversion examples to see how LEADTOOLS easily fits into any workflow converting PDF files into other document files or images and back again. Need help in the meantime? Contact our support team for free technical support! For pricing or licensing questions, you can contact our sales team via email or call us at +1-704-332-5532.

LEADTOOLS Blog

LEADTOOLS Powered by Apryse,the Market Leading PDF SDK,All Rights Reserved