我正在使用 Java 和 Apache Commons-IO 下载 PDF,但我只想获取第一页,有什么方法可以做到吗?
这是获取整个文档的代码片段:
public void getPDF(String route) throws IOException {
URL url = new URL(route);
File file = new File("file.pdf");
FileUtils.copyURLToFile(url, file);
}
最佳答案
继续您的代码,您可以使用一个新文档来仅保存给定 PDF 文件的第一页。
URL url = new URL(route);
File file = new File("file.pdf");
FileUtils.copyURLToFile(url, file);
PDDocument pdDoc = PDDocument.load(file);
PDDocument document = null;
int pageNumberToRead=0;
try {
document = new PDDocument();
document.addPage((PDPage) pdDoc.getDocumentCatalog().getAllPages().get(pageNumberToRead));
document.save("basepath/first_page.pdf");
document.close();
}catch(Exception e){}
关于java - Apache Commons IO 仅下载第一个 PDF 页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54703536/