Change color of certain words in a text field
New here? Learn about Bountify and follow @bountify to get notified of new bounties! x

I am working on a PDF document. I use calculation scripts to generate what the user's next step in a process is depending upon the selections they make in various fields on the document. Those calculation scripts are running fine however I wanted to add an additional element to their look. I have the text color for the field set to blue, but I would like to be able to change certain key words in the generated message to red and possible underline the key words as well. For instance:

MTH 35 - Intermediate Algebra AND MTH 123 - College Algebra and Trigonometry

The entire line would be blue, I would want the word AND to be turned red and underlined. The two key words I would want this to happen with is "AND" and "OR"

I don't know if this would be a calculation script or a validation script but if it is a calculation script I would need something that would work alongside the current calculation script that is in the text field.

Thanks! I can provide more info if needed.

What language do you use? Can you show some code ?
Rachid 3 months ago
Sorry about that, I use javascript being that it is run from within the adobe PDF document. The current codes i'm using in the calculation field are "else if" statements to check for variable inputs in other fields and then generating a response based on those inputs. If needed I can post up part of the code when I get to work in about an hour.
Santiman21 3 months ago
var f = this.getField("MathPlacement"); var g = this.getField("Major Text"); var h = this.getField("Pre-Professional"); var j = this.getField("MTHQ"); if (j.valueAsString=="On") event.value = ""; else if ((g.valueAsString=="Accounting BS") && (h.valueAsString=="") && (f.valueAsString=="Immersion"))event.value = "CUNYSTART, MATHStart, or Summer Immersion"; else if ((g.valueAsString=="Accounting BS") && (h.valueAsString=="") && (f.valueAsString=="MTH 20 or 104"))event.value = "MTH 20 - Elementary Algebra, 0 cr./4 eq. cr.";
Santiman21 3 months ago
awarded to mashtullah

Crowdsource coding tasks.

1 Solution

Winning solution

If your string is in a text field then you can only change the color of the whole textfield not part of it...


i wasnt thinking of spanning the textfield... but here you go...

var curString = this.getField("MTHQ");  
var words = curString.split(" ");  
var newString = [];  
for (var i in words) {  
    var span = {};  
    span.text = words[i];  
    span.textColor = words[i]==("AND"||"OR") ? :;  
event.value = newString.join(" ");  
it can be split into spans.
Santiman21 3 months ago
I have updated my answer with the javascript version, hope that works for you...
mashtullah 3 months ago
did you try the code?
mashtullah 2 months ago
@Santiman21 if there is any other issue you have with the solution let me know, but if the code worked for you, please award the bounty...
mashtullah 2 months ago