2 Replies Latest reply on Nov 30, 2012 4:58 PM by 977294

    Snapshot full WebView

      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
          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());
                      if (vbox.getChildren().contains(indicator)) {
          //    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));
          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
            There's a request for this here: http://javafx-jira.kenai.com/browse/RT-26571