我正在網頁上有一個鏈接,點擊它打開一個PDF文件在新窗口。 我必須閱讀該pdf文件才能根據所完成的交易驗證一些數據。一種方法是下載該文件然後使用它。 任何人都可以幫我解決這個問題。我必須工作在IE 11上如何閱讀使用硒的PDF文件
在此先感謝。
我正在網頁上有一個鏈接,點擊它打開一個PDF文件在新窗口。 我必須閱讀該pdf文件才能根據所完成的交易驗證一些數據。一種方法是下載該文件然後使用它。 任何人都可以幫我解決這個問題。我必須工作在IE 11上如何閱讀使用硒的PDF文件
在此先感謝。
使用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的功能已經過時,我們需要使用另一個FontBox與PDFBox的一起。我已經使用PDFBox (2.0.3)和FontBox (2.0.3),它工作正常。它不會讀取圖像。
不確定這是否適用於當前版本; 'PDDocument doc = PDDocument.load(url.openStream());'然後刪除所有不需要的代碼(COSDocument,PDFParser) –
試試下面的函數: –
我已經更新了我的答案。 –
First Downlaod pdfbox jar。
strURL是一個網頁URL包含.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;
}
使用硒將無法正常工作從閱讀PDF內容。下載PDF文件並使用PDFbox或任何其他庫讀取文件。 – metar