Skip to main content
skillful_knight15A8
New Participant
June 13, 2016
Answered

How to do an if then statement on a PDF form

  • June 13, 2016
  • 5 replies
  • 123046 views

I am trying to do an if then statement on a PDF form.

The field is a calculated field A*B=C.  If C<10 then C=10, else C=A*B

How do I write the is Adobe Pro on a PDF form?  I have gone into properties, calculated field and nothing I have tried works.

This topic has been closed for replies.
Correct answer gkaiseril

Each field is a separate object, so if you have 3 fields like "A", "B", and "C" each is a unique object has a unique value. Each field has to be accessed individually, You need to get the value for field "A" in one statement, the value of field "B" in another statement.

var C = this.getField( "A").value * this.getField("B").value;

if( C < 10 ) event.value = 10;

else event.value = C;

You might see it more clearly with some code like:

var A = this.getField( "A").value; // the value of field A;

var B = this.getField( "B").value; // the value of field B;

var C = A * B; // the value of field A times the value of field B;

if( C < 10 ) {

event.value = 10; // value is 10 when product less than 10;

} else [{

event.value = C; // else value is the product;

}

5 replies

Inspiring
June 24, 2020

Hey, sorry to bother with a different question.

I'm trying to make 2 boxes. If someone puts a number between 1-20 in the first box, another number appears in the second. So if they put a 4, they get a -2. If they put a 14, they get a 2. I need to be able to control the output number, because its not based on a math calculation. 

Would you know how to do this?

Thom Parker
Community Expert
June 24, 2020

The first thing to do is to write out the exact conditions that result in all the different outputs. A table is a good way to do this.  It needs to include all input conditions. Then you can write an "if" block using logical conditions to test the inputs and return the appropiate result. 

Did you read the article linked above? Do you have any programing experience?

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
New Participant
July 8, 2020

event.value=(7000+(this.getField("field.nam").value-500000)*0.025) but I want to add a if ("field.nam") == 0 or is less than 100,000 to leave value blank 

jenniferw2015
New Participant
March 13, 2020

I'm trying to do a fillable Order Form with tiered pricing on variable quantities. Not going to lie... I'm pretty good at figuring out simple code, but know nothing about JavaScript.

 

After reading through this thread, this is what I've come up with so far, but I keep getting a syntax error so I'm not even sure if this is right or not:

 

Total Item 1 = Var Quantity * Var Price

Var Quantity = this.getField(”Q1”).value; or this.getField(Q”2”).value; or this.getField(”Q3”).value;
or this.getField(”Q4”).value; or this.getField(”Q5”).value); or this.getField(”Q6”).value

Var Price = this.getField(”T1”).value; or this.getField(”T2”).value; or this.getField(”T3”).value;
or this.getField(”T4”).value; or this.getField(”T5”); or this.getField(”T6”).value

var quantity = this.getField("Q1").value;
var price = this.getField("Total Item 1").value;
event.rc = (T1=="")&&(Q1=="");
event.readonly = event.rc;
event.value = T1*Q1; else
var price=this.getField("TotalItem1").value;
var quantity = this.getField("Quantity").value;
event.rc = (T2=="")&&(Q2=="");
event.readonly = event.rc;
event.value = T2*Q2; else
var price = this.getField("TotalItem1").value;
var quantity = this.getField("Quantity").value;
event.rc = (T3=="")&&(Q3=="");
event.readonly = event.rc;
event.value = T3*Q3; else
var price = this.getField("TotalItem1").value;
var quantity = this.getField("Quantity").value;
event.rc = (T4=="")&&(Q4=="");
event.readonly = event.rc;
event.value = T4*Q4; else
var price = this.getField("TotalItem1").value;
var quantity = this.getField("Quantity").value;
event.rc = (T5=="")&&(Q5=="");
event.readonly = event.rc;
event.value = T5*Q5; else
var price = this.getField("TotalItem1").value;
var quantity = this.getField("Quantity").value;
event.rc = (T6=="")&&(Q6=="");
event.readonly = event.rc;
event.value = T6*Q6;

Thom Parker
Community Expert
March 13, 2020

It's not surprising you can't find the syntax error. This code is on the big messy size. I would suggest formatting it so it can be read. And also posting this to s new thread. 

 

There are a couple of obvious issues. You are missing the the "if" block brackets and the structure is incorrect. 

Before writing something this large, you should create a test with minimum code. That way you can figure out these types of issues in simpler, easier to handle setting.  

 

Here's an article on using the "if" statement:

https://www.acrobatusers.com/tutorials/conditional-execution/

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
jenniferw2015
New Participant
March 13, 2020

Thank you for your response, Mr. Parker.

Unfortunately, I do not know how to start a new thread.

I think the whole thing is a big ugly mess. Who would have thought that a simple thing could get so entirely complicated?

