7 Replies Latest reply on Oct 11, 2013 1:09 PM by gkaiseril

    Button to extract pages

    lithodora Level 1

      myexportFunc = app.trustedFunction(function(){

       

      ////Get Naming Convention

      var cRtn = app.response('Enter naming convention for section:');

       

       

      ////Save to current location. Get path of current doc

      var testing = this.path;

      var test = testing.split('/');

      var thepath = '';

          for (i = 0; test.length -2 >= i; i++)

          {

                thepath =  thepath + test[i] + '/';

          }

      ////Extract Pages

       

          for (p = 1; this.numPages >= p; p++)

                {

      var pgno = p;

      ///pgno = pgno + 1;

                          if (pgno < 10) {

                                    var pgno = '0' + pgno;

                          } else {

                                    var pgno = pgno;

                          }

                var newname = cRtn + pgno + '.pdf';

                  this.extractPages({

                               nStart: p,

                               cPath: thepath + newname

                  });

        }

       

      }

      )

       

       

       

      var myDoc = app.newDoc();

                // import icon (20x20 pixels) from the file specified

                myDoc.importIcon("myIcon", "/adsROP/1 COMMON/Application Settings/Scripts/assets/Untitled-1.pdf", 0);

                // convert the icon to a stream.

                oIcon = util.iconStreamFromIcon(myDoc.getIcon("myIcon"));

                // close the doc now that we have grabbed the icon stream

                myDoc.closeDoc(true);

                // add a toolbutton

                app.addToolButton({

                          cName: "myToolButton",

                          cLabel: "Extract Pages",

                       oIcon: oIcon,

                          cExec: "myexportFunc()",

                          cTooltext: 'Extract Pages',

                          cEnable: true,

                          nPos: 0

                });

      The button successfully is added. The function fires, etc....

       

       

      The extract pages function run alone works as is.

       

      I get the following error: 

      RangeError: Invalid argument value.

      Doc.extractPages:30:Doc 54d3a00myToolButton:Exec

        • 1. Re: Button to extract pages
          Test Screen Name Most Valuable Participant

          The first page is sometimes 0, sometimes 1. Be sure to read the documentation of each method with care to know which rule applies.

          • 2. Re: Button to extract pages
            lithodora Level 1

            The thing is it does not error on that line. It is the cPath: thepath + newname that is causing the error.

             

            The bit inside the function:

             

            ////Get Naming Convention

            var cRtn = app.response('Enter naming convention for section:');

             

             

            ////Save to current location. Get path of current doc

            var testing = this.path;

            var test = testing.split('/');

            var thepath = '';

                for (i = 0; test.length -2 >= i; i++)

                {

                      thepath =  thepath + test[i] + '/';

                }

            ////Extract Pages

             

                for (p = 1; this.numPages >= p; p++)

                      {

            var pgno = p;

            ///pgno = pgno + 1;

                                if (pgno < 10) {

                                          var pgno = '0' + pgno;

                                } else {

                                          var pgno = pgno;

                                }

                      var newname = cRtn + pgno + '.pdf';

                        this.extractPages({

                                     nStart: p,

                                     cPath: thepath + newname

                        });

              }

            Will perform exactly as expected when not run from the button. It only errors when I run it from the button.

             

            I am not sure what is missing.

             

             


            • 3. Re: Button to extract pages
              Test Screen Name Most Valuable Participant

              I think you are being too literal about line numbers. A command over multiple lines is executed when the END is reached, not as each line is seen.

              • 4. Re: Button to extract pages
                lithodora Level 1

                You are correct there and I knew that, but was being to literal.

                 

                 

                However the following works:

                 

                myexportFunc = app.trustedFunction(function(){

                 

                ////Get Naming Convention

                var cRtn = app.response('Enter naming convention for section:');

                 

                 

                ////Save to current location. Get path of current doc

                var testing = this.path;

                var test = testing.split('/');

                var thepath = '';

                    for (i = 0; test.length -2 >= i; i++)

                    {

                          thepath =  thepath + test[i] + '/';

                    }

                ////Extract Pages

                 

                    for (p = 1; this.numPages >= p; p++)

                          {

                var pgno = p;

                ///pgno = pgno + 1;

                                    if (pgno < 10) {

                                              var pgno = '0' + pgno;

                                    } else {

                                              var pgno = pgno;

                                    }

                          var newname = cRtn + pgno + '.pdf';

                newname = thepath + newname;

                app.alert(newname);

                 

                            this.extractPages({

                                         nStart: p,

                                         cPath: newname

                            });

                  }

                 

                }

                )

                 

                myexportFunc();

                 

                 

                It only fails to work when it is called from the button.

                • 5. Re: Button to extract pages
                  gkaiseril MVP & Adobe Community Professional

                  What is the exact error you are receiving?

                   

                  Have you checked the Acrobat JS API Reference for the extractPages method?

                   

                  Not only to you need to create a trusted function, but you need to use the privileged context for the extractPages. And the trusted function needs to be placed in a very specific application folder.

                  • 6. Re: Button to extract pages
                    lithodora Level 1

                    I get the error:

                     

                     

                    RangeError: Invalid argument value.

                    Doc.extractPages:30:Doc 54d3a00myToolButton:Exec

                     

                    I am running this from the console currently, but I did need to make the extract pages to be privileged.

                     

                     

                    myexportFunc = app.trustedFunction(function(){

                     

                    ////Get Naming Convention

                    var cRtn = app.response('Enter naming convention for section:');

                     

                     

                    ////Save to current location. Get path of current doc

                    var testing = this.path;

                    var test = testing.split('/');

                    var thepath = '';

                        for (i = 0; test.length -2 >= i; i++)

                        {

                              thepath =  thepath + test[i] + '/';

                        }

                    ////Extract Pages

                     

                        for (p = 0; this.numPages >= p; p++)

                              {

                    var pgno = p + 1;

                                        if (pgno < 10) {

                                                  var pgno = '0' + pgno;

                                        } else {

                                                  var pgno = pgno;

                                        }

                              var newname = cRtn + pgno + '.pdf';

                    app.beginPriv();

                                this.extractPages({

                                             nStart: p,

                                             cPath: thepath + newname

                                });

                    app.endPriv();

                      }

                     

                    }

                    )

                     

                     

                     

                    var myDoc = app.newDoc();

                              // import icon (20x20 pixels) from the file specified

                              myDoc.importIcon("myIcon", "/adsROP/1 COMMON/Application Settings/Scripts/assets/Untitled-1.pdf", 0);

                              // convert the icon to a stream.

                              oIcon = util.iconStreamFromIcon(myDoc.getIcon("myIcon"));

                              // close the doc now that we have grabbed the icon stream

                              myDoc.closeDoc(true);

                              // add a toolbutton

                              app.addToolButton({

                                        cName: "myToolButton",

                                        cLabel: "Extract Pages",

                                     oIcon: oIcon,

                                        cExec: "myexportFunc()",

                                        cTooltext: 'Extract Pages',

                                        cEnable: true,

                                        nPos: 0

                              });

                     

                     

                    This works. I just need to adjust the for loop to end at the last page rather than trying to go on to a page that does not exist.

                    1 person found this helpful
                    • 7. Re: Button to extract pages
                      gkaiseril MVP & Adobe Community Professional

                      Please look at your for statement and the parameters for the for statement.

                       

                      condition
                      An expression to be evaluated before each loop iteration. If this expression evaluates to true, statement is executed. This conditional test is optional. If omitted, the condition always evaluates to true. If the expression evaluates to false, execution skips to the first expression following the for construct.

                       

                      So " this.numPages >= p" might be clearer if written as "p < this.numPages".

                       

                      Also I would not increment or modify the control variable "p" within the loop.