How to Open PDF file in Java Swings

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • gaya3
    New Member
    • Aug 2007
    • 184

    How to Open PDF file in Java Swings

    Hi All,
    I am very much new to java Swings I need to open the PDF file from Swings application..
    I tried as follows...

    Runtime r= Runtime.getRunt ime();
    Process p = r.exec("C:/Program Files/Adobe/Reader 8.0/Reader/AcroRd32.exe Test.Pdf");


    it got work.. But is ter any other solution.. because am hard coding the path of the exe file in the above code....
    pl help me out ASAP...
  • jkmyoung
    Recognized Expert Top Contributor
    • Mar 2006
    • 2057

    #2
    1. Use the registry class to determine what file is used to open a .pdf file.
    HKEY_CURRENT_US ER\Software\Mic rosoft\Windows\ CurrentVersion\ Explorer\FileEx ts\.pdf\OpenWit hList
    HKEY_CURRENT_US ER\Software\Mic rosoft\Windows\ CurrentVersion\ Explorer\FileEx ts\.pdf\OpenWit hProgids

    You could then handle the case where the user does not have .pdf reader installed, by directing them to FoxIt's website, or some other .pdf reader site.

    2.You could pass the name of the exe file in as a String variable. eg get it from your String[] args.

    Comment

    • gaya3
      New Member
      • Aug 2007
      • 184

      #3
      Thanks. Could you please brief your first solution.. And any other solutions.

      Comment

      • pbrockway2
        Recognized Expert New Member
        • Nov 2007
        • 151

        #4
        java.awt.Deskto p has an open() method.

        Comment

        • gaya3
          New Member
          • Aug 2007
          • 184

          #5
          Thanks.. but Desktop api are supported only fron java 1.6 rt?.. i need to implement the above functionality in java 1.5 version

          Comment

          • gaya3
            New Member
            • Aug 2007
            • 184

            #6
            How to open the existing PDF file using iText

            Comment

            • jkmyoung
              Recognized Expert Top Contributor
              • Mar 2006
              • 2057

              #7
              Open up regedit and go to those registry entries. They should show what program you use to open .pdf files by default.

              Once you get the program command line, you can simply send that to the Process to exec.

              Comment

              • gaya3
                New Member
                • Aug 2007
                • 184

                #8
                Hi ,
                I tried as follows

                Runtime.getRunt ime().exec("run dll32 url.dll,FilePro tocolHandler http://www.google.com" );

                am able to open the google page.. where as when i tried to open a pdf file .. it is not able to do so..

                Runtime.getRunt ime().exec("run dll32 url.dll,FilePro tocolHandler C:/pdf/test.pdf");


                Do any one faced this issue? Please help me out.

                Comment

                • jkmyoung
                  Recognized Expert Top Contributor
                  • Mar 2006
                  • 2057

                  #9
                  You're using url.dll to open a non-url.

                  Comment

                  • gaya3
                    New Member
                    • Aug 2007
                    • 184

                    #10
                    can you please suggest some solution for opening non-url?

                    Comment

                    • jkmyoung
                      Recognized Expert Top Contributor
                      • Mar 2006
                      • 2057

                      #11
                      Registry method: There won't be a standard answer for all types, unless you use the registry method. Even then you still have the problem of 'What if there is no designated program to open files of this type?'

                      If you know for sure what type of files you will be dealing with, you may be able to find a particular dll that works with it.

                      You could also probably go
                      r.exec("C:/pdf/test.pdf");
                      and let your computer deal with it.

                      Comment

                      • dvswapnil
                        New Member
                        • Sep 2013
                        • 1

                        #12
                        Code:
                        try {
                        
                        						if ((new File("c:\\OFFER_LETTER_1.pdf")).exists()) {
                        
                        							Process p = Runtime
                        									.getRuntime()
                        									.exec("rundll32 url.dll,FileProtocolHandler c:\\OFFER_LETTER_1.pdf");
                        							p.waitFor();
                        
                        						} else {
                        
                        							System.out.println("File is not exists");
                        
                        						}
                        
                        						System.out.println("Done");
                        
                        					} catch (Exception ex) {
                        						ex.printStackTrace();
                        					}
                        Last edited by Rabbit; Sep 17 '13, 04:42 PM. Reason: Please use code tags when posting code or formatted error.

                        Comment

                        Working...