【发布时间】:2016-07-09 12:27:04
【问题描述】:
我想获取所有PDF文档的附件/嵌入文件的文件名。我已经搜索了很长时间,但我的代码仍然无法正常工作。
我尝试了什么:
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();
我不知道是否可以将 MAP 的内容打印到控制台。我在 eclipse 中编码,这不会给我任何错误。但是当我运行 jar 文件时,我总是得到:NullPointerException at org.apache.pdfbox.pdmodel.PDDocument.getDocumentCatalog(PDDocument.java:778)
有什么想法或帮助吗?非常感谢...
【问题讨论】:
-
PDDocument.load() 可能会失败并返回 null
-
PDDocument.load() 有效。我知道,因为我之前正在用“pd”做其他事情......接下来的 3 行(加载 pd = ...)一定是错误的:/
-
在几个例子中,我看到了
Map<String, PDComplexFileSpecification> existedNames = efTree.getNames();,但是当我在 Eclipse 中更改它时,它给了我一个错误:Type mismatch: Cannot convert from Map<String, COSObjectable> to Map<String, PDComplexFileSpecification>
标签: java apache pdf nullpointerexception pdfbox