Skip to main content
Known Participant
February 27, 2018
Question

How to do an if then statement based on the value of a field on a PDF form

  • February 27, 2018
  • 1 reply
  • 1142 views

Dear All,

I hope somebody can help me. I'm not a programmer but need to create some calculations within a PDF form. I am trying to calculate the number of daily calories required based on a number of criteria. I have managed to make the calculation work if I am just looking at the values of each field but I need to take into account if the user is male or female. For example: If the user is male then use: 1066 + (6.23 * v4) + (12.7 * v3) - (6.8 * v2) and If the user is Female use: 66 + (6.23 * v4) + (12.7 * v3) - (6.8 * v2).

I am performing the calculation in the Calories field.

This is what I have so far which does work but doesn't have the capacity to change between male or female.

(function() {

var v1 = +getField("Gender").value;

var v2 = +getField("Age").value;

var v3 = +getField("Height").value;

var v4 = +getField("Weight").value;

var v5 = +getField("Activity").value;

var result = 66 + (6.23 * v4) + (12.7 * v3) - (6.8 * v2);

event.value = result;

})();

Thank you in advance.

This topic has been closed for replies.

1 reply

Thom Parker
Community Expert
Community Expert
February 27, 2018

Here's an article on how to write an if statement in a calculation:

https://acrobatusers.com/tutorials/conditional-execution

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
Known Participant
February 28, 2018

Thanks Thom,

I nearly have it working with:

var v1 = this.getField("Gender").valueAsString;

var v2 = this.getField("Age").value;

var v3 = this.getField("Height").value;

var v4 = this.getField("Weight").value;

var v5 = this.getField("Activity").value;

if( v1 ="Female") event.value = 1066 + (6.23 * v4) + (12.7 * v3) - (6.8 * v2);

else event.value = 66 + (6.23 * v4) + (12.7 * v3) - (6.8 * v2);

The problem is that it only recognises the 'Female' equation. If I change the Gender to Male it doesn't change the equation. Could this be something to do with the lack some sort of 'After Update', 'Requery' or 'Refresh function assigned to the other boxes?

try67
Community Expert
Community Expert
February 28, 2018

Replace:

if( v1 ="Female")

With:

if( v1=="Female")