打开 PDF 只能在 netbeans 中使用
我编写了一个创建 PDF 文档的程序。创建后,必须打开 PDF 才能打印。
我有以下代码,但只有从 netbeans 启动时它才有效。有人能给我一些指点吗?
public void openPDF()
{
try {
System.out.println("Opening PDF");
File file = new File(pdfPath+pdfName);
String absolutePDFpath = file.getAbsolutePath().replace(""+(char)92,""+(char)92+(char)9);
System.out.println("Path = "+absolutePDFpath);
Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " + absolutePDFpath);
}
catch (IOException ex)
{
JOptionPane.showMessageDialog(null, "Er is een fout opgetreden tijdens het openen van het PDF"
+ " document\nFoutcode: 0xFF05");
Logger.getLogger(PrintJob.class.getName()).log(Level.SEVERE, null, ex);
}
I have written a program that creates PDF documents. After creating, the PDF's must be opened so it can be printen.
i have the following code, but it only works if i launch from netbeans. Could anyone give me some pointers?
public void openPDF()
{
try {
System.out.println("Opening PDF");
File file = new File(pdfPath+pdfName);
String absolutePDFpath = file.getAbsolutePath().replace(""+(char)92,""+(char)92+(char)9);
System.out.println("Path = "+absolutePDFpath);
Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " + absolutePDFpath);
}
catch (IOException ex)
{
JOptionPane.showMessageDialog(null, "Er is een fout opgetreden tijdens het openen van het PDF"
+ " document\nFoutcode: 0xFF05");
Logger.getLogger(PrintJob.class.getName()).log(Level.SEVERE, null, ex);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
请参阅
Desktop.open(File)
了解跨不同平台打开 PDF 的方法。像这样的东西..如果应用程序。需要支持 Java 1.5 或更早版本(在
Desktop
可用之前),坚持使用exec()
,但实施 当 Runtime.exec() 不会执行时。为了可靠地运行,使用
Process
需要程序员做很多事情。该代码没有执行其中任何一个操作。See
Desktop.open(File)
for a way to open a PDF across different platforms. Something like this..If the app. needs to support Java 1.5 or earlier (before
Desktop
was available), stick withexec()
, but implement all the recommendations of When Runtime.exec() won't.There are a number of things that using a
Process
requires the programmer to do, for reliable running. That code does none of them.您的代码可能无法运行,因为您没有指定环境变量。它应该是这样的:
Probably your code doesn't work because you don't specify environment variables. It should be something like this: