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

Apache Commons IO download only first PDF page

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