Skip to main content
Participant
July 3, 2023
Answered

If, then calculations

  • July 3, 2023
  • 1 reply
  • 720 views

Hello!

I am trying to do a custom calualtion script for a pdf. 

I have the value "weight" that I am inputting into the form. 

I want the box name O2Min to calulate "Weight" *50 to give a result in that O2Min box. However, if that calulated value is below 0.5, I want the box to display 0.5 becasue it can't drop below that for safety reasons. Can someone help me write this out?

 

This topic has been closed for replies.
Correct answer Thom Parker

You can read about writing calculation scripts here:

https://www.pdfscripting.com/public/How-to-Write-a-Basic-PDF-Calculation-Script.cfm

 

Use this script in the calculation for the "O2Min" field

var nRslt = this.getField("Weight").value * 50;
if(nRslt < 0.5)
  nRslt = 0.5;

event.value = nRslt;

 

1 reply

Thom Parker
Community Expert
Thom ParkerCommunity ExpertCorrect answer
Community Expert
July 4, 2023

You can read about writing calculation scripts here:

https://www.pdfscripting.com/public/How-to-Write-a-Basic-PDF-Calculation-Script.cfm

 

Use this script in the calculation for the "O2Min" field

var nRslt = this.getField("Weight").value * 50;
if(nRslt < 0.5)
  nRslt = 0.5;

event.value = nRslt;

 

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
try67
Community Expert
Community Expert
July 4, 2023

Note that the code above will cause 0.5 to show even when Weight is empty... Not sure if that's what you wanted to achieve.

Thom Parker
Community Expert
Community Expert
July 4, 2023

Well, lets get that fixed up.

 

var cWt = this.getField("Weight").valueAsString;
if((cWt == "") || !isNaN(cWt))
   event.value = "";
else{
   var nRslt = Number(cWt) * 50;
   if(nRslt < 0.5)
      nRslt = 0.5;

   event.value = nRslt;
}
Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often