2016-11-22 8 views
0

나는 링크가있는 웹 페이지에서 작업 중입니다.이 링크를 클릭하면 새 창에서 pdf 파일이 열립니다. 완료된 트랜잭션에 대해 일부 데이터의 유효성을 검사하려면 해당 pdf 파일을 읽어야합니다. 한 가지 방법은 해당 파일을 다운로드 한 다음 사용하는 것입니다. 아무도 나를 도울 수 있습니까? IE 11에서 작업해야합니다.셀렌을 사용하여 pdf 파일을 읽는 방법

감사합니다.

+0

같은 :

strURL는 웹 .pdf 파일을 포함 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

첫 번째 다운로드 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; 
    }