Java Formula in PDF form

I have a form that has some basic formulas which are working fine. I'm having difficulty with the following:

Field_1 has a qty of 3
Field_2 has a qty of 7
Field_3 I want the results to be 1-(Field_1/Field_2)

This has to go in the Custom Calculation script box in Java format, but I don't know Java.

Any help would be greatly appreciated.

Thanks

Lisa


Lisa Gilliatt


4 Answers

Voted Best Answer

It's acutely not Java, it's JavaScript - similar name, but completely different language.

There is one potential problem with this script: If Field_2 is set to 0, then your calculation will result in "Not a Number" (or NaN), so we need to prevent that. If that's not a problem, then you could just use the "Simple Field Notation" format, and get away without using JavaScript:

1 - (Field_1 / Field_2)

If you need to prevent the Infinity or NaN, then you have to go with JavaScript. The following script will only show a result if both Field_1 and Field_2 have valid data:

var f1 = this.getField("Field_1").valueAsString;
var f2 = this.getField("Field_2").valueAsString;

if (f1 != "" && f2 != "" && f2 != 0) {
    event.value = 1 - (f1 / f2);
}
else {
    event.value = "";
}

Karl Heinz Kremer
PDF Acrobatics Without a Net
PDF Software Development, Training and More...
http://www.khkonsulting.com


By Karl Heinz Kremer   

Hi.

  • Java and JavaScript is not the same thing.

  • You don't need JavaScript, you can use the Simplified field notation instead:

1 - (Field_1 / Field_2)

==> No space, no dot, no slash, etc. in fields names, otherwise you must escape them.


JR Boulay   

Great mind…

;-)


JR Boulay   

This should work exactly as written in a simplified field notation for Field_3. However, the answer could be a non-numeric due to a zero denominator in the division to this custom calculation script will make any non-numeric values blank:

event.value=1-(this.getField("Field_1").value/this.getField("Field_2").value);
if (event.value=="NaN" || event.value==Infinity  || event.value==-Infinity)
{event.value=""}


David Dagley   


Please specify a reason: