Skip to content

Instantly share code, notes, and snippets.

@GroupDocsGists
Last active July 8, 2025 11:06
Show Gist options
  • Save GroupDocsGists/1455521a4ccbc62aa04499c8c1af54a9 to your computer and use it in GitHub Desktop.
Save GroupDocsGists/1455521a4ccbc62aa04499c8c1af54a9 to your computer and use it in GitHub Desktop.
How to merge PDF Files in Java

Merge PDF Files in Java

Looking for a quick way to merge PDF files? This Gist shows how to do this using a straightforward Java example that leverages the capabilities of the GroupDocs.Merger library. By combining multiple PDF documents into a single file, you can streamline your workflow and enhance document management.

πŸ“¦ Prerequisites

  • Ensure you have access to GroupDocs.Merger library by installing it in your Java project.
  • Activate your GroupDocs license key for full capabilities. You can get a free trial of GroupDocs.Merger and utilize a temporary license for unrestricted access.
  • This functionality supports file types including PDF and DOCX, allowing for versatile document handling.

🧩 Key Capabilities

  • Effortlessly combine multiple PDF files into a single document.
  • Maintain original formatting during the merging process, avoiding any layout issues.
  • Add various PDFs sequentially for seamless integration of multiple files.
  • Utilize a streamlined interface for quick implementation in Java applications.
  • Easily manage document management tasks with just a few lines of code.

πŸ’» Code Example

See the following example 1: MergePdf.java

βœ… How to Use in Java

  1. Install the GroupDocs.Merger package in your project through Maven or Gradle.
  2. Open the primary PDF file that you want to merge using the Merger class.
  3. Add any additional PDF files you wish to combine by using the join method on the Merger instance.
  4. After adding all files, call the save method to save the merged document to your desired output path.
  5. Check the output folder to ensure that your merged PDF is available for use.

πŸ“Ž Related Resources

πŸ“œ Conclusion

That’s it! Now you know how to merge PDF files efficiently using Java. By following these instructions, you can improve your document handling techniques and enhance productivity. For further exploration, check out our documentation or grab a trial to experience additional features tailored for merging not just PDFs, but also excel files, word documents, and more.

import com.groupdocs.merger.Merger;
import com.groupdocs.merger.examples.Constants;
import java.io.File;
/**
* 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/java/merge/pdf
*/
public class MergePdf
{
public static void run() throws Exception
{
String outputFolder = Constants.OutputPath;
String outputFile = new File(outputFolder, "merged.pdf").getPath();
// Load the source PDF file
Merger merger = new Merger(Constants.SAMPLE_PDF);
{
// Add another PDF file to merge
merger.join(Constants.SAMPLE_PDF_2);
// Merge PDF files ans save result
merger.save(outputFile);
}
System.out.print("\nPDF files merge completed successfully. \nCheck output in "+ outputFolder);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment