Opening a PDF with Javascript

java , javascript New Zealand
  • 12 years ago

    Hi, I've been trying to get the following code to work, it accesses what kind of PDF Reader the computer has and then should load it in the appropriate reader. The start load word and excel documents just fine in the applications I would like PDFs to do the same. This is in IE only.
    If someone can give me some help I would really appreciate it!


       function open_document()
       {
          var document_object = "c:\documents\userguide.pdf";
          var extension = "pdf";
        
        if (extension == "csv" || extension == "xls")
        {
         var xlApp = new ActiveXObject("Excel.Application");
         xlApp.Visible = 'True';
         var wb = xlApp.Workbooks;
         wb.Open(document_object.path);
        }

        if (extension == "txt" || extension == "doc")
        {
         var wdApp = new ActiveXObject("Word.Application");
         wdApp.Visible = 'True';
         var wdDoc = wdApp.Documents;
         wdDoc.Open(document_object.path);
        }

        if (extension == "pdf")
        {
         var isInstalled = false;
         var version = null;
         if (window.ActiveXObject)
         {
          var control = null;
          try
          {
           // AcroPDF.PDF is used by version 7 and later
           control_text = "AcroPDF.PDF";
           control = new ActiveXObject(control_text);
          }
          catch (e)
          {
           // Do nothing
          }
          if (!control)
          {
           try
           {
            // PDF.PdfCtrl is used by version 6 and earlier
            control_text = "PDF.PdfCtrl";
            control = new ActiveXObject(control_text);
           }
           catch (e)
           {
            return;
           }
          }
          if (control)
          {
           isInstalled = true;
           version = control.GetVersions().split(",");
           version = version[0].split("=");
           version = parseFloat(version[1]);
           
           /*************************************************************************************
           ********    SHOULD LOAD THE READER AND PDF HERE            *********
           *************************************************************************************/
          }
          else
          {
           alert("Could not find recent version of Adobe PDF Reader on this machine. Please install and try again");
          }
         }
        }
       }

     

Post a reply

No one has replied yet! Why not be the first?

Sign in or Join us (it's free).

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Perl - The only language that looks the same before and after RSA encryption.” - Keith Bostic