我需要在webview中获取整个html文档的快照,我这样做:
BorderPane pane = new BorderPane();
browser = new WebView();
webEngine = browser.getEngine();
webEngine.loadContent(_content);
pane.setCenter(browser);
Scene scene = new Scene(pane);
try {
SnapshotParameters params = new SnapshotParameters();
Image image = pane.snapshot(params, null);
File out = new File("c:\\1.png");
ImageIO.write( SwingFXUtils.fromFXImage(image, null) , "png", out);
} catch (IOException e) {
e.printStackTrace();
}
问题是我不知道文件的高度和宽度。
答案 0 :(得分:0)
以下代码给出宽度&高度。它运行javascript来获取值。
int height = Integer.parseInt(webEngine.executeScript("var body = document.body, html = document.documentElement; Math.max( body.scrollHeight, body.offsetHeight, html.clientHeight, html.scrollHeight, html.offsetHeight );").toString());
int width = Integer.parseInt(webEngine.executeScript("var body = document.body, html = document.documentElement; Math.max( body.scrollWidth, body.offsetWidth, html.clientWidth, html.scrollWidth, html.offsetWidth );").toString());