This discussion is archived
2 Replies Latest reply: Nov 30, 2012 8:58 AM by 977294 RSS

Snapshot full WebView

970342 Newbie
Currently Being Moderated
WebView.snapshot method snapshots only visible area on the screen. Unscrolled area is not snapshoted :( How to snapshot full web page?
  • 1. Re: Snapshot full WebView
    jsmith Guru
    Currently Being Moderated
    I tried this and failed. My idea was to render the WebView to an offscreen scene of arbitrary size and take a snapshot of that. That process has worked for me in the past with other node types (such as graphs). But it didn't work for WebView (the image of the snapshoted node from the offscreen scene was just blank). My guess is that the rendering pipeline when WebView is involved may be different than some of the other nodes. I'll post the dummy test harness code for my failed attempt here in case somebody has some different idea. Just change the commented code to switch between rendering to the onscreen scene (for which snapshots work) and the offscreen scene (for which snapshots don't).
    import javafx.animation.PauseTransition;
    import javafx.application.Application;
    import javafx.beans.value.ChangeListener;
    import javafx.beans.value.ObservableValue;
    import javafx.event.ActionEvent;
    import javafx.event.EventHandler;
    import javafx.scene.*;
    import javafx.scene.control.*;
    import javafx.scene.image.*;
    import javafx.scene.layout.VBox;
    import javafx.scene.web.WebView;
    import javafx.stage.Stage;
    import javafx.util.Duration;
    import org.w3c.dom.Document;
    
    public class WebViewSnapshot extends Application {
      public static void main(String[] args) { launch(args); }
      @Override public void start(Stage stage) throws Exception {
        final ImageView snapshotView = new ImageView();
        
        final VBox vbox = new VBox(10);
        final ProgressIndicator indicator = new ProgressIndicator();
    
        final WebView webView = new WebView();
        webView.setPrefSize(800, 300);
        webView.getEngine().documentProperty().addListener(new ChangeListener<Document>() {
          @Override public void changed(ObservableValue<? extends Document> observable, Document oldValue, Document newValue) {
            System.out.println("Document Changed: " + webView.getEngine().getLocation());
            // pause for a period to give the document time to render.
            PauseTransition pt = new PauseTransition(Duration.seconds(5));
            pt.setOnFinished(new EventHandler<ActionEvent>() {
              @Override public void handle(ActionEvent event) {
                System.out.println("Taking snapshot: " + webView.getEngine().getLocation());
                Image image = webView.snapshot(null, null);
                System.out.println("Snapshot Taken: " + webView.getEngine().getLocation());
                snapshotView.setImage(image);
                
                if (vbox.getChildren().contains(indicator)) {
                  vbox.getChildren().remove(indicator);
                  vbox.getChildren().add(snapshotView);
                }  
              }
            });
            pt.play();
          }
        });
        webView.getEngine().load("http://www.anandtech.com");
        
    //    final Scene snapshotScene = new Scene(webView, 800, 600); // uncomment to render in off screen scene
    //    vbox.getChildren().addAll(indicator);                     // uncomment to render in off screen scene
        vbox.getChildren().addAll(new Label("WebView: "), webView, new Label("Snapshot: "), indicator);           // comment to render in off screen scene
        
        stage.setScene(new Scene(vbox, 800, 800));
        stage.show();
      }
    }
    My guess is you will need to end up creating a feature request for this at http://javafx-jira.kenai.com (you can refer to this thread from your feature request).
  • 2. Re: Snapshot full WebView
    977294 Newbie
    Currently Being Moderated
    There's a request for this here: http://javafx-jira.kenai.com/browse/RT-26571

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points