4 Replies Latest reply on Nov 3, 2008 8:43 AM by gkaiseril

    Execute JavaScript automatically when document opened. How to do?

      Execute JavaScript automatically when document opened. How to do?
        • 1. Re: Execute JavaScript automatically when document opened. How to do?
          gkaiseril MVP & Adobe Community Professional
          One can use either a document level JavaScript or a page action JavaScript like:

          // define function to initialize the PDF
          function Initialize()
          {
          // if document variable bInitialized is not true (ie undefined or false)
          if(!bInitialized){
          // place code to be run once here
          app.alert("Hello!", 3, 0);
          bInitialized = true;
          } // end if not initialized
          // add code to always run here
          return true; // return success
          } // end of function Initialize

          // code to be run
          // If form has not been initialized
          if (!bInitialized) {
          // define the bInitialized variable and set its value
          var bInitialized = Initialize()
          }

          Note the code allows for the running of certain code only once so if one leaves page one and comes back the code will not be executed a second time.
          • 2. Re: Execute JavaScript automatically when document opened. How to do?
            try67 MVP & Adobe Community Professional
            How about the addScript() method? It adds a script the is being run when the document is opened.
            • 3. Re: Execute JavaScript automatically when document opened. How to do?
              Patrick Leckey Level 3
              > How about the addScript() method?

              What would run the addScript method? You'd also have to be careful using addScript, especially for users who only have Reader. The script will not be saved permanently into the form automatically - the user has to re-save the form after addScript runs, so unless the form is Reader enabled, users with Reader will not be able to save the script added by addScript into the form. Easier to just embed it at the document level as Geo suggested, unless you need to add an identical script to a large number of documents via batch process.
              • 4. Re: Execute JavaScript automatically when document opened. How to do?
                gkaiseril MVP & Adobe Community Professional
                I should note that the page open action can be done with standard via the Acrobat GUI and document level JavaScripts can be added by the Acrobat GUI in Professional. If one wants to use the "addScript()" or "addPageAction()" methods, one needs to be careful not to over write existing code which would be available in Acrobat's GUI but not to the Scripting methods.