importorg.apache.pdfbox.multipdf.PDFMergerUtility;importorg.apache.pdfbox.pdmodel.PDDocument;importjava.io.File;importjava.io.IOException;publicclassMergingPdfs{publicstaticvoidmain(String[] args)throwsIOException{//Loading an existing PDF document File file1 =newFile("C:/pdfBox/sample1.pdf");PDDocument doc1 =PDDocument.load(file1);File file2 =newFile("C:/pdfBox/sample2.pdf");PDDocument doc2 =PDDocument.load(file2);//Instantiating PDFMergerUtility class PDFMergerUtilityPDFmerger=newPDFMergerUtility();//Setting the destination file PDFmerger.setDestinationFileName("C:/pdfBox/merged.pdf");//adding the source files PDFmerger.addSource(file1);PDFmerger.addSource(file2);//Merging the two documents PDFmerger.mergeDocuments();System.out.println("Documents merged");//Closing the documents
doc1.close();
doc2.close();}}