猿问

Apache Commons IO 仅下载第一个 PDF 页面

我正在使用 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);

}


ITMISS
浏览 127回答 1
1回答

慕森王

继续您的代码,您可以使用新文档来仅保存给定 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){}
随时随地看视频慕课网APP

相关分类

Java
我要回答