JavaFX:在WebView中显示PDF

4
我需要在JavaFX的默认WebView内显示一个PDF文件。我认为,我可以通过以下方式轻松完成此操作。
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");
    }
}

我错了。什么也没发生。看起来WebEngine没有内置的PDF渲染器。我尝试了JxBrowser,它运行良好,但是是一种相对昂贵的替代方案。

那么有没有办法在默认的WebView组件中直接显示PDF呢?


2
你可以在你的WebView中使用Mozilla Pdf.js - Cem Ikta
好的,但是如果我有这样的东西怎么办?https://www.cs.tut.fi/~jkorpela/html/iframe-pdf.html - Anton Horst
你不需要使用iframe来使用Pdf.js!你可以在WebView中调用Pdf.js来打开你的pdf文件。 - Cem Ikta
不,我知道我可以使用Pdf.js在WebView中显示PDF。但是我的目标是像其他浏览器一样动态呈现PDF。因此,如果一个网页在iframe或JavaScript中嵌入了PDF,我希望能够显示整个页面以及嵌入式的PDF。 - Anton Horst
1个回答

1
如果你的应用程序是开源项目,你可以在 这里 获取 JxBrowser 开源许可证。JxBrowser 虽然效果不错,但是价格较高。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接