Merging multiple PDF documents into a single PDF document is done using the PDFMergerUtility class. This class will take a list of PDF documents and merge them, the result will be saved to the new document. The prerequisite of this topic is that you have already installed apache library
Follow these steps to merge multiple PDF documents:
- First we have to instantiate the PDFMergerUtility class.
- Second we have to set the destination file using the setDestinationFileName() method.
- Now we have to set the source files using the addSource() method.
- Final step we have to merge the documents using the mergeDocuments() method of the PDFMergerUtility class.
Input : PDF1 = Alice.pdf, PDF2 = Bob.pdf Output: newMerged.pdf // merged pdf of pdf1 and pdf2
Before the execution of the code :
After the execution of the code :
If you check the specified path, you will see that a PDF document named newMerged.pdf has been generated which contains the pages of both the original documents.
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.