Setting a form field to auto fill based on another field

New Here ,
Sep 29, 2017 Sep 29, 2017

Copy link to clipboard

Copied

First of all i'm new to the calculations in Acrobat but i'm trying to figure out two things

First is i'm trying to Field A and Field B to equal field C

example Field A = Hello Field B=2014 so Field C would equal Hello2014

2nd I have a topaz signature pad that i capture signatures with and i would love to have a script that will auto save the file base on what is in Field C from above after the signature was captured or even open a save as box

TOPICS
PDF forms

Views

3.2K

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Sep 30, 2017 Sep 30, 2017

1. Use this code as the custom calculation script of Field C:

event.value = this.getField("Field A").valueAsString + this.getField("Field B").valueAsString;

2. That's more complicated. To save the file without user interaction requires installing a script on the local machine.

See: https://acrobatusers.com/tutorials/how-save-pdf-acrobat-javascript

The alternative is to present the user with the desired file name, and then open that Save As dialog and hope they use it.

Likes

Translate

Translate
Community Expert ,
Sep 30, 2017 Sep 30, 2017

Copy link to clipboard

Copied

1. Use this code as the custom calculation script of Field C:

event.value = this.getField("Field A").valueAsString + this.getField("Field B").valueAsString;

2. That's more complicated. To save the file without user interaction requires installing a script on the local machine.

See: https://acrobatusers.com/tutorials/how-save-pdf-acrobat-javascript

The alternative is to present the user with the desired file name, and then open that Save As dialog and hope they use it.

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Nov 02, 2021 Nov 02, 2021

Copy link to clipboard

Copied

Hi, I've been looking throughout the community support for javascript to help me with my issue.  Not sure it aligns to this but hope you can help.

I have a form which I have automatically calculating a total based on a series of check box values.  I would like to use that total to then display text in a text box below based on variables of  greater than or less than.  Looking at other threads I have written this script but nothing is displaying in the text field.  Is there something I am missing?

var v = this.getField("C1TotalScore").valueAsString;
if (v=="") event.value = "";
else {
var score = Number(v);
if (score<15) event.value = "Sample Text 1";
else if (score>=16 && score<=32) event.value = "Sample text 2";
else if (score>=33) event.value = "Sample text 3";
}

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 03, 2021 Nov 03, 2021

Copy link to clipboard

Copied

The code looks fine. Check the JS Console for error messages, though.

If no error messages appear share the file for further help.

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Nov 03, 2021 Nov 03, 2021

Copy link to clipboard

Copied

Hi @try67 & @Thom Parker

Thanks for helping out. 

I have to say I am very green at this Javascript thing and while I have tried to follow your guidance I have still not been able to resolve the issue. 

 

As suggested I've uploaded the document now hoping one of you might be able to take a look at it for me.  The fields I am working on are on page 3 of the document "Text14.0", where I would like the text to display and the "C1TotalScore" field which determines which text to display.

 

Appreciate your assistance.

Regards

MFHawk

 

 

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 03, 2021 Nov 03, 2021

Copy link to clipboard

Copied

Use script as 'custom calculation script'.

If you want to use it as validation, move script to "C1TotalScore" field and make changes to make it work in new field.

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Nov 04, 2021 Nov 04, 2021

Copy link to clipboard

Copied

Brilliant - thank you.  Knew something wasn't right, have done the odd javascript before and it has worked and thought it looked wrong in the validation section.  completely missed the Custom Calculation section for javascript.  All working now.  Thanks a bunch 🙂

 

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 03, 2021 Nov 03, 2021

Copy link to clipboard

Copied

The code looks good, but there are a couple of things to check.

1.  Field names must be verbatim.  

2. It's possible that  Number(v) is returning a NaN. So the if block would return nothing.

   Consider adding an else to the end of the inside if, that sets event.value to something like "Unknown Value" 

 

 

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jun 02, 2022 Jun 02, 2022

Copy link to clipboard

Copied

I have a similar situation, but I want to incorporate formating and what happens if a field is left blank.
I'm creating a business card template that the person fills in fields "Name", "Pronouns", "Designation_1" and "Designation_2"

I have a read only field that will populate from the other fields in the same order. The designation fields are drop downs with the first option being "-"

 

I have the script written as: 

//Populate text from fields
event.value = this.getField("Name").valueAsString + " " + this.getField("Pronouns").valueAsString + " " + this.getField("Designation_1").valueAsString + ", " + this.getField("Designation_2").valueAsString;

 

It's missing some things. I want the designation to not appear at all if the person selects the "-" option. As well I don't want the comma to appear between the designations unless anything either than "-" is selected in "Designation_2".

 

The designations also need to be formated to 5pt where the rest of the text in the field is 8pt.

 

Can anyone assist me with this?

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jun 02, 2022 Jun 02, 2022

Copy link to clipboard

Copied

Doing that will require a more complex script, especially the last part. You will need to set the field as having Rich Text Formatting and convert its value to an array of Span objects, each one with a part of the text and its own formatting, to achieve that.

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jun 02, 2022 Jun 02, 2022

Copy link to clipboard

Copied

I figured out the more complex script:

 

var Name = this.getField("Name").valueAsString;
var Pronouns = this.getField("Pronouns").valueAsString;
var Designation_1 = this.getField("Designation_1").valueAsString;;
var Designation_2 = this.getField("Designation_2").valueAsString;
var fullString = "";

 

if (Name !== "") {
fullString = Name;
if (Pronouns !== "") {
fullString = fullString + " (" + Pronouns + ")";
}
if (Designation_1 !== "-"){
fullString = fullString + " " + Designation_1
if (Designation_2 !== "-"){
fullString = fullString + ", " + Designation_2
}
}


}
event.value = fullString;

 

I've turned on Rich Text Formatting and I found your code from an answer back in 2017 regarding changing the colors of vowels to red: 

var newValue = event.value;

var letters = newValue.split("");

var newRichValue = [];

for (var i in letters) {

var span = {};

span.text = letters;

span.textColor = isVowel(letters) ? color.red : color.black;

newRichValue.push(span);

}

event.richValue = newRichValue;

function isVowel(s) {

return /^[aeiou]$/i.test(s);

}

 

I'm so close to getting this but I'm stuck on how to change the font sizes and apply it to the different fields. I need "Name" and "Pronouns" to be 8. I Need "Designation_1" and "Designation_2" to be 5.

 

getField("Name").textSize = 8;
getField("Pronouns").textSize = 8;
getField("Designation_1").textSize = 5;
getField("Designation_2").textSize = 5;

 

Any assistance would be appreciated!

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jun 03, 2022 Jun 03, 2022

Copy link to clipboard

Copied

You want to edit the text in those fields, or the text copied from them and used in the single, combined string?

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jun 06, 2022 Jun 06, 2022

Copy link to clipboard

Copied

The text is copied from those fields and used in a single combined string.

Fields Name, Pronouns, Designation_1 and Desination_2 are all editable but the field that contains the above script (NameCombined) is read only. 

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jun 06, 2022 Jun 06, 2022

Copy link to clipboard

Copied

I can see how my initial message could be confusing. The font sizes need to be different within the string not the fields.

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jun 06, 2022 Jun 06, 2022

Copy link to clipboard

Copied

I am open to another solution if one exists.

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jun 09, 2022 Jun 09, 2022

Copy link to clipboard

Copied

Should I start a new conversation regarding this?

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jun 09, 2022 Jun 09, 2022

Copy link to clipboard

Copied

No, it's fine here. Try this code:

 

 

var Name = this.getField("Name").valueAsString;
var Pronouns = this.getField("Pronouns").valueAsString;
var Designation_1 = this.getField("Designation_1").valueAsString;
var Designation_2 = this.getField("Designation_2").valueAsString;
 
var spans = [];
if (Name !== "") {
	spans.push({text: Name, textSize: 8});
}
if (Pronouns !== "") {
	if (spans.length>0) Pronouns = " (" + Pronouns + ")";
	else Pronouns = "(" + Pronouns + ")";
	spans.push({text: Pronouns, textSize: 8});
}
if (Designation_1 !== "-"){
	if (spans.length>0) Designation_1 = " " + Designation_1;
	spans.push({text: Designation_1, textSize: 5});
}
if (Designation_2 !== "-"){
	if (spans.length>0) Designation_2 = " " + Designation_2;
	spans.push({text: Designation_2, textSize: 5});
}
event.richValue = spans;

 

 

In order for it to work you have to first tick the Rich Text Formatting option under the field's Properties, though.

 

Edit: Fixed error in code

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jun 09, 2022 Jun 09, 2022

Copy link to clipboard

Copied

LATEST

Very good! Except it was missing the closing "}" and a comma between the designations. I was able to make those small corrections and it works beautifully! Thank you!

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Apr 02, 2020 Apr 02, 2020

Copy link to clipboard

Copied

try67 - You are brilliant!! - This custom calculation allows the developer to set alternative text formatting such as beeing bold an in a smaller font (set in the property Dialog box). If you simply duplicate the text field then it takes on the text formating of the parent field. I know that you know this, however, other newbies might not so this is a great solution along with concatenating fields together. Thank you again! 🙂 

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines