从 Java 应用程序即时打开 PDF 文件 [英] Open PDF file on the fly from a Java application
本文介绍了从 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(File)
,其中:
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屋!
查看全文