简体   繁体   English

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

[英]Apache Commons IO download only first PDF page

I'm using Java with Apache Commons-IO to download a PDF but I only want to get the first page, is there a way I can do it? 我正在将Java与Apache Commons-IO一起使用来下载PDF,但是我只想获得首页,有没有办法做到这一点?

Here's the piece of code that gets the whole doc: 这是获取整个文档的代码:

public void getPDF(String route) throws IOException {
    URL url = new URL(route);
    File file = new File("file.pdf");
    FileUtils.copyURLToFile(url, file);
}

In continuation to your code, you may use a new Document to hold only first page of given PDF 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){}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM