I want to merge many PDF files into one using PDFBox and this is what I've done:
PDDocument document = new PDDocument(); for (String pdfFile: pdfFiles) { PDDocument part = PDDocument.load(pdfFile); List<PDPage> list = part.getDocumentCatalog().getAllPages(); for (PDPage page: list) { document.addPage(page); } part.close(); } document.save("merged.pdf"); document.close();
Where pdfFiles
is an ArrayList<String>
containing all the PDF files.
When I'm running the above, I'm always getting:
org.apache.pdfbox.exceptions.COSVisitorException: Bad file descriptor
Am I doing something wrong? Is there any other way of doing it?
Open Acrobat to combine files: Open the Tools tab and select "Combine files." Add files: Click "Add Files" and select the files you want to include in your PDF. You can merge PDFs or a mix of PDF documents and other files.
Select the files you want to merge using the Acrobat PDF combiner tool. Reorder the files if needed. Click Merge files. Download the merged PDF.
Why not use the PDFMergerUtility of pdfbox?
PDFMergerUtility ut = new PDFMergerUtility(); ut.addSource(...); ut.addSource(...); ut.addSource(...); ut.setDestinationFileName(...); ut.mergeDocuments();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With