2013-04-29 59 views
1

我需要得到內部的WebView整個HTML文檔的快照,我這樣做是在這樣的方式:如何在WebView中獲取文檔的尺寸?

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

以下代碼給出了寬度&的高度。它運行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());