简体   繁体   English

如何在Netbeans中打开PDF文件?

[英]How do i open a PDF file in Netbeans?

I want my application to open a pdf file when I click a dedicated button. 单击专用按钮时,我希望我的应用程序打开pdf文件。 How would i approach this? 我将如何处理? Also if I run the application from netbeans it shows the pdf but when compiled nothing comes up? 另外,如果我从netbeans运行该应用程序,它将显示pdf,但是编译时什么都没出现?

My code 我的密码

    private void showHelpMenuItemActionPerformed(java.awt.event.ActionEvent evt) {                                                 
        File f = new File("ana.pdf");
    try {
      Desktop.getDesktop().open(f);
  } catch (Exception ex) {
    System.out.println(ex);}

Your code gets the file from the current directory. 您的代码从当前目录获取文件。 The file is there when you run it from netbeans, but the file is not there when you run it. 从netbeans运行该文件时该文件在那里,但是在运行它时该文件不在那里。

Unfortunately, there's no easy way to do this. 不幸的是,没有简单的方法可以做到这一点。 I think the best idea would be write the documentation as HTML, put it on a server, and open the web browser (using Desktop.browse ). 我认为最好的办法是将文档编写为HTML,将其放在服务器上,然后打开Web浏览器(使用Desktop.browse )。 If someone else has a better idea, please comment. 如果其他人有更好的主意,请发表评论。

You can explicitly give the entire file path, which might solve your problem. 您可以显式给出整个文件路径,这可能会解决您的问题。 Also the OS you are using must support the operation. 另外,您使用的操作系统必须支持该操作。 This might help: 这可能会有所帮助:

if (Desktop.isDesktopSupported()) {
                    try {
                        File myFile = new File("C:\\Users\\klinks\\Documents\\pdf.pdf");
                        Desktop.getDesktop().open(myFile);
                    } catch (IOException e) {
                        // System probably doesn't have a default PDF program
                    }
                }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM