Link
Skip to main content

Merge PDF files

v2025.05

JPedal provides several methods to allow easy merging of PDFs into a single new file. This tool produces a single file containing all the pages from the provided files. The original files are left untouched by this process.

Merge PDFs into a single file from the Command-Line or another language

PdfMerge can be used from the command to merge pages from multiple files into a new single file. The arguments include input files, options, and the output file.
The input files and options can be in any order and the files will be merged in the order they appear in the arguments.
The last argument must always be the output file for all merged pages.

java -cp jpedal.jar org.jpedal.tools.PdfMerge [mergeOptions] inputFile1.pdf inputFile2.pdf... outputFile.pdf

Merge PDF files in Java

PdfMerge has a series of static convenience methods for performing merges.
These methods cover common use cases for PdfMerge to make using the tool quick and simple.

Merge 2 Files
These methods accept 2 File objects and merge the pages of those PDFs into a single file. Some methods allow passwords to be supplied and\or merge options to control different aspects of the merge.

//Merge two files into a single file
PdfMerge.mergeFiles(new File("inputFile1.pdf"), new File("inputFile2.pdf"), new File("outputFile.pdf"));

//Merge two files with passwords into a single file
PdfMerge.mergeFiles(new File("inputFile1.pdf"), "inputFile1_Password", new File("inputFile2.pdf"), "inputFile2_Password", new File("outputFile.pdf"));


//Create an array of Merge Options
final ArrayList<MergeOption> options = new ArrayList<>();
//options.add(MergeOption.INTERLEAVE); //Example showing use of interleave option

//Merge two files into a single file with additional options
PdfMerge.mergeFiles(new File("inputFile1.pdf"), new File("inputFile2.pdf"), new File("outputFile.pdf"), options);

//Merge two files with passwords into a single file with additional options
PdfMerge.mergeFiles(new File("inputFile1.pdf"), "inputFile1_Password", new File("inputFile2.pdf"), "inputFile2_Password", new File("outputFile.pdf"), options);

Merge an Array of Files
These methods accept a File Array and merges the pages of those PDFs into a single file. Some methods allow passwords to be supplied as a String array that must match the length of the input file array. Some methods also allow merge options to be provided and control different aspects of the merge.

//Merge an array of PDFs into a single file
PdfMerge.mergeFiles(new File[]{new File("inputFile.pdf"), ...}, new File("outputFile.pdf"));

//Merge an array of PDFs with passwords into a single file
//Input File array and Password array must be the same length
PdfMerge.mergeFiles(new File[]{new File("inputFile.pdf"), ...}, new String[]{"inputFile_password", ...}, new File("outputFile.pdf"));


//Create an array of Merge Options
final ArrayList<MergeOption> options = new ArrayList<>();
//options.add(MergeOption.INTERLEAVE); //Example showing use of interleave option

//Merge an array of PDFs into a single file
PdfMerge.mergeFiles(new File[]{new File("inputFile.pdf"), ...}, new File("outputFile.pdf"), options);

//Merge an array of PDFs with passwords into a single file
//Input File array and Password array must be the same length
PdfMerge.mergeFiles(new File[]{new File("inputFile.pdf"), ...}, new String[]{"inputFile_password", ...}, new File("outputFile.pdf"), options);

PdfMerge Merge Options

The PdfMerge has some options that will allow you to control different aspects of the merge. These can be passed into the merge methods or set in the command line as follows.

Interleave

By default PdfMerge will merge add pages from the first file then add all pages from each file that follows. When using the interleave options the pages are added one at a time from each file.
This results in the first page of each file being added in order, following by the second, and so forth. If a file has no more pages to merge the remaining files will continue to merge following the same rule.

Command Line Arguments

--interleave

MergeOption Value

MergeOption.INTERLEAVE

Why JPedal?

  • Actively developed commercial library with full support and no third party dependencies.
  • Process PDF files up to 3x faster than alternative Java PDF libraries.
  • Simple licensing options and source code access for OEM users.

Learn more about JPedal

Start Your Free Trial


Customer Downloads

Select Download