How to merge all the pdf files into one using java?

278 views Asked by At

In a folder I have few pdf files, I want to merge all the files into one.using java how can I achieve this ?

1

There are 1 answers

0
GeoSn0w On

You can use PDFMergerUtility (https://pdfbox.apache.org/docs/2.0.0/javadocs/org/apache/pdfbox/multipdf/PDFMergerUtility.html) for this:

PDFMergerUtility pdfMerge = new PDFMergerUtility();
pdfMerge.addSource("/pdf1.pdf");
pdfMerge.addSource("/pdf2.pdf");
// Add as many pdfMerge.addSource("/pdfx.pdf"); as you want
pdfMerge.setDestinationFileName("/pdf-final.pdf");
pdfMerge.mergeDocuments();

This has also been extensively discussed and answered here: How to merge two PDF files into one in Java?