我正在使用 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);}
1 回答

慕森王
TA贡献1777条经验 获得超3个赞
继续您的代码,您可以使用新文档来仅保存给定 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){}
添加回答
举报
0/150
提交
取消