AVAILABLE NOW: Spring 2025 Release

Merge PDF Files - C# and Java Code

By Apryse | 2025 Apr 03

Sanity Image
Read time

10 min

If you are looking to merge multiple PDF files into a single PDF file, Apryse has an SDK that can combine PDF files easily.

The Apryse SDK is available for .NET, .NET Core, Java, and more. In addition to PDF file merge, the SDK 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# and Java code snippets that can be used to combine PDF files into one PDF file.

Merge PDF Files C# Code

using pdftron;  

using pdftron.PDF;  

using pdftron.SDF;  

  

namespace ConsoleApp1  

{  

    internal class Program  

    {  

        static void Main(string[] args)  

        {  

            PDFNet.Initialize("your_license_key");  

  

            using (PDFDoc doc1 = new PDFDoc("path_to_pdf1.pdf"))  

            using (PDFDoc doc2 = new PDFDoc("path_to_pdf2.pdf"))  

            {  

                doc1.InitSecurityHandler();  

                doc2.InitSecurityHandler();  

  

                // Merge doc2 into doc1  

                doc1.InsertPages(doc1.GetPageCount() + 1, doc2, 1, doc2.GetPageCount(), PDFDoc.InsertFlag.e_none);  

  

                // Save the merged document  

                doc1.Save("merged.pdf", SDFDoc.SaveOptions.e_linearized);  

  

                Console.WriteLine("Documents merged successfully!");  

            }  

        }  

    }  

}  

Merge PDF Files Java Code

import com.pdftron.pdf.*;  

import com.pdftron.sdf.*;  

import com.pdftron.common.PDFNetException;  

  

public class App {  

public static void main(String[] args) {  

    try {  

      PDFNet.initialize("your_license_key");  

  

      PDFDoc doc1 = new PDFDoc("path_to_pdf1.pdf");  

      PDFDoc doc2 = new PDFDoc("path_to_pdf2.pdf");  

      doc1.initSecurityHandler();  

      doc2.initSecurityHandler();  

  

      // Merge doc2 into doc1  

      doc1.insertPages(doc1.getPageCount() + 1, doc2, 1, doc2.getPageCount(), PDFDoc.InsertBookmarkMode.NONE, null);  

      // Save the merged document  

      doc1.save("src\\samples\\merged.pdf", SDFDoc.SaveMode.LINEARIZED, null);  

  

      System.out.println("Documents merged successfully!");  

  

      doc1.close();  

      doc2.close();  

     

    } catch (PDFNetException e) {  

      e.printStackTrace();  

    }  

  }  

}  

The Apryse SDK 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 Apryse SDK for free. It’s fully-functional for 60 days and comes with free chat and email support.

Stay Tuned for More Conversion Samples

Stay tuned for more conversion examples to see how Apryse 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.

Sanity Image

Apryse

Share this post

email
linkedIn
twitter