Skip to main content
Participating Frequently
May 10, 2019
Answered

If/Else Statement from text to numbers in PDF Form

  • May 10, 2019
  • 2 replies
  • 4387 views

Hello, I have been reading few answers in this forum but none is about my case. I'm trying to add an IF / ELSE statement to a quote form in PDF but is not working and obviously I'm writing a wrong code.

I have a drop list called "STATE" and another text field called "FL_CALC". Now my question is how can I get a numerical result result in "FL_CALC" field depending the selection made in "STATE"  field?

All that I need is: that number "145" appear in "FL_CALC" field when you choose FL state in "STATE" field... if you choose any other state, then in "FL_CALC" appear 450. Is this possible?

This topic has been closed for replies.
Correct answer try67

Sure. Use this code as the custom calculation script of "FL_CALC":

event.value = (this.getField("STATE").valueAsString=="FL") ? "145" :  "450";

2 replies

Karl Heinz  Kremer
Community Expert
Community Expert
May 10, 2019

I would recommend that you learn how JavaScript works. It's not overly complex, and you can probably learn a lot about it in just an afternoon. A while ago I wrote a blog post about how to learn to program in JavaScript for Acrobat: Learning to Program JavaScript for Adobe Acrobat - KHKonsulting LLC

In general, an if else statement looks like this:

if (somethingIsTrue) {

  // .... if block

}

else {

  // .... else block

}

If "somethingIsTrue" is true, then the "if block" gets executed, otherwise the "else block".

So, in your example, the user picks the "STATE", and when STATE is set to "FL", you want FL_CALC to be 145, and otherwise you want FL_CALC to be set to 450:

if (this.getField("STATE").value == "FL") {

  this.getField("FL_CALC").value = 145;

}

else {

  this.getField("FL_CALC").value = 450;

}

This does not contain any error checking (e.g. what happens if the field "STATE" or "FL_CALC" does not exist?), so you may want to look into some exception handling.

The code as I've described it will work from e.g. a global calculation script. If you want to do a calculation script, you will need to set the return value for the event handler: You want to calculate FL_CALC based on the "STATE" field, so you will need to create a custom caulcation script for the field FL_CALC. There are more elegant ways to do this, but to keep it easy to understand, I would recommend you use something like  this:

if (this.getField("STATE").value == "FL") {

   event.value = 145;

}

else {

  event.value = 450;

}

Participating Frequently
May 10, 2019

Thanks Karl... I will give a try tomorrow when I be back to my office.... thanks for taking time to answer me....

try67
Community Expert
try67Community ExpertCorrect answer
Community Expert
May 10, 2019

Sure. Use this code as the custom calculation script of "FL_CALC":

event.value = (this.getField("STATE").valueAsString=="FL") ? "145" :  "450";

Participating Frequently
May 11, 2019

Ok, first at all... THANK YOU !!!...works perfect, but....

as you can see in the above pict. the total field created is always shown the value $ 450, (because we set the custom script for "FL_CALC" field in my first question). As we set FL = 145 and every else = 450, this rule include the "--- Select State ---" option as well, but there is a chance to exclude this first option and to set FL = 145, else = 450, and --- Select State --- = 0 ?? everything in the same custom calculation script ?... I prefer that every time that open this PDF or push the CLEAR button, value shown be zero (0), since is the total result for a quote, and doesn't looks good even before to start filling up the form that $ 450 appears and you don't know from where... I been trying but I mess up everything with not result at all.

Thanks again in advance.

try67
Community Expert
Community Expert
May 13, 2019

Hello, I have finished my form and thanks to you it works well ... but I have 2 problems now:


1- It would be a great idea if I can add my form to my Company's website, I thought it would be as easy as I did with our product catalog (PDF too), but when I added the link to the form, it is shown as a plain PDF, not as a form, and none of buttons or fields work ... it is difficult to achieve this, add as PDF form to complete online?

2- On the other hand and assuming that option mentioned before is possible: I added a "send by email" button and it is working, but I receive the attached file as a PDF form, BUT because I set the "QUOTE No" field as a generated number automatically DATE + TIME when I try to open the attachment, this number changes automatically ... so, is there any way to send it as a plain PDF when I press the "send by email" button? ... I also try adding another button: "save as pdf" and the result is the same = you can save it, but as a form and then when you open it again, the "QUOTE No" will change one more time ... at the end It's not a big deal, I can eliminate the "QUOTE No." field and that's it, but I would like to have a reference besides the name of the clients.

I really appreciate any help, this is an prt-sc of my form:


1. That usually happens because the file is opened using the browser's internal PDF plugin, and most of those plugins are sub-part, at best. Instruct your users to not open the file in the browser, but to save it locally and then open it using Adobe Reader. It's a bit of a hassle, but it will work much better.

2. To do that you would need to define some condition as to when to update the field, and when not.

For example, you can include a (hidden) check-box in the file and use the script that submits it to tick it before sending, and then adjust the code that populates the field with the time-stamp to only do so if that field is not ticked.