Search code examples
javaapachepdfnullpointerexceptionpdfbox

List pdf Attachments using PDFBox (Java)


I'd like to get all filenames of attachments/embedded files of a PDF document. I've been searching for a long time now, but my code still doesn't work.

What I tried:

File input = new File(inputfile); // Input File Path, Given as param from args[]
pd = PDDocument.load(input);
PDDocumentNameDictionary names = new PDDocumentNameDictionary(pd.getDocumentCatalog());
PDEmbeddedFilesNameTreeNode efTree = names.getEmbeddedFiles();
Map<String, COSObjectable> existedNames = efTree.getNames();

System.out.println(existedNames);//Print Embedded-Filenames to console
pd.close();

I don't know if it is even possible to print the content of a MAP to console. I'm coding in eclipse which doesn't give me any errors. But when I run the jar File I get always: NullPointerException at org.apache.pdfbox.pdmodel.PDDocument.getDocumentCatalog(PDDocument.java:778)

Any ideas or help? Many thanks...


Solution

  • Finally found a solution. For anyone with the same problem, the following code worked for me:

    PDDocument pd;
    
    File input = new File(inputfile); // Input File
    
    pd = PDDocument.load(input);
    
    //Writes all embedded Filenames (from pdf document) into Logfile
    try{
        PDDocumentCatalog catalog = pd.getDocumentCatalog();
        PDDocumentNameDictionary names = catalog.getNames();
        PDEmbeddedFilesNameTreeNode embeddedFiles = names.getEmbeddedFiles();
        Map<String, COSObjectable> embeddedFileNames = embeddedFiles.getNames();
    
        //For-Each Loop is used to list all embedded files (if there is more than one)          
        for (Map.Entry<String, COSObjectable> entry : embeddedFileNames.entrySet())
        {   
            //You might need to configure the logger first
            logger.info("Inputfile: " + inputfile +"Found embedded File: " + entry.getKey() + ":");
        }
    
    }
    catch (Exception e){
        System.out.println("Document has no attachments. ");
    }