1 2 Previous Next 17 Replies Latest reply on Nov 1, 2006 2:34 AM by 807596

    opening PDF file in a standalone Java Swing application

    807596
      Hi
      I need a functionality by which a user can click on an icon in a JPanel and it would open a pdf document using Adobe Acrobat,
      I am not sure how to goabout this.
      There is NO additional requirement such as writing / creating / manipulating PDFs.

      Just a 'click and read' functionality is sufficient.
      I am a novice at programming using Swing and Java in general. Hence I would appreciate some assistance in this regard
        • 1. Re: opening PDF file in a standalone Java Swing application
          800387
          You can use Runtime.exec() to call the Adobe executable and pass the PDF document as the first parameter. However, your challenge will be to ensure that you can locate the Adobe executable reliably (e.g., what happens if I installed it on my E: drive instead of C:?)

          - Saish
          • 2. Re: opening PDF file in a standalone Java Swing application
            800387
            Also, this project on sourceforge advertises that part of its components can be used to display PDF natively in Java.

            http://sourceforge.net/projects/xom/

            - Saish
            • 3. Re: opening PDF file in a standalone Java Swing application
              807596
              Hi
              I created a mouselistener on the JLabel , clicking on which the pdf file should open.
              The code for the same is pasted below,
              public void mouseClicked(MouseEvent me)
              {

              Runtime r= Runtime.getRuntime();
                   try{
              System.out.println("testing : Inside mouseclicked");
                   Process p = r.exec("AcroRd32.exe paper1.pdf");
                   }
                   catch(Exception ex)
                   {
                        ex.printStackTrace();
                   }
              }
              However on executing it , I am getting the following error
              java.io.IOException: CreateProcess: paper1.pdf error=2
              at java.lang.ProcessImpl.create(Native Method)
              at java.lang.ProcessImpl.<init>(ProcessImpl.java:81)
              at java.lang.ProcessImpl.start(ProcessImpl.java:30)
              at java.lang.ProcessBuilder.start(ProcessBuilder.java:451)
              at java.lang.Runtime.exec(Runtime.java:591)

              I am not sure about the best course of action for this problem
              • 4. Re: opening PDF file in a standalone Java Swing application
                800387
                Try:

                Runtime.exec("adobe.exe", new String[] { "paper1.pdf" });

                instead.

                - Saish
                • 5. Re: opening PDF file in a standalone Java Swing application
                  807596
                  I am continuing to get the following error messages :(
                  java.io.IOException: CreateProcess: ArcoRd32.exe error=2
                  at java.lang.ProcessImpl.create(Native Method)
                  at java.lang.ProcessImpl.<init>(ProcessImpl.java:81)
                  at java.lang.ProcessImpl.start(ProcessImpl.java:30)
                  at java.lang.ProcessBuilder.start(ProcessBuilder.java:451)
                  at java.lang.Runtime.exec(Runtime.java:591)
                  at java.lang.Runtime.exec(Runtime.java:429)
                  at java.lang.Runtime.exec(Runtime.java:367)
                  at UI$2.mouseClicked(UI.java:49)
                  at java.awt.AWTEventMulticaster.mouseClicked(AWTEventMulticaster.java:212)
                  at java.awt.Component.processMouseEvent(Component.java:5491)
                  at javax.swing.JComponent.processMouseEvent(JComponent.java:3093)
                  at java.awt.Component.processEvent(Component.java:5253)
                  at java.awt.Container.processEvent(Container.java:1966)
                  at java.awt.Component.dispatchEventImpl(Component.java:3955)
                  at java.awt.Container.dispatchEventImpl(Container.java:2024)
                  at java.awt.Component.dispatchEvent(Component.java:3803)
                  at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4212)
                  at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3901)
                  at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3822)
                  at java.awt.Container.dispatchEventImpl(Container.java:2010)
                  at java.awt.Window.dispatchEventImpl(Window.java:1766)
                  at java.awt.Component.dispatchEvent(Component.java:3803)
                  at java.awt.EventQueue.dispatchEvent(EventQueue.java:463)
                  at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:234)
                  at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:163)
                  at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:157)
                  at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:149)
                  at java.awt.EventDispatchThread.run(EventDispatchThread.java:110)
                  • 6. Re: opening PDF file in a standalone Java Swing application
                    800387
                    It works fine on my machine, so I am not sure what the difficulty is. Runtime.exec() returns a Process object, see if you can inspect the object for a more detailed error message. Also, have you perchance tried simply double-clicking the file and verifying it is a valid (e.g., non-corrupted) PDF?

                    - Saish
                    • 7. Re: opening PDF file in a standalone Java Swing application
                      807596
                      Is the Acrobat executable included in your operating system's path?
                      • 8. Re: opening PDF file in a standalone Java Swing application
                        807596
                        Try this

                        Process theProcess =
                        Runtime.getRuntime().exec("/Program Files/Adobe/Acrobat7.0/Reader/AcroRd32.exe C:\\filename.pdf");

                        You need to pass the absolute pathway for the executable and the file being read.

                        if you are reading in a file from a file chooser you can also do the following

                        Process theProcess =                                    Runtime.getRuntime().exec("/Program Files/Adobe/Acrobat7.0/Reader/AcroRd32.exe "+chooser.getSelectedFile().getPath());

                        hope this helps

                        nelly
                        • 9. Re: opening PDF file in a standalone Java Swing application
                          807596
                          Hi.

                          Process theProcess =
                          Runtime.getRuntime().exec("/Program Files/Adobe/Acrobat7.0/Reader/AcroRd32.exe C:\\filename.pdf");

                          Nellywatters : This works quite fine on my system as I know Acrobate Reader path but what happen's if the program is going to run on other systems .
                          • 10. Re: opening PDF file in a standalone Java Swing application
                            807596
                            Hi @ all,

                            what you are looking for is something like

                            Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " + file);

                            So, by specifying a *.pdf file, Windows will try to start the appropriate program (Adobe Reader or whatever application is registered for PDF files). This works for all types of files, of course: MSWord, Excel etc ...
                            And: you do not have to specify application paths in your java code (which is a bad thing IMHO anyway)

                            Cheers :-)
                            • 11. Re: opening PDF file in a standalone Java Swing application
                              807596
                              That's a great solution for Windows, but is there a way to do something similar that is platform independent?
                              • 12. Re: opening PDF file in a standalone Java Swing application
                                807596
                                THere is a library from pdfgo.com it's commercial but we used it in a project. It works and you do not have any problem related to finding the acrobat viewer and starting it.

                                http://www.icesoft.com/products/icepdf.html

                                Marteijn
                                • 13. Re: opening PDF file in a standalone Java Swing application
                                  807596
                                  Thank you miraco, For the Windows Good Experience.

                                  Short java Flames
                                  • 14. Re: opening PDF file in a standalone Java Swing application
                                    807596
                                    can anybody give swing programs on different outputs
                                    1 2 Previous Next