3 Replies Latest reply on Nov 10, 2014 9:21 AM by syswizardx

    Document-level Javascript as Object

    syswizardx Level 1

      I created a document level script called Utility.

      I want to create methods within this script rather than coding each one separately.

      function Utility()


      this.getFormattedDateOnly=function(pDate) {


        if (typeof pDate == "undefined") {

          pDate = new Date();


        var retval = util.printd("m/dd/yyyy",pDate);

        return retval;



      this.getFldsOnPagenum = function(pPageNum, pDoc) {

        /* returns an array of the form fields who are on or start on a page */

        /* zero based return ; however pPageNum is 1 thru numPages, not 0 -> numPages-1 */

        var currPage;

        var currFld;

        var cnt = -1;

        var ARflds = [];

      CPR("# flds=" + pDoc.numFields+" Type of pDoc:"+ (typeof pDoc));


        for (var i = 0; i < pDoc.numFields; i++) {

          currFld = pDoc.getNthFieldName(i);

          if (typeof currFld.page == "number") {


            if ((1*currFld.page+1) == pPageNum) {


              ARflds.length = cnt + 1;

              ARflds[cnt] = currFld;

      CPR("\n cnt="+cnt);



          } else if ((1*currFld.page[0]+1) == pPageNum) {


            ARflds.length = cnt + 1;

            ARflds[cnt] = currFld;



        return ARflds;




      var MyUtil = new Utility();

      ------------------------------------------------------------------------------------------ ---------


      This is all working fine with the following exceptions:

      1) the methods are not being registered in the Javascript hierarchy listing.

      That's annoying.


      2) "This" is not recognized at the document level....I must pass it as a parameter (pDoc).

      I thought that was strange...I could see it if were at the folder level.

      It appears the document object is out-of-scope !!


      3) currFld = pDoc.getNthFieldName(i);

      The above is failing....sending back a "currFld is undefined" error.

      I don't see the problem....very strange IMHO.
      When I call the getNthFieldName function from the console....no problem.


      What am I doing wrong in the definition here ?

      It appears that what I did has Acrobat functions and document properties out-of-scope.

        • 1. Re: Document-level Javascript as Object
          try67 MVP & Adobe Community Professional

          1) I never really use it, but it's possible it's just a list of the top-level methods, not nested ones.

          2) Correct. If you nest methods then you need to pass a reference to the document object. The "this" object will not always point to it.

          3) This method returns the name of a field, not a Field object.

          • 2. Re: Document-level Javascript as Object
            Test Screen Name Most Valuable Participant

            The JavaScript API document gives the definition of "this" in various contexts. In any context not listed it is undefined - DO NOT USE IT. "this" is not a variable to be inherited.

            • 3. Re: Document-level Javascript as Object
              syswizardx Level 1

              I think this would work if you could make the document object a member of the Object being created.

              To do that, a constructor needs to be invoked so that it can instantiate a public property of the Object.

              var MyUtil = new Utility(this);

              inside of Utility I would have:

              this.oPDFdoc =  this;


              thereby transferring a reference to the entire PDF document to my Object.


              Getting this to work would eliminate the need for all of the document-level javascript references.