簡體   English   中英

在 javafx 應用程序中打開 PDF

[英]Opening a PDF in a javafx aplication

我正在開發一個 javafx 應用程序,當我按下一個按鈕時它會打開一個 PDF。 我在 linux 中使用 xdg-open 命令,如下所示:

String[] command = {"xdg-open",path}
Process p = Runtime.getRuntime().exec(command);
p.waitFor();

但是當我按下按鈕時,什么也沒有發生。 我在一個不同的項目中對其進行了測試,它可以毫無問題地打開 PDF。 知道如何解決這個問題嗎?

這是我使用的方法。 Desktop.getDesktop().open()方法的簡單調用將使用系統的默認應用程序打開任何給定的File

這還將在后台Thread打開文件,因此您的應用程序在等待文件加載時不會掛起。

public static void openFile(File file) throws Exception {
    if (Desktop.isDesktopSupported()) {
        new Thread(() -> {
            try {
                Desktop.getDesktop().open(file);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }).start();
    }
}

此代碼在默認瀏覽器中顯示文檔:

File file = new File("C:/filePath/Test.pdf");
HostServices hostServices = getHostServices();
hostServices.showDocument(file.getAbsolutePath());

我希望這有幫助!!

我使用org.icepdf.core.pobjects.Documentorg.icepdf.core.pobjects.Document來渲染我的 PDF 頁面; 如此處所述 這給出了ava.awt.image.BufferedImage一個ava.awt.image.BufferedImage 我將其轉換為 JavaFX 節點:

Image fxImage = SwingFXUtils.toFXImage(bufferedImage, null);

ImageView imageView = new ImageView(fxImage);

從那里您可以在 JavaFX 中編寫自己的簡單分頁查看器。 渲染速度很快,結果看起來符合預期。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM