PDF阅读使用PDF盒子-澄清与页数

  • 本文关键字:PDF 盒子 java pdfbox pdf-reader
  • 更新时间 :
  • 英文 :


使用PDFbox从url读取pdf文件,下面的代码可以完美地读取pdf并存储在项目位置

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文件

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

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);
    }   
}

最新更新