从Java应用程序中即时打开PDF文件 [英] Open PDF file on the fly from a Java application

查看:127
本文介绍了从Java应用程序中即时打开PDF文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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?

推荐答案

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

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


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

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
    }
}

这篇关于从Java应用程序中即时打开PDF文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