Skip to main content
Participant
April 3, 2019
質問

Change color of part of text field.

  • April 3, 2019
  • 返信数 1.
  • 1183 ビュー

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!

このトピックへの返信は締め切られました。

返信数 1

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

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

}

This is where I became completely lost. I am very novice when it comes to this stuff. I have almost no experience with spans or objects. What do you mean when you say to build a list of locations? and the if statement at the end I don't understand.


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. 

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