首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用PDF框阅读PDF -带页数的说明

使用PDF框阅读PDF -带页数的说明
EN

Stack Overflow用户
提问于 2016-08-18 22:22:10
回答 1查看 570关注 0票数 1

使用PDFbox从url读取pdf文件,下面的jave代码非常适合读取pdf并存储在项目位置。

代码语言:javascript
复制
String pdfPageCount = 17;
String pdfUrl = "abc.org/invoicepdf.pdf?Range=1";
URL pdfDownload = new URL(pdfUrl);
connectionGet = (HttpsURLConnection) pdfDownload.openConnection();
String authorizationHeader1 = "Bearer " + getToken;
connectionGet.setRequestProperty("Authorization", authorizationHeader1);
connectionGet.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
connectionGet.setRequestMethod("GET");
int responseCode = connectionGet.getResponseCode();
    if (responseCode != 404) {
        PDDocument pd = new PDDocument();
        InputStream inputstreamFinal1 = connectionGet.getInputStream();
        PDDocument load = PDDocument.load(inputstreamFinal1);                        
        load.save("CopyOfInvoice1.pdf");
    }

我的下一步

我想根据pdfPageCount值循环执行进程,目前我在pdfUrl (/invoicepdf.pdf?Range=1)中将页面计数硬编码为1。

期望值:

阅读所有17页并保存到单个pdf文件中

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-08-19 17:10:50

下面是一些基于注释中提到的PDFMergerExample的代码。请注意,我还没有检查您的URL检索代码是否正确。

代码语言:javascript
复制
List<InputStream> sources = new ArrayList<InputStream>();
int pdfPageCount = 17;
try
{
    for (int p = 1; p <= pdfPageCount; ++p)
    {
        String pdfUrl = "abc.org/invoicepdf.pdf?Range=" + p;
        URL pdfDownload = new URL(pdfUrl);
        HttpsURLConnection connectionGet = (HttpsURLConnection) pdfDownload.openConnection();
        String authorizationHeader1 = "Bearer " + getToken;
        connectionGet.setRequestProperty("Authorization", authorizationHeader1);
        connectionGet.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
        connectionGet.setRequestMethod("GET");
        int responseCode = connectionGet.getResponseCode();
        if (responseCode != 404)
        {
            sources.add(connectionGet.getInputStream());
        }
        else
        {
            //TODO error handling
            return;
        }
    }
    PDFMergerUtility pdfMerger = new PDFMergerUtility();
    pdfMerger.addSources(sources);
    pdfMerger.setDestinationFileName("CopyOfInvoice1.pdf");
    pdfMerger.mergeDocuments(MemoryUsageSetting.setupMainMemoryOnly());
}
catch (IOException e)
{
     //TODO error handling
     return;
}
finally
{
    // cleanup
    for (InputStream source : sources)
    {
        IOUtils.closeQuietly(source);
    }   
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39020679

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档