10 Replies Latest reply on Mar 8, 2012 8:35 AM by Kristofer04569

    How to set which page to open on

    Kristofer04569 Level 1

      Hi,

       

      Can someone tell me how to set the property (which one?) so the pdf document will open on a certain page?

       

      I just cannot figure out how to use the reference. There is a GoTo, which maybe is the one i'm looking for(?), but how do i call/set it?

       

      (no, it is not pageNum in the doc object)

       

      Thanks!

        • 1. Re: How to set which page to open on
          try67 MVP & Adobe Community Professional

          File - Properties - Initial View

          • 2. Re: How to set which page to open on
            Kristofer04569 Level 1

            Hi,

             

            I posted this question in the JavaScript forum since i wanted a solution using JavaScript, not how to do it in the GUI. I need to do this on a lot of files so i want to run it as a batch job.

            • 3. Re: How to set which page to open on
              try67 MVP & Adobe Community Professional

              This can't be set using JavaScript. What you can do, though, is embed a doc-level script in the file that will cause it to jump to the page you wish when the file is opened. The script would simply be:

              this.pageNum = 0;

              (or whatever number you wish...)

               

              If you want to add this code with a script, use the addScript method, like so:

              var goToPageScript = "this.pageNum = 0;";

              this.addScript("GoToPage", goToPageScript);

              • 4. Re: How to set which page to open on
                Kristofer04569 Level 1

                That is a great way to do it, thanks!

                 

                But, when I run this in a batch, i get this error:

                 

                GeneralError: Åtgärden misslyckades.

                Doc.addScript:12:Batch undefined:Exec

                 

                The first one is in Swedish and says something like "action failed". But even though this error message, it changes the pdf-file and adds the script (so the outcome seems correct). Everything also works good outside the batch (manual execution).

                 

                This is the batch code:

                /* Edit PDF */
                var re = /.*\/|\.pdf$/ig;
                // filename is the base name of the file Acrobat is working on
                var filename = this.path.replace(re,"");
                console.println(filename);
                //var mypage = filename.substring(filename.length)
                //console.println(mypage);

                 

                var goToPageScript = "this.pageNum = 3;";
                this.addScript("GoToPage", goToPageScript);

                 

                *************************************

                I execute it as normal in the Batch Sequences dialog box, and no pdf-file is opened. When i am asked to select a file, i do so. Then the error comes up in the debugger.

                 

                Am i doing anything wrongly?

                 

                (yes, it is the first time i write a batch script)

                • 5. Re: How to set which page to open on
                  try67 MVP & Adobe Community Professional

                  I'm not sure why you're getting this error, but did you know that with a

                  batch sequence you can change the Initial Page View setting directly,

                  without needing to use a script?

                  • 6. Re: How to set which page to open on
                    Kristofer04569 Level 1

                    If I understands that option, you cannot have a changing variable, or?

                     

                    This is what i am doing (and must do, otherwise it does not work in another system):

                     

                    For each page of a pdf-file, create one file.

                    For each new file, set the initial page view to the same as the pagenumber. So it is like:

                     

                    foo001.pdf, initial page view = 1

                    foo002.pdf, initial page view = 2

                    foo003.pdf, initial page view = 3

                    and so on...

                     

                    My first thought was to set the pageNum for each file. But, i do not find a way to do it. If the file is open, it goes to the correct page, but the pageNum property is never set.

                    Then you provided me with another great solution, which worked fine, but with that error.

                     

                    :-(

                    • 7. Re: How to set which page to open on
                      try67 MVP & Adobe Community Professional

                      No, if you use that option you can't make it variable.

                       

                      OK, so back to the script. Could you explain a bit better what you're

                      doing, exactly?

                      Are you running the batch on a folder that already contains those files, or

                      are you trying to generate them in the batch?

                      • 8. Re: How to set which page to open on
                        Kristofer04569 Level 1

                        Sure, i can explain.

                         

                        I have a separate batch script that copies the original document, and creates new for each page. I am using extractPages(...) for this, and it works fine, all files are created and no error is shown.

                         

                        The second batch script is the one that should set initial page view.

                         

                        - I create the batch script.

                        - In the Edit Sequence dialog box, i move "Execute JavaScript" to the right.

                        - I paste the above code into the editor, and click "Ok".

                        - The other settings i do not change, so it is:

                             Run Commands on = Ask When Sequence is Run

                             Store files = Same as original  (sorry, no english translation, but i hope you understand which one)

                        - I then make sure that no pdf-file is opened, Acrobat is just opened without any file.

                        -  In the "Batch Sequences" box, i select the sequence i just created, and then run it.

                        - Then in "Run Sequence Confirmation", i just click Ok, nothing else.

                        - I am now asked to select files. I select one of the files created in the previous batch script. (For this moment I select only one, in the future it will all of them).

                        - The Debugger is opened and the above error messages are shown.

                         

                        Do you see any step that i have missed or should not do? This is so strange.

                        • 9. Re: How to set which page to open on
                          Bernd Alheit Adobe Community Professional & MVP

                          Does all PDF documents contain only one page?

                          • 10. Re: How to set which page to open on
                            Kristofer04569 Level 1

                            Each document contains all pages. So essentially, there are a lot of copies of the same file and has the same content (yes, it is stupid).

                             

                            Anyway, i have found a way to do this. I open each document (which i thought would already be open so i could use "this"), set the property, save and lastly close it. It works, but it aint fast. So if someone ever finds a way to do this faster, that would be really nice.  :-)

                             

                            Here is the code:

                             

                            /* Set Initial Page View */

                            function trimNumber(s) {   while (s.substr(0,1) == '0' && s.length>1) { s = s.substr(1,9999); }   return s; }

                            var re = /.*\/|\.pdf$/ig;
                            var filename = this.path.replace(re,"");
                            console.println(filename);
                            var mypage = filename.substring(15-3);

                            var mDoc = app.openDoc(this.path);
                            var goToPageScript = "this.pageNum = \"" + (trimNumber(mypage)-1) + "\";";
                            mDoc.addScript("GoToPage", goToPageScript);
                            mDoc.saveAs(this.path);
                            mDoc.closeDoc();

                            /* **************** */

                             

                            Example file name: tqr02_10a02_003.pdf

                             

                            I really appreciate the help you gave me! I don't think i would ever get this to work otherwise. And now it saves me time opening 300+ document manually to set the initial page view, and do this every month!

                             

                            Thanks!