9 Replies Latest reply on May 29, 2009 1:43 PM by kentgbaker

    Odd or Even Page selection

    kentgbaker

      Is there a typical way to define all odd pages in a document, as well as all even.

       

      I have scripts (with help from these forums) that do page rotations on the current page

       

      example:

       

      var crtpage = this.pageNum
      var crtpagerotate = this.getPageRotation({nPage:crtpage});
      try {
      this.setPageRotations({nStart:crtpage, nEnd:crtpage, nRotate:(90+crtpagerotate)%360});
      }
      catch(e)
      {
      app.alert("Processing error: "+e)
      }

       

       

      But I would like to be able to rotate all odd pages 90, and all even pages 270

       

      Any help is appreciated

        • 1. Re: Odd or Even Page selection
          MarkWalsh Level 4

          Here's one way you can determine if the page number is odd or even:

           

          ((myPageNumber/2) == parseInt(myPageNumber/2))

           

          This will return true for even pages and false for odd pages. Rotate 270 if true, or 90 if false.

          • 2. Re: Odd or Even Page selection
            gkaiseril MVP & Adobe Community Professional

            You could also use the "%" modulos operator to obtain the remainder of the division by 2  (modulos) of 1 (odd page) or zero (even page).

             

            if(this.pageNum % 2) {
            // result true  not 1 so even page

            app.alert("even - this.pageNum % 2: " + (this.pageNum % 2) );
            } else {

            // result false  1 so odd page
            app.alert("odd - this.pageNum % 2: " + (this.pageNum % 2) );
            }

             

            So one could even use this result to either add 180 or 0 degree rotation adjusment.

             

             

            if(this.pageNum % 2) {
            // result true  not 1 so even page
            app.alert("even - this.pageNum % 2: " + (this.pageNum % 2) );
            } else {
            // result false  1 so odd page
            app.alert("odd - this.pageNum % 2: " + (this.pageNum % 2) );
            }

            var pRotate = 90;
            pRotate += ((this.pageNum % 2) % 2) * 180;
            app.alert("rotation: " + pRotate);

            1 person found this helpful
            • 3. Re: Odd or Even Page selection
              kentgbaker Level 1

              great,

               

              but I need to have the rotations happen on all pages of the document. I can use setPageRotations and define only the nEnd to get to all pages.......but how to get one rotation if odd, and a different rotation if even.

               

              Is there someway to loop through pages? then do the check for odd or even, then move on to the next page

              • 4. Re: Odd or Even Page selection
                gkaiseril MVP & Adobe Community Professional

                Yes, you can use the JavaScript "for() {" loop control and use the "numPages" of the doucment object for the control value for the number of pages in the PDF.

                 

                var pRotate; // degrees for page rotation

                for(i = 0; i < this.numPages, i++) {

                // compute rotation for this page

                pRotate =  90 + ((this.pageNum % 2) * 180);
                // rotate this page

                this.setPageRotations({nStart: i, nEnd: i, nRotate: pRotate });

                } // end page loop

                1 person found this helpful
                • 5. Re: Odd or Even Page selection
                  kentgbaker Level 1

                  Sorry,

                   

                  console reports missing ; after for-loop condition, and I can't figure out what it needs.

                  • 6. Re: Odd or Even Page selection
                    kentgbaker Level 1

                    sorry, it was only the this.numPages;

                    • 7. Re: Odd or Even Page selection
                      kentgbaker Level 1

                      Thanks in advance,

                       

                      here is where I am, stop me if this is the long way:

                       

                      for(i = 0; i < this.numPages; i++)
                      {
                      if(this.pageNum % 2) {
                      var crtpagerotate = this.getPageRotation({nPage:?});
                      this.setPageRotations({nStart: ?, nEnd: ?, nRotate:(270+crtpagerotate)%360}); 
                      }
                      else {
                      var crtpagerotate = this.getPageRotation({nPage:?});
                      this.setPageRotations({nStart: ?, nEnd: ?, nRotate:(90+crtpagerotate)%360}); 
                      }
                      }

                       

                      notice the nStart: ?, nEnd: ?,  nPage:? this needs to be the page the loop is currently executing. Is this possible?

                      • 8. Re: Odd or Even Page selection
                        MarkWalsh Level 4

                        the variable 'i' will be your page number, so this should be what you want (untested)

                         

                        for(i = 0; i < this.numPages; i++)
                        {

                        if(i % 2) {

                        var crtpagerotate = this.getPageRotation({nPage:i});
                        this.setPageRotations({nStart: i, nEnd: i, nRotate:(270+crtpagerotate)%360}); 
                        }
                        else {
                        var crtpagerotate = this.getPageRotation({nPage:i});
                        this.setPageRotations({nStart: i, nEnd: i, nRotate:(90+crtpagerotate)%360}); 
                        }
                        }

                        • 9. Re: Odd or Even Page selection
                          kentgbaker Level 1

                          Thanks everybody. I had used i as the variable at one point and could not get it to work. I will blame it on Friday. Thanks very much Mark.