我正在处理有链接的网页,单击它会在新窗口中打开一个 pdf 文件。 我必须阅读该 pdf 文件以根据已完成的交易验证一些数据。一种方法是下载该文件然后使用它。 谁能帮我解决这个问题。我必须在 IE 11 上工作
提前致谢。
最佳答案
使用 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)它工作正常。但它不会读取图像。
关于java - 如何使用 Selenium 读取pdf文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40738373/