1

我需要在 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();
}

问题是我不知道文档的高度和宽度。

4

1 回答 1

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());
于 2014-03-11T10:51:33.117 回答