25 Replies Latest reply on Feb 5, 2016 7:47 AM by emmanuelem85

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

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!

• ###### 1. Re: Javascript on a PDF that checks the value of a specific field and then do the right calculation.

Is it not possible? t_t

• ###### 2. Re: Javascript on a PDF that checks the value of a specific field and then do the right calculation.

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 = "";
```
• ###### 3. Re: Javascript on a PDF that checks the value of a specific field and then do the right calculation.

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!

• ###### 4. Re: Javascript on a PDF that checks the value of a specific field and then do the right calculation.

Yes, it is possible. Use this syntax:

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

• ###### 5. Re: Javascript on a PDF that checks the value of a specific field and then do the right calculation.

Thank you very very much!!!!

• ###### 6. Re: Javascript on a PDF that checks the value of a specific field and then do the right calculation.

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 = "";

• ###### 7. Re: Javascript on a PDF that checks the value of a specific field and then do the right calculation.

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.

• ###### 8. Re: Javascript on a PDF that checks the value of a specific field and then do the right calculation.

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..

• ###### 9. Re: Javascript on a PDF that checks the value of a specific field and then do the right calculation.

Can you share the file in question?

• ###### 11. Re: Javascript on a PDF that checks the value of a specific field and then do the right calculation.

First of all, that's not a radio-button but a drop-down list. And I'm not following what you're trying to achieve with it, exactly. Please clarify.

• ###### 12. Re: Javascript on a PDF that checks the value of a specific field and then do the right calculation.

Oh sorry.. you are right.

Ok, this is what i need to achieve:

I have to calculate the necessary gasoline for every kind of different operation in agricolture.

I have a specific chart that tells the necessary gasoline for every kind of work.

Example:

Necessary Gasoline for Organic Companies:

Treshing

• ###### 13. Re: Javascript on a PDF that checks the value of a specific field and then do the right calculation.

OK, but how is it supposed to work, exactly? Is the "operation" something the user enters manually or selects from a drop-down menu?

• ###### 14. Re: Javascript on a PDF that checks the value of a specific field and then do the right calculation.

Sorry the answer was not complete!

The operations and the coltures are limited, like 8 operations and 10 coltures, so i would like to make the user choose from a drop down menu, to avoid errors.

Organic company or not: Check button

Kilometers: Manually

Example:

Necessary Gasoline for Organic Companies:

Treshing

Beans: 40 liters per Kilometer (Hectar)

Wheat: 35 liters per Kilometer (or Hectar)

Seeding

Beans: 50 liters per Km

Wheat: 46 liters per Km

Necessary Gasoline for non Organic Companies:

Treshing

Beans: 65 liters per Kilometer (Hectar)

Wheat: 65 liters per Kilometer (or Hectar)

Seeding

Beans: 56 liters per Km

Wheat: 67 liters per Km

At this moment we do the calculations manually on the pdf, we check if the company uses organic methods or not, then write the colture, then again the km, and finally we go to check the chart, take the calculator and do the simple math.

What i want to achieve is that the user has just to check is the company is organic or not, what kind of work has done, how many kilometers of it and on which colture, and on the field "necessary gasoline" it appears the outcome, without having to go to check the chart and do the math manually every single time.

• ###### 15. Re: Javascript on a PDF that checks the value of a specific field and then do the right calculation.

Then you'll need to convert your formulas to JS and insert the values of the fields where necessary.

If you use a check-box you can check if it's value is "Off" or not to see if it's ticked.

For text fields and drop-down menus you can use the literal values, as I did in the code above.

• ###### 16. Re: Javascript on a PDF that checks the value of a specific field and then do the right calculation.

I don't know why the code above works with text fields and doesn't with drop-down menus.. what do you mean with literal values?

• ###### 17. Re: Javascript on a PDF that checks the value of a specific field and then do the right calculation.

Can you show an example of it not working?

By literal values I mean just the plain text that you see in the field.

• ###### 18. Re: Javascript on a PDF that checks the value of a specific field and then do the right calculation.

try67 ha scritto:

Can you show an example of it not working?

By literal values I mean just the plain text that you see in the field.

I did a pdf for you to check and it worked! Probably i was doing something wrong before!

This is the new pdf, i wrote it in english!

I can't make the code work with the check for ORGANIC FIELD, could you check it please? And thanks for all the help!!!

http://www.filedropper.com/examplepdf

This is the code that doesn't work with the check:

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

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

var cultivation = this.getField("CULTIVATIONDROPDOWN").value;

var check = this.getField("organiccheck").value;

if (operation=="Seed"&&cultivation=="Wheat"&&check=="off") event.value = km * 40;

else if (operation=="Seed"&&cultivation=="Beans") event.value = km * 70;

else if (operation=="Weed"&&cultivation=="Wheat") event.value = km * 120;

else if (operation=="Weed"&&cultivation=="Beans") event.value = km * 150;

else event.value ="";

• ###### 19. Re: Javascript on a PDF that checks the value of a specific field and then do the right calculation.

How do you want the organic check-box to influence the calculations you're doing?

• ###### 20. Re: Javascript on a PDF that checks the value of a specific field and then do the right calculation.

The value of a check-box when it's not checked is "Off", not "off"...

• ###### 21. Re: Javascript on a PDF that checks the value of a specific field and then do the right calculation.

Oops! Right!

I'd like to create two subroutines of code, like this:

if organic is "off"

"code"

if organic is "on"

"code"

Is it possible?

Is there a good ebook to learn about Acrobat Javascript?

I'd really like to learn about it!

• ###### 22. Re: Javascript on a PDF that checks the value of a specific field and then do the right calculation.

if (check=="Off") {

...

} else {

...

}

• ###### 23. Re: Javascript on a PDF that checks the value of a specific field and then do the right calculation.

Thank you again, this will simplify enormously my work! Thank you also from my coworkers here!

I bought two books about JavaScript and Javascript for acrobat And I hope to help other people like you helped me

Generic questions:

1) Is it possible to reduce the possible choices of a drop down menu dinamically?

If for example the culture is "Almond Trees" then the operation dropdown menu shows only the possible operations for almond trees.

2) if "AND" operator is "&&" which is the "OR" operator?

• ###### 24. Re: Javascript on a PDF that checks the value of a specific field and then do the right calculation.

You're very welcome...

1. Yes, by using the setItems method of the Field object.

2. OR in JS is ||

• ###### 25. Re: Javascript on a PDF that checks the value of a specific field and then do the right calculation.

Thank you very much!

See you!