5 Replies Latest reply on May 19, 2011 7:25 AM by ringerc_auspost

    Possible bug in script api: doc.addThumbnails aborted by subsequent actions

    ringerc_auspost

      Hi all

       

      I'm struggling with another issue while scripting a task in Acrobat Pro 9 (Win). It's beginning to look like a bug in Acrobat - or an extreme case of stupidity on my part.

       

      I have a folder script that creates a couple of menu items to invoke priveleged functions. One of these functions scans the document to add web links, adds thumbnails, crops the document to add a thin white border around all edges for all pages, then invokes the PDF optimizer dialog. Most of this works just fine, but for some reason the doc.setPageBoxes command used to crop the pages only processes the first couple of pages.

       

      If invoked stand-alone in the script console, the same doc.setPageBoxes command will reliably process all pages. It almost looks like the command runs asynchronously, with any subsequent action aborting it... but it's not documented as such and no other Acrobat commands work that way.

       

      Here's the function of interest:

       

      postProcessDoc = app.trustedFunction( function(doc) {
        var lastPage = this.numPages - 1;
        var crop = doc.getPageBox("Crop",1);
        var x = 20;
        doc.setPageBoxes({cBox: "Crop", rBox: [crop[0]-x,crop[1]+x,crop[2]+x,crop[3]-x], nStart:0, nEnd: lastPage});
        /* Add web links and thumbnails */
        var nLinks = doc.addWeblinks({nStart:0, nEnd: lastPage});
        var ignored = doc.addThumbnails({nStart:0, nEnd: lastPage});  
        /* then save */
        app.beginPriv();
        doc.saveAs("/Users/production/Desktop/fullpaper.pdf");
        /* Open the optimize dialog. We have to trust the user to do the right thing after this. */
        app.execMenuItem("PDFOptimizer");
        app.endPriv();
      });
      
       
      

        • 1. Re: Possible bug in script api: doc.addThumbnails aborted by subsequent actions
          try67 MVP & Adobe Community Professional

          You're still using "this" instead of the doc object (in the line that

          defines lastPage).

          1 person found this helpful
          • 2. Re: Possible bug in script api: doc.addThumbnails aborted by subsequent actions
            ringerc_auspost Level 1

            Just for further confirmation, the documentation for Doc.setPageBoxes reads:

             

            nStart

            (optional) A 0-based index that defines the start of an inclusive range of pages in the document to be operated on. If nStart and nEnd are not specified, operates on all pages in the document. If only nStart is specified, the range of pages is the single page specified by nStart.

             

            nEnd

            (optional) A 0-based index that defines the end of an inclusive range of pages in the document to be operated on. If nStart and nEnd are not specified, operates on all pages in the document.

             

            ... but these arguments seem to be ignored. In Acrobat 9 (Win) the behavior turns out not to match the above I reported, which I encountered on Acrobat 8 (Mac) during intial testing. On Acrobat 9 (Win) only the page currently being viewed in the UI is cropped, irrespective of the values of and presence or absence of the nStart and nEnd arguments. I've tried looping to set the current page and then calling Doc.setPageBoxes, but that lands up cropping whichever the last page Doc.pageNum was set to. Eg:

             

            for (var i = 0; i <= lastPage; i++) {
              this.pageNum = i;
              this.setPageBoxes({cBox: "Crop", rBox: [crop [0]-x,crop[1]+x,crop[2]+x,crop[3]-x], nStart:i, nEnd: i});
            }
            

             

            will crop page 91 of a 91 page document, and no others.

             

            Ideas? What stupid thing am I doing wrong?

            • 3. Re: Possible bug in script api: doc.addThumbnails aborted by subsequent actions
              ringerc_auspost Level 1

              Thanks for the catch re incorrect use of `this'. You're quite right, and it's kind of strange that it worked at all in that case*. However, the problem persists even with that fix.

               

              It turns out that unlike Acrobat 8 (Mac) which I did my initial testing on, I can reproduce this in the console in Acrobat 9 (Win) as well as in a script. The following script, when run in the console, should add a white border around all pages in a document, but instead adds that border only to the page being viewed when the script is run.

               

              var lastPage = this.numPages - 1;
              var crop = this.getPageBox("Crop",1);
              var x = 20;
              this.setPageBoxes({cBox: "Crop", rBox: [crop [0]-x,crop[1]+x,crop[2]+x,crop[3]-x], nStart:0, nEnd: lastPage });
              

               

              * Acrobat's JavaScript environment uses `this' in a different way to every other JS environment I've ever used, though, often pointing it to the current Doc object irrespective of execution context, so it probably actually pointed to the Doc anyway and that'll be why it worked.

              • 4. Re: Possible bug in script api: doc.addThumbnails aborted by subsequent actions
                try67 MVP & Adobe Community Professional

                Why use nStart and nEnd at all if you want to apply this to all the pages?

                • 5. Re: Possible bug in script api: doc.addThumbnails aborted by subsequent actions
                  ringerc_auspost Level 1

                  As noted above, the problem persists if they are omitted entirely.