These forums are now Read Only. If you have an Acrobat question, ask questions and get help from one of our experts.

Script help

adb23
Registered: Sep 10 2008
Posts: 5
Answered

I haven't done any programming in years and it was basic C++ so I really don't know much about javascript. I'm working on a simple pricing pdf that I want user input in text fields to end up giving them their final price. Not sure if I am going about coding this correctly but lets say I am starting with two text fields. I make one text field "Num1" and another "Total". Now on my Num1 user input will be the quantity of the product but in the calculation coding I want it to calculate what that specific total will be from their quantity which is determined on different price breaks so I can call it up in my "Total" field later. I want the quantity to stay in the field though so I don't need the calculation to actually show up in that exact location. Here is how I am trying to work this on "Num1" text field :

var n1

if (this.getField("Num1").value <= 9)
{ n1 = this.getField("Num1").value * 15.74; }
else if (this.getField("Num1").value <25 && =>10)
{ n1 = this.getField("Num1").value * 9.25; }
else if (this.getField("Num1").value >=25 && <50)
{ n1 = this.getField("Num1").value * 7.40; }
else if (this.getField("Num1").value >=50)
{ n1.value = this.getField("Num1").value * 5.95; }
else { n1 = 0; }

With the hopes that when I get down to my "Total" field I can just add up n1 and other n#'s I have made from other text fields on the form

event.value = n1 + n2 + etc;

Now, I have no clue if I am even going about this in the correct way so some help would be much appreciated.

adb23
Registered: Sep 10 2008
Posts: 5
Well it looks like this is all wrong. Can I just use input value in text field "Num1" and then do something along these lines at the very end with a calculation script in the "Total" field?
gkaiseril
Expert
Registered: Feb 23 2006
Posts: 4308
Your code should read:

var n1 = 0;

if (this.getField("Num1").value <= 9) {
n1 = this.getField("Num1").value * 15.74;

} else if (this.getField("Num1").value >= 10 && this.getField("Num1").value < 25) {
n1 = this.getField("Num1").value * 9.25;
} else if (this.getField("Num1").value >this.getField("Num1").value >= 25 & this.getField("Num1").value >this.getField("Num1").value < 50) {
n1 = this.getField("Num1").value * 7.40;
} else if (this.getField("Num1").value >= 50) {
n1 = this.getField("Num1").value * 5.95;
}

console.show();
console.clear();
console.println(n1);

You need to learn your comparative operators and variables in general do not have a value property. You also might find that somewhere along the line that one or more of your large number of stored variables might get changed by some other script within Acrobat. You could do all the calculations for each field in the total field and then just have one variable.

I would not use the "getField()" object for the field I have placed the calculation in but the "event" object. I would also avoid the nested "if" stamens, they are just too confusing. With JavaScript there is a "switch()" stamen that provides a better way to test values and execute a specific line of code when the logical condition is meet and then skips the code for the other conditional test.

For the "Custom calculation script" in the total field:

event.value = ''; // clear the field;

// field Num1
quantity = this.getField("Num1").value
extendedCost = 0; // get the cost for a field
switch(true) {
// less than 9 units
case (quantity <= 9) :
extendedCost = quantity * 15.74;
break;
// more than 9 and less than 25
case (quantity<25) :
extendedCost = quantity * 9.25;
break;
// more than 25 and less than 50
case (quantity< 50) :
extendedCost = quantity * 7.40;
break;
// 50 or more
case (quantity>=50) :
extendedCost = quantity * 5.95;
break;
// all other values
default:
extendedCost = 0;
break;
} // end switch
event.value += extendedCost;

console.show();
console.clear();
console.println("extendedCost for Num1 = " + extendedCost);
console.println("sum total = " + event.value);

// field Num2
quantity = this.getField("Num2").value
extendedCost = 0; // get the cost for a field
switch(true) {
// less than 9 units
case (quantity <= 9) :
extendedCost = quantity * 15.74;
break;
// more than 9 and less than 25
case (quantity<25) :
extendedCost = quantity * 9.25;
break;
// more than 25 and less than 50
case (quantity< 50) :
extendedCost = quantity * 7.40;
break;
// 50 or more
case (quantity>=50) :
extendedCost = quantity * 5.95;
break;
// all other values
default:
extendedCost = 0;
break;
} // end switch
event.value += extendedCost;

console.println("extendedCost for Num2 = " + extendedCost);
console.println("sum total = " + event.value);

// and so on.

George Kaiser