How to Merge PDFs in Java
Combine two or more PDF files (pdf) into a single PDF document, preserving the order of the input documents in the combined document
Join the DZone community and get the full member experience.
Join For FreeFor both small businesses and large corporations, keeping your important files organized will improve your workflow and exponentially grow your organization’s productivity. PDF documents are often the ideal format for sharing large quantities of information due to their security and flexibility with the types of input formats they accept.
When needing to share several PDF documents at the same time, however, it may be more useful to merge similar documents into one file. For example, if you are sharing or collecting order forms, contracts, invoices, or statements, storing all like documents in one file will greatly improve your productivity and organization, especially if the forms must be sorted in a specific way. This will save you and the recipient the wasted time and hassle that might occur from losing track of separate documents or if the documents are read in the incorrect order.
With the two following Convert APIs, we will show you how you can merge two or more PDF documents into one file to help you stay organized and make sharing your documents easier. Our main goals for this process are to maintain formatting and preserve the order of documents after merging.
The first API shown here will merge two PDF documents in Java. Our first step should be to install our library with a repository reference for our Maven POM file.
xxxxxxxxxx
<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
Then, you can add a reference to the dependency in pom.xml:
xxxxxxxxxx
<dependencies>
<dependency>
<groupId>com.github.Cloudmersive</groupId>
<artifactId>Cloudmersive.APIClient.Java</artifactId>
<version>v3.54</version>
</dependency>
</dependencies>
To install with Gradle, add this in your root build.gradle at the end of repositories:
xxxxxxxxxx
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
And, add the dependency in build.gradle:
xxxxxxxxxx
dependencies {
implementation 'com.github.Cloudmersive:Cloudmersive.APIClient.Java:v3.54'
}
Then, within our function, we will add our imports to the top of the file:
xxxxxxxxxx
// Import classes:
//import com.cloudmersive.client.invoker.ApiClient;
//import com.cloudmersive.client.invoker.ApiException;
//import com.cloudmersive.client.invoker.Configuration;
//import com.cloudmersive.client.invoker.auth.*;
//import com.cloudmersive.client.MergeDocumentApi;
Once we have completed this step, we can call our function, MergeDocumentPdf:
xxxxxxxxxx
ApiClient defaultClient = Configuration.getDefaultApiClient();
// Configure API key authorization: Apikey
ApiKeyAuth Apikey = (ApiKeyAuth) defaultClient.getAuthentication("Apikey");
Apikey.setApiKey("YOUR API KEY");
// Uncomment the following line to set a prefix for the API key, e.g. "Token" (defaults to null)
//Apikey.setApiKeyPrefix("Token");
MergeDocumentApi apiInstance = new MergeDocumentApi();
File inputFile1 = new File("/path/to/inputfile"); // File | First input file to perform the operation on.
File inputFile2 = new File("/path/to/inputfile"); // File | Second input file to perform the operation on (more than 2 can be supplied).
try {
byte[] result = apiInstance.mergeDocumentPdf(inputFile1, inputFile2);
System.out.println(result);
} catch (ApiException e) {
System.err.println("Exception when calling MergeDocumentApi#mergeDocumentPdf");
e.printStackTrace();
}
To make sure that this and our next API are running correctly, we need to ensure the following:
Make sure your input files are valid
Set your API Key; this is available for free from the Cloudmersive website and will give you access to up to 800 calls across our library of APIs
If you need to merge more than two files, the following function will allow you to insert a list of up to ten input files to be combined into one PDF. When using this function, take note that the order in which the input files are placed will be preserved in the combined document. If needed, you may also perform this action more than once and use your output file from the first instance as the first input for the second, followed by the rest of the documents you wish to merge.
To run our second API, you should first perform the same install steps as outlined above. Then, we will once again place our imports at the top of the file and call our function, MergeDocumentPdfMulti:
// Import classes:
//import com.cloudmersive.client.invoker.ApiClient;
//import com.cloudmersive.client.invoker.ApiException;
//import com.cloudmersive.client.invoker.Configuration;
//import com.cloudmersive.client.invoker.auth.*;
//import com.cloudmersive.client.MergeDocumentApi;
ApiClient defaultClient = Configuration.getDefaultApiClient();
// Configure API key authorization: Apikey
ApiKeyAuth Apikey = (ApiKeyAuth) defaultClient.getAuthentication("Apikey");
Apikey.setApiKey("YOUR API KEY");
// Uncomment the following line to set a prefix for the API key, e.g. "Token" (defaults to null)
//Apikey.setApiKeyPrefix("Token");
MergeDocumentApi apiInstance = new MergeDocumentApi();
File inputFile1 = new File("/path/to/inputfile"); // File | First input file to perform the operation on.
File inputFile2 = new File("/path/to/inputfile"); // File | Second input file to perform the operation on.
File inputFile3 = new File("/path/to/inputfile"); // File | Third input file to perform the operation on.
File inputFile4 = new File("/path/to/inputfile"); // File | Fourth input file to perform the operation on.
File inputFile5 = new File("/path/to/inputfile"); // File | Fifth input file to perform the operation on.
File inputFile6 = new File("/path/to/inputfile"); // File | Sixth input file to perform the operation on.
File inputFile7 = new File("/path/to/inputfile"); // File | Seventh input file to perform the operation on.
File inputFile8 = new File("/path/to/inputfile"); // File | Eighth input file to perform the operation on.
File inputFile9 = new File("/path/to/inputfile"); // File | Ninth input file to perform the operation on.
File inputFile10 = new File("/path/to/inputfile"); // File | Tenth input file to perform the operation on.
try {
byte[] result = apiInstance.mergeDocumentPdfMulti(inputFile1, inputFile2, inputFile3, inputFile4, inputFile5, inputFile6, inputFile7, inputFile8, inputFile9, inputFile10);
System.out.println(result);
} catch (ApiException e) {
System.err.println("Exception when calling MergeDocumentApi#mergeDocumentPdfMulti");
e.printStackTrace();
}
Published at DZone with permission of Brian O'Neill. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments