Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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;
}
Copy link to clipboard
Copied
If you don't want to change the calculations you can show/hide the fields based on the selection in the radio-button group.
To do that you can use something like this as the MouseUp event of those fields:
this.getField("Field1").display = (event.target.value=="Canine") ? display.visible : display.hidden; // show this field if "Canine" is selected
this.getField("Field2").display = (event.target.value=="Canine") ? display.visible : display.hidden; // show this field if "Canine" is selected
this.getField("Field3").display = (event.target.value=="Canine") ? display.visible : display.hidden; // show this field if "Canine" is selected
this.getField("Field4").display = (event.target.value=="Feline") ? display.visible : display.hidden; // show this field if "Feline" is selected
this.getField("Field5").display = (event.target.value=="Feline") ? display.visible : display.hidden; // show this field if "Feline" is selected
It's not a great solution, but it should work...
Copy link to clipboard
Copied
do I apply these to the radio buttons Field or add this in for each calculated field?
Copy link to clipboard
Copied
To the radio-button fields (all of them!).
Copy link to clipboard
Copied
Thank you Try it seems that perhaps the radio button group is not running the JavaScript appropriately (because nothing is happening, maybe a conflict of the other calculations). It's still putting in field for Canine calculation even if I have selected a choice for Feline under the radial field. Perhaps I am entering the code above in the wrong. I did modify it to include my fields rather than 'Field1', etc.
If I were to modify my calculations what would that look like?
Thanks again for any help,
Copy link to clipboard
Copied
It might be easier to help you out if you could share the file with us (via Dropbox, Google Drive, Adobe Cloud, etc.)...
Copy link to clipboard
Copied
I wish I could but for confidentiality reasons I can't...I know it makes it tough...could I send over an example with just the portion cut out...I would be able to put in fake fields for reasons of privacy. Would that work and what's the address I could share it with? I have google drive.
Thank you
Copy link to clipboard
Copied
Yes, do that. Share it publicly and post the link to it on the forum.
Copy link to clipboard
Copied
Thank you Try, here's the link to the sample form. Please let me know if you can help.
SAMPLE PDF Form.pdf - Google Drive
Thank you so much
Copy link to clipboard
Copied
I'm not seeing where you placed the code I provided...
Copy link to clipboard
Copied
Ok thank you try, I have added in calculation on the species radio buttons. It seems as though it works, however I am running into a problem where if I clear the form the zero's from the previous choice stay the same and if the person only enters the BW field then it plugs in doses for the last saved species. Is there a way to make them hidden until a selection is made on species?
Here's the new link:
SAMPLE PDF Form.pdf - Google Drive
Thank you in advance
Copy link to clipboard
Copied
Did you ever figure this out? I'm having a similar issue.
Copy link to clipboard
Copied
Start a new thread with the full details of your question.
Copy link to clipboard
Copied
Any luck with this?
Copy link to clipboard
Copied
How to convert excel formula into pdf form
Copy link to clipboard
Copied
I am trying to do an IF statement on a PDF form.
The field is a calculated field on the properties: the ‘Simplified field notation’: BT*7.5 (there is two fields: BT field & Total field) , how do I do so if the field BT is less than 20, multiply by 7.5, but if BT is more than 21, I want to multiply by 5.
Here is my calculation on the Total field with values:
If BT<20 then *7.5, else BT *5
I even tried on ‘Simplified field notation’ and on ‘Custom Calculation Script’. What am I missing?
Copy link to clipboard
Copied
Can you help me create a javascript for an IF statement.
What I am trying to accomplish is this (these are numeric fields: IF FieldA > Field B, display the number in Field B to this field.
Your help would be greatly appreciated.
Copy link to clipboard
Copied
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;
}
Copy link to clipboard
Copied
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”;
}
Copy link to clipboard
Copied
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);
Copy link to clipboard
Copied
- Also, you should add a default value, in case none of the if-statements were true. Add this line to the end of your code:
else event.value = "";
Copy link to clipboard
Copied
WooHoo it works now. Thank you so much try67
Copy link to clipboard
Copied
Hi try67,
I am wondering if you could help me? I am starting out learning java coding and have been reading all posts and trying to get the script to work but have been getting syntax error 3 in the below script:
var f = this.getField("Amount inc GST Row1");
var g = Number(this.getField("GST CodeRow1").value);
if (var g = "GST") {event.value = (f.value / 11)};
else if (var g = "FREE") {event.value = "0"};
else if (var g = "NA") {event.value = "0";
}
Basically, I have 3 text options, GST, FREE and NA. When I select GST text in my dropdown, it correctly calculates GST from the amount in the "Amount inc GST Row1" field. However, if I select FREE or NA in the drop down in the PDF form, the amount does not change to $0.00, but stays as the amount / 11 as if there were GST on the amount.
I am not certain if the text is causing an issue or if it is the script.
I would be grateful if you could assist?
Thank you.
Copy link to clipboard
Copied
Use this:
var f = this.getField("Amount inc GST Row1");
var g = this.getField("GST CodeRow1").value;
if (g == "GST") event.value = f.value / 11;
else if (g == "FREE") event.value = 0;
else if (g == "NA") event.value = 0;
Copy link to clipboard
Copied
Thanks Bernd,
This worked!
Very helpful 🙂
Copy link to clipboard
Copied
You have several issues in your code, but Bernd's version of it should work fine.
It's important to understand the difference between the comparison operator ("==") and the value assignment operation ("=").
In some languages you can use the same operator for both, but not in JavaScript.
Find more inspiration, events, and resources on the new Adobe Community
Explore Now