Search code examples
acrobat

Is there a way to simplify incremental calculations in acrobat?


Other than using a long string of if statements is there a way to program an acrobat text box to incrementally calculate based on variables entered into an other text box? Here is what I have.

 var v = this.getField("FixNum").value;

 if (v == "1")
 {
 event.value = 84 ;
 }
 else if (v == "2")
 {
 event.value = 88 ;
 }
 else if (v == "3")
 {
 event.value = 92 ;
 }
 else
 {
 event.value = "";
 }

As you can see this will get cumbersome because this goes from 1 - 9 in this pattern where 9 = 116, then 10 - 100 where 10 = 135. After 100 the pattern is 495 + 6 for every unit i.e. 101 = 501. I hope someone can understand this because I can't think of another way to ask!


Solution

  • Several possibilities:

    • If you can come up with a formula, it would be simplest.

    • If the value pairs are arbitrary, you might create a lookup table (in form of an array).

    • If the input numbers are (more or less) consecutive integers, you can work with a simple array, where the index is the input number, and the element with that index number is the output number.

    • If it is more complicated, you would have to create a 2-dimensional array, where each element is an array of input and output number.

    • If you are looking for a simple replacement of if…else if statements, you can use the switch… statement, which would work well for totally arbitrary value pairs.

    Update, following the comment by OP:

    According to a comment by the OP, the input numbers are consecutive, which means that the third point above would be the method of choice. For that, proceed as follows:

    Create a document-level script (the name does not matter):

    var myPricesArr = [0, 12, 15, 17, 23, 27, 30, 33] ;
    

    alternate possibility:

    var myPricesArr = new Array() ;
    myPricesArr[0] = 0 ;
    myPricesArr[1] = 12 ;
    myPricesArr[2] = 15 ;
    myPricesArr[3] = 17 ;
    myPricesArr[4] = 23 ;
    myPricesArr[5] = 27 ;
    myPricesArr[6] = 30 ;
    myPricesArr[7] = 33 ;
    

    Both variants are equivalent, but the second one is easier to maintain, but requires more typing…

    Create a text field named "numFixts", formatted as number, no decimals, maybe with a maximum value (to protect the form from failing).

    Create a text field named "fixtsAmt, formatted as number. In the Calculate event of this field add the following JavaScript:

    event.value = myPricesArr[this.getField("numFixts").value] ;
    

    And that should do it.