2016-11-22 6 views
0

私はリンクがあるウェブページ上で作業しています。そのリンクをクリックすると、新しいウィンドウでPDFファイルが開きます。 完了したトランザクションに対していくつかのデータを検証するには、そのpdfファイルを読む必要があります。 1つの方法は、そのファイルをダウンロードし、それを使用することです。 これについて私は誰でも助けてくれますか?私はIE 11で作業する必要がありますセレンを使用してPDFファイルを読む方法

ありがとうございました。

+0

urlがその.pdfファイルを含むWeb URLです。 pdfファイルをダウンロードし、PDFboxやその他のライブラリを使用してファイルを読み込む。 – metar

答えて

1

PDFBoxとFontBoxを使用してください。

public String readPDFInURL() throws EmptyFileException, IOException { 
     WebDriver driver = new FirefoxDriver(); 
     // page with example pdf document 
     driver.get("file:///C:/Users/admin/Downloads/dotnet_TheRaceforEmpires.pdf"); 
     URL url = new URL(driver.getCurrentUrl()); 
     InputStream is = url.openStream(); 
     BufferedInputStream fileToParse = new BufferedInputStream(is); 
     PDDocument document = null; 
     try { 
      document = PDDocument.load(fileToParse); 
      String output = new PDFTextStripper().getText(document); 
     } finally { 
      if (document != null) { 
       document.close(); 
      } 
      fileToParse.close(); 
      is.close(); 
     } 
     return output; 
    } 

古いバージョンのPDFBoxの機能の一部は廃止されているため、PDFBoxとともに別のFontBoxを使用する必要があります。私はPDFBox (2.0.3)FontBox (2.0.3)を使用しており、正常に動作しています。それはイメージを読むことはありません。

+0

これが現在のバージョンで動作するかどうかは不明です。 'PDDocument doc = PDDocument.load(url.openStream());'を実行し、不要なコード(COSDocument、PDFParser)をすべて削除してください。 –

+0

次の関数を試してください: –

+0

答えを更新しました。 –

0

First Downlaod pdfbox jar。セレンは動作しません使用してPDFから内容を読み出す

https://example.com/downloads/presence/Online-Presence-CA-05-02-2017-04-13.pdf)のような
public boolean verifyPDFContent(String strURL, String text) { 

     String output =""; 
     boolean flag = false; 
     try{ 
      URL url = new URL(strURL); 
      BufferedInputStream file = new BufferedInputStream(url.openStream()); 
      PDDocument document = null; 
      try { 
       document = PDDocument.load(file); 
       output = new PDFTextStripper().getText(document); 
       System.out.println(output); 
      } finally { 
       if (document != null) { 
        document.close(); 
       } 
      } 
     }catch(Exception e){ 
      e.printStackTrace(); 
     } 
     if(output.contains(text)){ 
      flag = true; 
     } 
     return flag; 
    } 
関連する問題