Skip to main content
Participant
April 3, 2019
Question

Change color of part of text field.

  • April 3, 2019
  • 1 reply
  • 1211 views

I am working with text boxes, I have already run calculation scripts to return a value based on other field selections. Those values are all being returned correctly, however based on the nature of the form some of the values returned include the word "and" while others may include the word "or" the text color for the entire field is currently blue but I would like to bring attention to those words "and"/"or" by either assigning them a different color or bolding them.

Example:

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

I want them to stand out from the rest of the text. I don't know if this is possible, I imagine it could be a validation script or something but I am not sure how best to set it up.

Any help would be great, I've tried reading documentation but I haven't found anything that similar to what I want to do.

Thanks in advance!

This topic has been closed for replies.

1 reply

Thom Parker
Community Expert
Community Expert
April 3, 2019

This can be done with Rich Text. 

Look up the "field.richValue" property and the "Spans" object in the Acrobat JavaScript Reference.

It's best to do this in the same calculation script that creates the text. But it could be done by a validation script.

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
Participant
April 3, 2019

This is an area I am largely unfamiliar with.

This is what I have:

//

var l = this.getField("MTH Courses");

var spans = l.richValue;for ( var i = 0; i < spans.length; i++ ){if( spans.text = " AND " ){spans.textColor = color.red;spans.underline = true;}}

l.richValue = spans;

//

The field that the text appears in is titled "MTH Courses" which is where I want the script to look. I then want the script to look for the word " AND " and change that word to red and underlined.

How far off am I? I'm embarrassed to know.

Thanks for the direction Thom.

Thom Parker
Community Expert
Community Expert
April 3, 2019

You're not close, but moving in the right direction.

If a the "field.value" property is set, then the field has one span.  You need to parse the text to find the "And" and "Or" bits, then put it all back together as spans.

Something like this:

var l = this.getField("MTH Courses");

var cMasterSpan = l.richValue[0].toSource();  // Get the JSON for the basic span

var aWords = l.value.split(" "); // This breaks the raw string into words

// Build a list of locations for the highlight words

var aIdxList = [];

for(var i=0;i<aWords.length;i++)

{

   if(/(and)|(or)/i.test(aWords)) aIdxList.push(i);

}

Now you have a list of words, a list of indexes into that list for each highlight word, and a master template for the spans.

Just walk the list of indexes to build a set of spans.

This code assumes a single line where the words are separated by spaces. I haven't tested the code, so you'll need to make sure there are no syntax errors.

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