我目前正在使用 PDFBox 版本 3.0.1,在尝试从 URL 加载 PDF 时遇到挑战。似乎没有为此目的提供具体方法。
下面是我尝试过的代码片段,但它没有按预期运行:
public void loadPDF(String url, String name) throws IOException {
PDDocument document = null;
URL pdfUrl = new URL(url);
URLConnection urlConnection = pdfUrl.openConnection();
InputStream ip = urlConnection.getInputStream();
BufferedInputStream bf = new BufferedInputStream(ip);
try {
document = Loader.loadPDF((RandomAccessRead) ip);
} catch (IOException e) {
Assert.fail("Could not load PDF. Please check the URL again");
}
}
有人可以提供从 PDFBox 3.0.1 中的 URL 加载 PDF 的指导或替代方法吗?
从
InputStream
获得的 URLConnection
不会实现 org.apache.pdfbox.io.RandomAccessRead
,因此转换将会失败。
您需要使用
Loader.loadPDF(RandomAccessBuffer.createBufferFromStream(is))
(并摆脱 BufferedInputStream
的构造)。