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.

Add Dividing Page

PdfMerge will add a blank page after the pages of each input documents except the last. The new black page will match the size of the page before it. This option is not compatible with interleave and will do nothing if interleave is selected.

Command Line Arguments

--add_dividing_page

MergeOption Value

MergeOption.ADD_DIVIDING_PAGE

PdfMerge will add a footer to all pages that extends below the content of the original pages. This footer will display the original filename and page number for that page, and can be customised using PdfMergeAttributes.

Command Line Arguments

--add_footer

MergeOption Value

MergeOption.ADD_FOOTER

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

Normalise Page Size

PdfMerge will adjust the sizes of all merged pages to match the first page of the first document,

Command Line Arguments

--normalise_page_size_to_match_first

MergeOption Value

MergeOption.NORMALISE_PAGE_SIZE_TO_MATCH_FIRST

Normalise Page Rotation

PdfMerge will adjust the rotation of all merged pages to match the first page of the first document,

Command Line Arguments

--normalise_rotation_to_match_first

MergeOption Value

MergeOption.NORMALISE_ROTATION_TO_MATCH_FIRST

Using Pdf Merge Attributes

The PdfMerge has some options that create new content in the resulting file. In order to allow you greater control of the outputs appearance you can set attributes that the merge will use when creating new content. These attributes can be defined on the command line by including the key value pairs detailed below or by passing a PdfMergeAttributes Object to an instance of PdfMerge.

Adding attributes via Command Line

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

#For Example, setting the font to use for any new text content
java -cp jpedal.jar org.jpedal.tools.PdfMerge [mergeOptions] --font_name Helevetica inputFile1.pdf inputFile2.pdf... outputFile.pdf

Setting Attributes in Java

final File[] inputFiles = //create your input file array here
final byte[][] passwords = //create your input file password array here
final File outputFile = //create your output file object here

final PdfMergeAttributes attributes = new PdfMergeAttributes();
attributes.setFontName("Helvetica");
attributes.setFontSize(16);

final PdfMerge pdfMerger = new PdfMerge();
pdfMerger.setPdfMergeAttributes(attributes);
pdfMerger.merge(inputFiles, outputFile, passwords);

Pdf Merge Attributes

The following is the list of attributes that can be used to customise new content generated by PdfMerge. Each entry in this list details the attributes use, the command line argument, and the method to set the value in a PdfMergeAttributes object.

Font Name

The font name will specify the font to be used in generating any new text content for the merge. The value for this attribute is limited to one of the 14 pdf base fonts.

Command Line Argument
Key: –font_name
Values: [Courier, Courier-Bold, Courier-Oblique, Courier-BoldOblique, Helvetica, Helvetica-Bold, Helvetica-Oblique, Helvetica-BoldOblique, Times-Roman, Times-Bold, Times-Italic, Times-BoldItalic, Symbol, ZapfDingbats]
Command Line Example:

--font_name Helvetica

PdfMergeAttribute Method

PdfMergeAttributes.setFontName(String fontname);

Font Size

The font size will specify the size of the text to be used in generating any new text content for the merge. The value for this attribute should be a positive integer

Command Line Example
Key: –font_size
Values: A positive integer for the text font size
Command Line Arguments:

--font_size 16

PdfMergeAttribute Method

PdfMergeAttributes.setFontSize(int fontsize);

The footer height controls the extra space added to the bottom of the page when adding a footer. The height is in PDF coordinates and appears below the existing page content.

Command Line Example
Key: –footer_height
Values: A positive integer for the footer height
Command Line Arguments:

--footer_height 30

PdfMergeAttribute Method

PdfMergeAttributes.setFooterHeight(int fontsize);

The footer template controls the text content added when using the add_footer option. The value for this attribute has 2 dynamic variables that will be replaced with document and page specific values. %FILENAME - is replaced with the original files name. %PAGENUMBER - is replaced with the original page number of this page in the original file.

Command Line Example
Key: –footer_template
Values: A string to be added to the footer of each page if add_footer option is being used. Command Line Arguments:

--footer_template "%FILENAME, Page %PAGENUMBER"

PdfMergeAttribute Method

PdfMergeAttributes.setFooterTemplate(String template);

Text Color

The text color will set the color of any text content generated for the merge.

Command Line Example
Key: –text_color
Values: 3 float values representing a RGB color, each value being between 0 and 1 in the order of Red, Green, and Blue Command Line Arguments:

--text_color 0.0 0.0 0.0

PdfMergeAttribute Method

PdfMergeAttributes.setTextColor(float[] color);

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