从 Java 应用程序即时打开 PDF 文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2546968/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 09:08:42  来源:igfitidea点击:

Open PDF file on the fly from a Java application

javapdf

提问by Mr CooL

Is there any way to have a code that opens a PDF file in Java application in a platform independant way? I mean using a batch file in Windows could do that. Is there any other way to have a platform independent code to open PDF files on the fly?

有没有办法让代码以独立于平台的方式在 Java 应用程序中打开 PDF 文件?我的意思是在 Windows 中使用批处理文件可以做到这一点。有没有其他方法可以让平台独立代码即时打开 PDF 文件?

采纳答案by Michael Myers

I'd try Desktop.open(File), which:

我会尝试Desktop.open(File),其中:

Launches the associated application to open the file.

启动关联的应用程序以打开文件。

So this code should do the trick:

所以这段代码应该可以解决问题:

if (Desktop.isDesktopSupported()) {
    try {
        File myFile = new File("/path/to/file.pdf");
        Desktop.getDesktop().open(myFile);
    } catch (IOException ex) {
        // no application registered for PDFs
    }
}

回答by mark stephens

You can use Runtime to execute and script and there are also several Java PDF viewers out there (ie Icepdf, JPedal, PDFRenderer).

您可以使用 Runtime 来执行和编写脚本,还有一些 Java PDF 查看器(即 Icepdf、JPedal、PDFRenderer)。

回答by amit

Use this to open pdf file using java

使用它来使用java打开pdf文件

File file = new File(filepath);
    if (file.toString().endsWith(".pdf")) 
        Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " + file);
    else {
        Desktop desktop = Desktop.getDesktop();
        desktop.open(file);
}

This code is used to open your pdf and other files.

此代码用于打开您的 pdf 和其他文件。

回答by Nimo ismail

Use this code to open a specific file:

使用此代码打开特定文件:

String cmds[] = new String[] {"cmd", "/c", "C:\Users\PC\Desktop\EA01.pdf"};
try {
    Runtime.getRuntime().exec(cmds);
}

回答by Ed S

Michael Meyer's solution didn't quite work for me. Specifically, a path with spaces fails with an IllegalArgumentException rather than an IOException.

Michael Meyer 的解决方案对我来说不太适用。具体来说,带有空格的路径会因 IllegalArgumentException 而不是 IOException 失败。

Here's what works for me:

以下是对我有用的内容:

    if (Desktop.isDesktopSupported()) {
try {
File theUMFile = new File(usersManualPath);
 Desktop.getDesktop().open(theUMFile);
}
catch (FileNotFoundException fnf){
okDialog(msg_fnf);
theConcours.GetLogger().log(Level.SEVERE, null, fnf);
theConcours.GetLogger().info(msg_fnf);
}
catch (IllegalArgumentException fnf) {
 okDialog(msg_fnf);
            theConcours.GetLogger().log(Level.SEVERE, null, fnf);
            theConcours.GetLogger().info(msg_fnf);
        }
        catch (IOException ex) {
            okDialog(msg_cno);
            theConcours.GetLogger().log(Level.SEVERE, null, ex);
            theConcours.GetLogger().info(msg_cno);
        }
    } 

回答by Liam Maddren

3-rd party applications can not access src dir in your application, in case, when your app assemble in jar archive. You should place your file separately from src.

3-rd 方应用程序无法访问您的应用程序中的 src 目录,以防您的应用程序在 jar 存档中组装。您应该将文件与 src 分开放置。

Of course, java find icons, because it's java API. You can access any resources in src folder through follow methods:

当然,java找到图标,因为它是java API。您可以通过以下方法访问 src 文件夹中的任何资源:

 URL url = getClass().getResource("/path/in/src");
 File file = new File(url.toURI());

I am currently using the below:

我目前正在使用以下内容:

        if (Desktop.isDesktopSupported()) {
             try {
                    URL url = getClass().getResource("/pdf/XXXX.pdf");
                    File myFile = new File(url.toURI());
                    Desktop.getDesktop().open(myFile);
            } catch (IOException | URISyntaxException ex) {
                        // no application registered for PDFs
                }
            }