JavaFX: Display PDF in WebView

2019-07-14 07:05发布

问题:

I need to display a PDF inside the default WebView of JavaFX. I assumed, that i would easily be able to do that like this.

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import javafx.stage.Stage;

public class ShowPdfTest extends Application {
    public static void main(String[] args) {
        launch();
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        WebView webView = new WebView();
        WebEngine engine = webView.getEngine();
        Scene scene = new Scene(webView);
        primaryStage.setScene(scene);
        primaryStage.show();

//        engine.load("https://www.google.com");
        engine.load("http://www.orimi.com/pdf-test.pdf");
    }
}

I was wrong. Nothing happens. It seems like the WebEngine has no built-in PDF-Renderer. I tried JxBrowser which worked fine, but is a rather costly alternative.

So is there any way to display a PDF directly inside the default WebView component?

回答1:

JxBrowser which worked fine, but is a rather costly alternative.

If you your application is an open-source project, you can obtain the JxBrowser Open-Source license here.