0

I have many PDF into my folder (C:\Users\Username\Desktop\PDF):

documents

I want to create a simple program to merge all these documents into one with PdfBox. So I create this code:

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import org.apache.pdfbox.multipdf.PDFMergerUtility;

    public class MergePDF {
    
        public static void main (String args[]) throws IOException {
            
            PDFMergerUtility PDFmerger = new PDFMergerUtility();
            PDFmerger.setDestinationFileName("C:\\Users\\fgioli\\Desktop\\PDF\\MergePDF.pdf");
            
            File pdfFolder = new File("C:\\Users\\fgioli\\Desktop\\PDF");
                
            File currentFile;
            
            for(File fileEntry : pdfFolder.listFiles()) {
                System.out.println("File entry: "+fileEntry.getAbsolutePath());
                
                currentFile = new File(fileEntry.getAbsolutePath());
                PDFmerger.addSource(currentFile);
            }
            
            PDFmerger.mergeDocuments();
            
            System.out.println("END");
        }
    }

If I try to execute the code I get the error:

Exception in thread "main" java.io.FileNotFoundException: C:\Users\Username\Desktop\PDF\Merge (Access is denied)
    at java.io.RandomAccessFile.open0(Native Method)
    at java.io.RandomAccessFile.open(Unknown Source)
    at java.io.RandomAccessFile.<init>(Unknown Source)
    at org.apache.pdfbox.io.RandomAccessBufferedFileInputStream.<init>(RandomAccessBufferedFileInputStream.java:99)
    at org.apache.pdfbox.pdmodel.PDDocument.load(PDDocument.java:1079)
    at org.apache.pdfbox.pdmodel.PDDocument.load(PDDocument.java:1006)
    at org.apache.pdfbox.multipdf.PDFMergerUtility.legacyMergeDocuments(PDFMergerUtility.java:451)
    at org.apache.pdfbox.multipdf.PDFMergerUtility.mergeDocuments(PDFMergerUtility.java:346)
    at org.apache.pdfbox.multipdf.PDFMergerUtility.mergeDocuments(PDFMergerUtility.java:330)
    at MergePDF.main(MergePDF.java:44)

How can I solve this problem?

TontonVelu
  • 471
  • 2
  • 8
  • 21
  • 2
    Does this answer your question? [java.io.FileNotFoundException: (Access is denied)](https://stackoverflow.com/questions/4281143/java-io-filenotfoundexception-access-is-denied) – Emmanuel Chebbi Jun 30 '20 at 09:08
  • 2
    Basically, you have to check that `currentFile` is actually a PDF file (and not a folder) before giving it to `PDFmerger` – Emmanuel Chebbi Jun 30 '20 at 09:10
  • Thank you Emmanuel! I add this code and It works: for(File fileEntry : pdfFolder.listFiles()) { if(fileEntry.getAbsolutePath().contains(".pdf")) { System.out.println("File entry: "+fileEntry.getAbsolutePath()); currentFile = new File(fileEntry.getAbsolutePath()); PDFmerger.addSource(currentFile); } } – Francesco Gioli Jun 30 '20 at 09:37
  • Better:for(File fileEntry : pdfFolder.listFiles()) { if(!fileEntry.isDirectory()) { System.out.println("File entry: "+fileEntry.getAbsolutePath()); currentFile = new File(fileEntry.getAbsolutePath()); PDFmerger.addSource(currentFile); } } PDFmerger.mergeDocuments(); – Francesco Gioli Jun 30 '20 at 09:48
  • I would advise you to call `isFile` to make sure you're actually dealing with files (folders' name may contain ".pdf" as well) and to check whether the file's _extension_ is "pdf": your current code will match any file which name is "a.pdf.png" or that is contained in a "a.pdf.b" folder. Your code works for your use case but I believe it's good practice to take care of possible error cases. – Emmanuel Chebbi Jun 30 '20 at 09:52

0 Answers0