打开 PDF 只能在 netbeans 中使用

发布于 2024-12-21 02:34:41 字数 827 浏览 3 评论 0原文

我编写了一个创建 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

安静 2024-12-28 02:34:41

请参阅 Desktop.open(File) 了解跨不同平台打开 PDF 的方法。像这样的东西..

File file = new File(pdfPath+pdfName);
Desktop.getDesktop().open(file);

如果应用程序。需要支持 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..

File file = new File(pdfPath+pdfName);
Desktop.getDesktop().open(file);

If the app. needs to support Java 1.5 or earlier (before Desktop was available), stick with exec(), 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.

最单纯的乌龟 2024-12-28 02:34:41

您的代码可能无法运行,因为您没有指定环境变量。它应该是这样的:

Runtime.getRuntime().exec("command to execute", env[]);

Probably your code doesn't work because you don't specify environment variables. It should be something like this:

Runtime.getRuntime().exec("command to execute", env[]);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文