Basically, I have an order form that has tiered pricing on all items. I want the customer to fill in the Quantity and then I want the total to auto generate based on the quantity entered multiplied by the corresponding tier price. Quantities entered MUST match one of the tiers. In other words, if the customer wanted to order less than 500, then they have to get a custom quote. Same holds true for values in between tiers or over 5000.

 

 

New Participant
November 7, 2017

I have a similar if statement to this one.

but I cant manage it to work.

i have text field A,

the text field B would be dependent of the number to be input on field A.

if A <=12

field B = 1

if A >=13 && <= 24

field B = 2

if A>= 25 && <= 40

field B = 3

please help on what is the correct script for this.

i have use this script.

var B = this.getfield("A");

if (A <=12 && >=2)

B = 1;

if (A <=24 && >=13)

B = 2;

if (A <=36 && >=25)

B = 3;

try67
Community Expert
November 7, 2017

Why are you placing the value of field A in a variable called B? That's really confusing.

Use this code as the custom calculation script of the B field:

var A = Number(this.getField("A").value);

var B = "";

if (A<=12) B = 1;

if (A>=13 && A<=24) B = 2;

if (A>=25 && A<=40) B = 3; // Is it 36 or 40? What about if it's higher than 40?

event.value = B;

New Participant
November 7, 2017

it was 40.

if it exceeds 40, it would not accept. because i set the limit to 1-40 only.

thank you very much

Inspiring
June 13, 2016

Each field is a separate object, so if you have 3 fields like "A", "B", and "C" each is a unique object has a unique value. Each field has to be accessed individually, You need to get the value for field "A" in one statement, the value of field "B" in another statement.

var C = this.getField( "A").value * this.getField("B").value;

if( C < 10 ) event.value = 10;

else event.value = C;

You might see it more clearly with some code like:

var A = this.getField( "A").value; // the value of field A;

var B = this.getField( "B").value; // the value of field B;

var C = A * B; // the value of field A times the value of field B;

if( C < 10 ) {

event.value = 10; // value is 10 when product less than 10;

} else {

event.value = C; // else value is the product;

}

You appear to be confusing the parameter  "Value is the _____ of the following fields" and the "Simplified field notation" that are fed into some JavaScript functions that perform the calculation.

A simplified version could be of such a script could be:

function Product(cField1, cField2)

{

/*

purpose: return the product of the value for 2 fields

inputs: names of the two fields

returns: the product of the 2 named fields

*/

var nField1 = this.getField(cField1).value;

var nField2 = this.getField(cField2).value;

return nField1 * nField2;

}

var C = Product("A", "B");

if( C < 10 ) {

event.value = 10; // value is 10 when product less than 10;

} else {

event.value = C; // else value is the product;

}

Inspiring
December 18, 2018

I am trying to do something similar.

I have a field that needs to read either 100, 200 or 300 depending on a value entered in a previous field.

If someone enters 1 in "Field a" then "Field b" needs to read "$100"

If someone enters 2 in "Field a" then "Field b" needs to read "$200"

If someone enters 3 in "Field a" then "Field b" needs to read "$300"

Here was my attempt that I placed in the Custom Calculation Script area for "Field b" and did not work. Can anyone let me know what is wrong? Thank you.

var v1 = this.getField(“Fielda”).value;

if (v1 = 1) {

    event.value = “100”;

}

else if (v1 = 2) {

    event.value = “200”;

}

else if (v1 = 3) {

    event.value = “300”;

}

try67
Community Expert
December 18, 2018

A couple of things:

- Use "==" for the comparisons, not "=".

- Only use straight-quotes ("), not curly ones (“ ”).

- Convert the value of the first field to a number explicitly, like this:

var v1 = Number(this.getField("Fielda").value);

skillful_knight15A8
New Participant
June 13, 2016

I have very little experience in this.  I can get it to do the first half so it equals 10 but cannot get it to equal A*B.  Here is the formula.

var C = this.getField( "A*B").value;

if( C < 10 ) event.value = 10;

else event.value = C;

What am I missing?

gkaiserilCorrect answer
Inspiring
June 13, 2016

Each field is a separate object, so if you have 3 fields like "A", "B", and "C" each is a unique object has a unique value. Each field has to be accessed individually, You need to get the value for field "A" in one statement, the value of field "B" in another statement.

var C = this.getField( "A").value * this.getField("B").value;

if( C < 10 ) event.value = 10;

else event.value = C;

You might see it more clearly with some code like:

var A = this.getField( "A").value; // the value of field A;

var B = this.getField( "B").value; // the value of field B;

var C = A * B; // the value of field A times the value of field B;

if( C < 10 ) {

event.value = 10; // value is 10 when product less than 10;

} else [{

event.value = C; // else value is the product;

}