PDFBox - 加载后文档为空

PDFBox - document is empty after loading

我正在使用 Apache PDFBox 呈现 PDF 文档的缩略图。因此,我加载 PDF 并将第一页用作缩略图。问题是,对于某个特定文档,它似乎没有正确加载。对于所有其他文档,它按预期工作。

ByteArrayInputStream is = new ByteArrayInputStream(pdfData);

PDDocument pdf = PDDocument.load(is, true);

List<PDPage> pages = pdf.getDocumentCatalog().getAllPages(); //pages is empty here

pdf 文件有 238 页,大小约为 6.5 MB。

假设您使用的是 1.8.* 版本,请使用非顺序解析器:

PDDocument pdf = PDDocument.loadNonSeq(is, null);

非顺序解析器在旧解析器失败的某些情况下是成功的,例如对于有修订的 PDF (example)。另一个优点是 "protected" 使用空密码加密的 PDF 不需要额外的代码。