Skip to main content
Participant
April 3, 2019
Question

Change color of part of text field.

  • April 3, 2019
  • 1 reply
  • 1203 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 5, 2019

The code seems to have no more errors should this be a calculation script or validation? I've placed it in the text field as a calculation along with my current script but it isnt working.


I wrote the initial code as a generic script script.  In it's current form it won't work as either a calculation or validation because the first 3 lines acquire the values directly from the field.

A validation script is a good place to put it, so that its a separate modification to the entered or calculated text. To do this the value needs to be acquired through the event object. Here's the code.

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

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

var aSpans=[], oSpn;

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

{

  oSpn = eval(cMasterSpan);

  oSpn.text = aWords + " ";

  if(aWords == "AND" || aWords == "OR")

      oSpn.textColor  = color.red;

  aSpans.push(oSpn);

}

event.richValue = aSpans;

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