Apache PDFBox 找不到 class 'Loader'。为什么?

Apache PDFBox cannot find class 'Loader'. Why?

我正在使用 pdfbox-app-2.0.18.jarpdfbox-app-2.0.17.jar.

根据示例 here,我有以下代码:

try (FileOutputStream fos = new FileOutputStream(signedFile);
     PDDocument doc = Loader.loadPDF(inputFile)) {
    
     // code

}

执行此代码后,出现以下错误:

org.apache.pdfbox.Loader is not found 

如何解决这个问题?

Loader class 从未在 2.x 或更低版本中引入。所以,你不能使用它。

或者,您可以使用 PDDocument class 中的 load() 方法加载 PDF 文件。

修改为:

try (FileOutputStream fos = new FileOutputStream(signedFile);
     PDDocument document = PDDocument.load(inputFile)) {

        // code 

}

读这个:- https://pdfbox.apache.org/2.0/migration.html

加载程序 class 已于 2020 年 1 月 25 日添加。SVN log

它不是版本 2.0.18 的一部分,因为它不在这个文件中: pdfbox-2.0.18-src.zip

所以这个 class 太新了,所以你不能使用它!

PDDocument class 将代表正在处理的 PDF 文档。它的 load() 方法将加载 File 对象指定的 PDF 文件:

PDDocument document = PDDocument.load(new File("path/to/pdf"));