5 Replies Latest reply on May 2, 2012 1:52 AM by try67

    Adding Scripts to Specific Pages


      I've been struggling with this one for a little while, so thought I'd see if any one else can come up with a solution.


      I've been trying to add scripts to specific pages in PDFs.  I usually add one script at the document level that applies actions to all buttons within the document, but I then have to manually add scripts to pages.


      Is there a way around this that anyone can think of?  Can I simply write an event listener for the Page/Open and Page/Close events?


      In a nutshell, I'm trying to achieve something like this (using a modified version of the getField function):


      this.getPage("1").setAction("Open", "Show('layer1');");

      this.getPage("1").setAction("Close", "Hide('layer1');");

      this.getPage("3").setAction("Open", "Show('layer1');");

      this.getPage("3").setAction("Close", "Hide('layer1');");


      If anyone can assist, I'd greatly appreciate it!  I don't mind adding these scripts manually to a few pages, but it gets a little tedious when everything else is scripted.

        • 1. Re: Adding Scripts to Specific Pages
          try67 MVP & Adobe Community Professional

          The Document object has no such method as getPage() in Acrobat, so this

          code is invalid.

          There are no event listeners to the Page Open/Page Close actions.


          However, it looks like something that can easily done with a loop that

          applies the same scripts to all the relevant pages.

          • 2. Re: Adding Scripts to Specific Pages
            dtm-1979 Level 1

            I know that function doesn't work . . . I just wish there was that was as easy for pages as it is for fields!


            I thought an custom event listener would have been the way around this, as the Api Reference states:



            All JavaScript scripts are executed as the result of a particular event. For each of these events, JavaScript

            creates an event object. During the occurrence of each event, you can access this object to get and

            possibly manipulate information about the current state of the event.


            Or does the Page/Open event apply to any page in the document, and therefore can't be modified for specific pages?


            I've trawled over the API docs countless times looking for a way to do this, then I usually leave it a few weeks until it comes up again, and I still can't figure out a way to apply a script to specific pages.  Gilad - if you can help point me in the right direction to start the loop, that would be fantastic!



            • 3. Re: Adding Scripts to Specific Pages
              try67 MVP & Adobe Community Professional

              If you read the documentation of setPageAction(), you've seen that the first parameter is the page number.

              So if you defined which pages you want to apply this script to, you can change that number in a loop.

              Something like this can be executed from the console:


              var pages = [ 0,1,2,10]; // These are the pages we want to add the script to

              for (var i in pages) {

                    this.setPageAction(pages[i], "Open", "Show('layer1');");

                    this.setPageAction(pages[i], "Close", "Hide('layer1');");


              • 4. Re: Adding Scripts to Specific Pages
                dtm-1979 Level 1

                aha!  Gilad, you're a lifesaver!  I don't know how on earth i missed that!


                Thank you so much for pointing that out!  (and supplying a script that I'm sure is a lot cleaner than mine would have been!)

                • 5. Re: Adding Scripts to Specific Pages
                  try67 MVP & Adobe Community Professional

                  Just remember those page numbers are zero-based, so the first page of the file is 0, not 1.