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 Free
For 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.
Then, you can add a reference to the dependency in pom.xml:
To install with Gradle, add this in your root build.gradle at the end of repositories:
And, add the dependency in build.gradle:
Then, within our function, we will add our imports to the top of the file:
Once we have completed this step, we can call our function, MergeDocumentPdf:
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:
Published at DZone with permission of Katherine Coyle. See the original article here.
Opinions expressed by DZone contributors are their own.