7 Replies Latest reply on May 5, 2009 7:09 AM by Patrick Leckey

    Does Acroforms JavaScript provide facilities to deal with arrays?

    Ramon F Herrera Level 1

      After the "Detect Fields" feature is done, I am left with a bunch of scalar field names. Naturally, one needs to perform operations such as determining the number of rows and columns, and using indexing.

       

      At the very least, I would like to determine the list of field names.

       

      -Ramon

        • 1. Re: Does Acroforms JavaScript provide facilities to deal with arrays?
          Patrick Leckey Level 3

          What do you mean by "deal with arrays"?  That is not very specific.  Arrays are part of the ECMAScript specification, so yes, Acrobat JavaScript will work with arrays.

          • 2. Re: Does Acroforms JavaScript provide facilities to deal with arrays?
            Ramon F Herrera Level 1

            I guess I should be more specific, then.

             

            There is:

             

            (1) Generic JavaScript.

             

            (2) JavaScript as implemented in the traditional AcroForms.

             

            (3) JavaScript as implemented in the newfangled XFA forms.

             

            My question pertains to (2).

             

            I use the following type of statement all the time:

             

            var myVariable = this.getField("Some Field Name");

            var myOtherVariable = this.getField("Some Other Field Name");

             

            Later on, the object fields and methods are accessed as follows:

             

            myVariable.hidden = true;

            myOtherVariable.hidden = false;

             

            What I need is to perform the above in a table composed of many fields.

             

            TIA,

             

            -Ramon

            • 3. Re: Does Acroforms JavaScript provide facilities to deal with arrays?
              gkaiseril MVP & Adobe Community Professional

              If you want better results then some standard name that works with every possible shortcut, you need to do your own work.

               

              Suppose I have 5 fields called 'Text1', 'Text2', 'Text3', 'Text4' and 'Text5'. Can use the 'Simplified field calculation' to sum the data or write a 'Custom calculation script'.

               

              The 'Simplified field calculation':

               

              Text1 + Text2 + Text3 + Text4 + Text5

               

              A 'Custom calculation script':

               

              // array of field names to sum
              var aFields = new Array('Text1',  'Text2', 'Text3', 'Text4', 'Text5');
              var fSum = 0; // variable for sum
              // loop through the fields
              for (i = 0; i < aFields.length, i++) {
              fSum += Number(this.getField(aFields[i]).value); // add the numeric values
              } // end loop
              event.value = fSum; // assign value

               

              Now if you named your fields 'Text.0', 'Text.1', 'Text.2', 'Text.3' and 'Text.4'.

               

              You can do the "Sum of" 'Text' option,

               

              A 'Custom calculation script':

               

              // get field object
              var oField = this.getField('Text');

              // make an array from the object
              aField = oField.getArray();
              var fSum = 0; // variable for sum of values
              // loop through the array of fields
              for(i = 0; i < aField.length; i++) {
              fSum += Number(aField[i].value); // add values
              } // end of loop
              event.value = fSum;

               

              or

               

              // get field object array
              var aField = this.getField('Text').getArray();
              var fSum = 0; // variable for sum of values
              // loop through the array of fields
              for(i = 0; i < aField.length; i++) {
              fSum += Number(aField[i].value); // add values
              } // end of loop
              event.value = fSum;

               

               

              For form fields with a common propertiy,  like above one can hide them all with:

               

              this.getFeild('Text').display = display.hidden;

              1 person found this helpful
              • 4. Re: Does Acroforms JavaScript provide facilities to deal with arrays?
                Patrick Leckey Level 3

                Ramon F Herrera wrote:

                 

                There is:

                 

                (1) Generic JavaScript.

                 

                (2) JavaScript as implemented in the traditional AcroForms.

                 

                (3) JavaScript as implemented in the newfangled XFA forms.

                 


                Actually Ramon that is incorrect.  Acrobat (assuming version 9.0) fully complied with the ECMA-262, edition 3 which is equivalent to JavaScript 1.5, regardless of whether you are using it in AcroForms, XFA forms or even on a PDF that does not contain any form fields.

                 

                What you are confusing as a different type of JavaScript are actually different object provided by the JavaScript engine based on the type of form.  For example, when you are using JavaScript in an HTML web page, you have access to the "document" object.  This object is not "part of" JavaScript.  It is simply a pre-defined object provided by your browsers JavaScript engine.  No where in the JavaScript or ECMAScript specification is the "document" object defined.

                 

                Since "arrays" are part of the ECMAScript specification, it doesn't matter whether you are using what you refer to as "Generic JavaScript", working with an AcroForm or an XFA form - arrays are handled in the same manner as they are a part of the base JavaScript language.  How you deal with objects inside those arrays may differ, but by definition in the ECMAScript specification arrays themselves will not change no matter what type of PDF you are working with.

                 

                It's a matter of learning to differentiate the language from the objects provided by the application in which you are using that language.  A common hiccup for someone used to a compiled language like C and then moving to an interpreted language like JavaScript.  Took me a while to wrap my head around it too.

                • 5. Re: Does Acroforms JavaScript provide facilities to deal with arrays?
                  Ramon F Herrera Level 1

                  Thanks to all that have answered. I will be more specific.

                   

                  What I really need is to roll the following into array notation:

                   

                  var leftTable = new Array(13, 3);
                  leftTable[0][0] = this.getField("Table 1.1.1");
                  leftTable[0][1] = this.getField("Table 1.1.2");
                  leftTable[0][2] = this.getField("Table 1.1.3");

                   

                  (I think I can manage to generalize the above, the part that is not working is the one below):

                   

                  leftTable[0][0].hidden = true;
                  leftTable[0][1].hidden = true;
                  leftTable[0][2].hidden = true;

                   

                  When those lines are executed I get an error message:

                   

                  "leftTable[0][0] has no properties"

                   

                  Any assistance is most welcome...

                   

                  -Ramon

                  • 6. Re: Does Acroforms JavaScript provide facilities to deal with arrays?
                    Bernd Alheit Adobe Community Professional & MVP

                    leftTable[0] has the value 13 and leftTable[1] has the value 3. leftTable[0][0] has no value.

                    1 person found this helpful
                    • 7. Re: Does Acroforms JavaScript provide facilities to deal with arrays?
                      Patrick Leckey Level 3

                      Ramon,

                       

                      The problem is how you are defining your array.

                       

                      new Array(13, 3) does not make two-dimensional array with three elements for each of the thirteen primary elements.  That makes a one-dimensional array with two valuues; 13 and 3.

                       

                      When controlling array size, you can only pass the first element as an integer to set the size.  If you pass more than one element the JS interpreter will assume you are defining the literal elements in your array.

                       

                      For a two-dimensional array, use the following method:

                       

                      var leftTable = new Array(13);

                      for ( var i = 0; i < 13; i++ )

                           leftTable[i] = new Array(3);

                       

                      Of course with JavaScript, you do not need to define an array size as it will grow as needed.

                       

                      var leftTable = new Array();

                      leftTable[0] = new Array();

                      leftTable[0][0] = this.getField(...);