12 Replies Latest reply on Dec 1, 2013 11:37 PM by kb-NetCom

# How can I put a mathematically if - formula in a field of a pdf - form?

Hello together,

I am absolutely unexperienced with JavaScript.

I have to implement formulas into a Form.

In one field I have to add field a and b - but the result should be displayed only, if the result is positiv.

In the next field the result has to be displayed, when it is negativ.

this has to be done in about 15 rows and at the end of every row is a resulting field which multiplies the negative or positive number with a price.

And .. to sum up two more things, I have to add up all numbers which are positive and all numbers which are negative.

I am quite sure, that this is not really difficult - but it is for me.

It would be great, If anybody could help me.

Klaus

• ###### 1. Re: How can I put a mathematically if - formula in a field of a pdf - form?

This is easily done. You just use the built-in Sum function under the Calculate tab, and then enter this code as the custom validation script:

(For the positive only fields)

if (Number(event.value)<0) event.value = "";

(For the negative only fields)

if (Number(event.value)>0) event.value = "";

The total fields can also be implemented with the built-in Sum function...

• ###### 2. Re: How can I put a mathematically if - formula in a field of a pdf - form?

thanks a lot for the quick reply.

But .. I think, I have not expressed correctly, what I need.

there are 7 Fields in a row - and in Field 1 and 2 numbers have to be put in - and field 1 and 2 have to be added - and if this result is positiv it has to be displayed in field 3 and if negative in field 4.

How can I define field 1 and 2, that they have to be added .. and - do I have to mark the code with a start and end sign??

• ###### 3. Re: How can I put a mathematically if - formula in a field of a pdf - form?

I understood the situation, but let me give you some more detailed advice. So you set the following setting as the Calculate option for fields 3 AND 4:

For "Field 3" you use the following as the custom Validation script:

And for "Field 4" you use this as the custom Validation script:

The results are:

You can find the file here:

https://files.acrobat.com/preview/c00025e0-8bfb-46d7-9ac9-300e87578333

• ###### 4. Re: How can I put a mathematically if - formula in a field of a pdf - form?

Really good :-) !!!

Thank you very much.

Is it the same, when I want to subtract Field 2 from Field 1? There is no subtraction in the calculation ????

• ###### 5. Re: How can I put a mathematically if - formula in a field of a pdf - form?

You will have to use the simplified field notation or a custom JavaScript calculation to perform subtraction or division. If there is any chance of a zero divisor then one needs to use a custom JavaScript calculation.

As to your specific example, because the field names have a space within their name you need to either use a special character in the simplified field notation or a custom JavaScript calculation.

Simplified field notation requires use of the JavaScript escape character to proceed the space or other white space/special character.

Field\  1 - Field\ 2

Custom JavaScript calculation:

event.value = this.getField("Field 1").value - this.getField("Field 2").value;

• ###### 6. Re: How can I put a mathematically if - formula in a field of a pdf - form?

!! Thank you GKaiseril,

this works! :-)

Now I tried to combine the subtraction with the if - formula, but somehow I get a syntax error ... even after trying nearly every possibility - I cannot find out, what is wrong

Please have a look to what I did ...

if((event.value=this.getField("Anzahl_berechnet_01").value-this.getField("Anzahl_erhalten_ 01").value)<0;"";event.value=this.getField("Anzahl_berechnet_01").value-this.getField("Anz ahl_erhalten_01").value)

• ###### 7. Re: How can I put a mathematically if - formula in a field of a pdf - form?

You have at the end a close parenthesis (bracket) which belongs at the end of the if condition, not the end of the statement. Look at the form in reply#1.

• ###### 8. Re: How can I put a mathematically if - formula in a field of a pdf - form?

Hello together,

thanks again for all yóur help.

Everything is very helpful and works at all!!

Nevertheless I have anexpected results ... ;-).

In fields with calculation field1+field2 the result is not always correct f.e. 60-70=5   and  2 fields right it calculates 10*5=350€.

In fact I am worried !! (and a little bit confused).

Is there anybody who has experienced the same ...????

Do you know, how to change this miscalculating?

Thanks again in advance ..... !!

• ###### 9. Re: How can I put a mathematically if - formula in a field of a pdf - form?

JavaScript calculations do have some surprises, especially with big numbers and fractions. People working with money often find it is much harder than they expected. But small whole numbers is something JavaScript does well.

If you seem to see that 60-70 isn't -10 and 10*5 isn't 50, then first look at the formatting of the result fields. And look for other JavaScript and calculations which may be left in that affect these fields. If that doesn't help, please post your final code. Please use copy/paste, every detail matters.

• ###### 10. Re: How can I put a mathematically if - formula in a field of a pdf - form?

It sounds like some of the calculations are one step behind. You may need to correct the field calculation order.

• ###### 11. Re: How can I put a mathematically if - formula in a field of a pdf - form?

Good Morning,

I tried several things to solve the calculation problems.

At last I am quite sure, that the problem is, that our trainee, who had to make the formular, has built the fields and the layout in EXCEL and then imported to Acrobat.

When I made all fields manually in Acrobat, all the calculations and formulas you proposed to me, worked well and fine. Right now I think, my problems are solved. I will come back to tell you - probably tomorrow.

So ... thank you very much.

• ###### 12. Re: How can I put a mathematically if - formula in a field of a pdf - form?

Good morning all together,

I hope you enjoyed your weekend!?

All the things you told me in your answers above are working fine - so far !THANK YOU!.

Now I need to know, if I can select from several rows all the items which are positiv or all items which are negativ ...

For a better understanding: in the formular are 20 rows which calculate if things are delivered or not. If not delivered and on the bill  -  it would be a value which has to be subtracted. The other way around, if there are goods which are delevered, but not on the bill, we have to add the price for it - positive numbers !

I hope there is a function for this - otherwise it is a very uncomfortable if .. function.