6 Replies Latest reply on Apr 15, 2011 10:16 PM by maxwyss

    Improving performance of scripts in a PDF form

    angelsfury77

      Hello there.  I'm a bit new to the scripting in Acrobat and find myself with a complex form that has a number of instances (about 70)  of the following script (specific to individual fields):

       

      event.value=this.getField("Ranks").value + this.getField("Mod").value + this.getField("MiscMod").value;
      var trained = "Untrained";
      var skill = "Ranks";
      if ( (this.getField(trained).value != "On" ) && (this.getField(skill).value < 1)) {
      event.target.textColor = ["G", 1]; }
      else {event.target.textColor = ["G", 0];}

       

       

       

      The code works fine, but with this many instances, performance of form is sluggish while updating. In addition, there are approximately 200 simple addtion fields being used.

       

      I'm looking for any advice, scripting or otherwise, to help improve performance.

       

      Thanks so much in advance.

        • 1. Re: Improving performance of scripts in a PDF form
          try67 MVP & Adobe Community Professional

          First of all, if the script is identical, except for the names of the fields

          you're using, you should really consider placing it in a document-level

          function that takes the names of the fields as parameters. It will save a

          lot of extra code and will make maintenance much simpler.

           

          I don't see any real way to improve the efficiency of this simple script.

          The only thing I can think of is not to define variables for the names of

          the fields in the if-statement. Since you're only using them once, just

          enter the actual name into getField.

          1 person found this helpful
          • 2. Re: Improving performance of scripts in a PDF form
            maxwyss Level 4

            As a consequence of the event processing model in Acrobat, whenever the Calculate event is initiated, the whole Calculate chain gets executed (meaning that the calculations in every field having a Calculation script gets executed. And if as consequence of this execution another Calculate event gets triggered, the same happens recursively again.

             

            This is the reason why forms with many small Calculation scripts get more and more sluggish.

             

            To overcome this, you have some possibilities, depending on the effects you need to accomplish and whether calculations depend on each other.

             

            a) if indivicual calculations do NOT depend on each other, you can move your scripts to the Validation event, which only gets executed when there is an actual change of the field's value itself.

             

            b) If you have "chained" calculations, where the results depend on each other, consolidate the individual scripts into one single big script. You assign this script to the Calculate event of the final result field. You may even assign it to the Calculate event of a hidden field not being involved in any other way into the form (the reason this works is mentioned above).

             

            In both cases you have reduced the number of fields wiht a Calculate event, and therefore massively reduced the number of times the Calculate chain gets executed.

             

            If you have chosen method b), you have an additional possibility to speed up your form. You can suppress the recursive recalculation by adding

             

               this.calculate = false ;

             

            at the beginning of the script, and

             

               this.calculate = true ;

             

            at the end.

             

            With the above steps, you should be able to considerably speed up your form.

             

            HTH.

             

            Max Wyss.

            1 person found this helpful
            • 3. Re: Improving performance of scripts in a PDF form; calulating only when needed.
              angelsfury77 Level 1

              Thank you for the responses.  I cleaned up the variables post haste and consolidated as many calculations as possible.  Devling deeper into the problems, approximately half of those 70 calculations really needed slightly different code, and about 20 of the others didn't really need any code at all.

               

              What would be ideal, however, is being able to update the calculation only when one of those fields is updated.  Is that possible, and if so, what would that look like?

               

              Thank you again.

              • 4. Re: Improving performance of scripts in a PDF form; calulating only when needed.
                MarkWalsh Level 4

                If you create a function in the document scripts :

                 

                function updateField() {

                event.value=this.getField("Ranks").value + this.getField("Mod").value + this.getField("MiscMod").value;
                var trained = "Untrained";
                var skill = "Ranks";
                if ( (this.getField(trained).value != "On" ) && (this.getField(skill).value < 1)) {
                event.target.textColor = ["G", 1]; }
                else {event.target.textColor = ["G", 0];}

                }

                 

                you could replace the code in each field with the following function call, and they would all use the same code:

                 

                updateField()

                 

                 

                 

                Without knowing exactly what 'slightly different code'' entails, you can add parameters to the function call:

                 

                function updateField(Rank, Mod, MiscMod) {

                event.value= Rank + Mod + MiscMod;
                var trained = "Untrained";
                var skill = "Ranks";
                if ( (this.getField(trained).value != "On" ) && (this.getField(skill).value < 1)) {
                event.target.textColor = ["G", 1]; }
                else {event.target.textColor = ["G", 0];}

                }

                 

                and you could call it in each field with different values for 'Rank', 'Mod', and 'MiscMod' (or whatever parameters you use) like this:

                 

                updateField(getField("Ranks").value, this.getField("Mod").value, this.getField("MiscMod").value )

                or

                updateField(getField("aDifferentRanks").value, this.getField("aDifferentMod").value, this.getField("aDifferentMiscMod").value )

                or

                updateField(47, 12, 45)

                 

                 

                If you need to make changes, you only need to change the 'updateField' function once, and any field which calls it will use the updated code.

                • 5. Re: Improving performance of scripts in a PDF form; calulating only when needed.
                  angelsfury77 Level 1

                  Thank you for the response, and the code was quite helpful for my own understanding of syntax in making the document level functions, well, function.  My brain was making it seem far harder than it is.  I have already streamlined the code per all of your (pl) suggestions, but I am running into a fundamental problem: this form has too many fields.

                   

                  I was unclear earlier when I said:

                   

                  "being able to update the calculation only when one of those fields is updated"

                   

                  What I really meant was, is there a way to make fields update their calculated values only when their dependents change?  I am going to try playing around with Validations instead of Calculations, but any insights or guidance is always appreciated.  You folks are great!

                  • 6. Re: Improving performance of scripts in a PDF form; calulating only when needed.
                    maxwyss Level 4

                    To limit the number of Calculation event recursions, you can use the Validate event of the field causing a change; this event will only happen when there is an actual change in that field.

                     

                    Otherwise, as said, consolidate the code into one (or a few) big scripts and reduce the number of fields with autocalculation. You may go as far as you will no longer be able to change the calculation order (because you have only one single field having a Calculate event.

                     

                    HTH.

                     

                    Max Wyss.