Skip to main content
Participant
April 3, 2019
Question

Change color of part of text field.

  • April 3, 2019
  • 1 reply
  • 1183 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.

Participant
April 4, 2019

This is a somewhat complex task.  It's not easy.

So the strategy I've outlined breaks the sentence into a list of words. The task is to identify the specific words that need to be highlighted. To do the identification I've used a regular expression. Here's an article on the topic.

https://acrobatusers.com/tutorials/text-matching-regular-expressions

I developed this strategy for efficiency. It results in the fewest number of spans. But here is a more straight forward method that makes every word a span

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

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);

}

l.richText = aSpans;

Again, I have not tested this code. 


Thanks for the extra help Thom. I understand most of the code, the spans still confuse me. There is a syntax error in the code somewhere around here I think:

//

var aSpans[], oSpn;

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

{

Do you know if there is somewhere I can maybe pay to have this task completed? I also want to have a save script built that I would talk about, but I don't know if services like that exist without paying through the nose. I don't have a lot to spend on it. Just trying to automate some of my work tasks.

Thanks!