Skip to main content
emmanuelem85
Known Participant
February 1, 2016
Answered

Javascript on a PDF that checks the value of a specific field and then do the right calculation.

  • February 1, 2016
  • 3 replies
  • 3211 views

Hi guys!

Sorry for my english, it's not my mothertongue.

This is my first time writing here, i just discovered the great opportunities of Javascript in PDF files and i am very excited about it!

I don't have experience with Javascript, this is what i would like to do:

Basically, i need to calculate how many liters of gasoline are necessary to do a specific job in agricolture.

I have a table that is like this:

Seed: 120 Liters/Km

To weed: 20 Liters/Km

Fertilize: 140 Liters/Km

I have 3 fields in the pdf: OPERATION/ KM / NECESSARYGASOLINE

I need something like this:

Seed = 120;

Weed = 20;

Fertilize = 140;

IF OPERATION is "Seed" then NECESSARYGASOLINE= 120*KM;

ELSE IF OPERATION is "Weed" then NECESSARYGASOLINE = 20*KM;

ELSE IF OPERATION is "Fertilize" then NECESSARYGASOLINE = 140*KM;

Is it possible to translate it in Javascript?

Thank you very much for the help! It would really be wonderful!

This topic has been closed for replies.
Correct answer try67

Yes, it is possible. Use this syntax:

if (organic=="Yes" && cultivation=="Wheat")

3 replies

emmanuelem85
Known Participant
February 4, 2016

Sorry for asking again, i tried the code with the standard Form and it worked, but when i change the form to a "choice" form it doesn't.

I mean, if i write "Wheat" "Beans" manually every time on the form it works, if i make a form with choices like "Wheat" "Beans" and make the user choice between them it doesn't, is there any change on the code that i should make for this different kind of form?

This is the code

var operation = this.getField("Operation").value;

var km = Number(this.getField("Km").value);

if(operation=="Seed")event.value = km * 20;

else event.value = "";

try67
Community Expert
Community Expert
February 4, 2016

Do you mean radio-buttons? If so, you have to make sure that you set their "choice values" (under Properties - Options) to these values you're comparing to.

emmanuelem85
Known Participant
February 4, 2016

I tried, i don't know why but the same code works when the OPERATION is in a standard form and doesn't when it's on a form with radio-buttons..

try67
Community Expert
Community Expert
February 2, 2016

Use this code as the custom calculation script for NECESSARYGASOLINE:

var operation = this.getField("OPERATION").value;

var km = Number(this.getField("KM").value);

if (operation=="Seed") event.value = km * 120;

else if (operation=="Weed") event.value = km * 20;

else if (operation=="Fertilize") event.value = km * 140;

else event.value = "";

emmanuelem85
Known Participant
February 2, 2016

Woah thank you SO MUCH!!!!

It works like a charm!

Can i ask another question to make the calculation even more useful?

I would like to add another two fields to the calculation: CULTIVATION and ORGANIC

I'd like to modify the calculation in this way:

  1. var operation = this.getField("OPERATION").value; 
  2. var km = Number(this.getField("KM").value); 
  3. var cultivation = this.getField("CULTIVATION").value
  4. var organic = this.getField("ORGANIC").value
  5.  
  6. if (organic=="Yes") and (cultivation=="Wheat") and (operation=="Seed") event.value = km * 120
  7. else if (organic=="Yes") and (cultivation=="Beans") and (operation=="Seed") event.value = km * 110; 
  8. else if .........................................etc etc...............................................
  9. MANY LINES LATER.................................................
  10. else if event.value = ""

Is it possible? Could you write me down an example? I can't make it works, i don't know the right sintax! 

try67
Community Expert
try67Community ExpertCorrect answer
Community Expert
February 2, 2016

Yes, it is possible. Use this syntax:

if (organic=="Yes" && cultivation=="Wheat")

emmanuelem85
Known Participant
February 2, 2016

Is it not possible? t_t