Copy link to clipboard
Copied
Hi: I am trying to figure out a way to underline only part of a "string" (if i'm using that correctly) I have a paragraph that auto populates based on a custom calculation script.
I used the follwoing in the script in the custom validation script section wich underlines the whole paragraph. I would like to only underline the first two words of the paragraph.
var span1 = {};
span1.text = event.value;
span1.underline = true;
event.richValue = [span1];
Any suggestions or solutions. Thank you !
var span1 = {};
span1.text = event.value;
span1.underline = true;
var span2 = {};
span2.text = event.value;
span2.underline = false;
event.richValue = [span1,span2];
this works but its duplicating everything, so everything is underlined and then repeated not underlined
Copy link to clipboard
Copied
Use 2 spans. One for the first two words. The other for the rest.
Copy link to clipboard
Copied
How would that script look written out ?
Copy link to clipboard
Copied
You can see a sample in the Acrobat Javascript Reference.
Copy link to clipboard
Copied
It didn't work ... this is my current script in the custom calulation script
var a = this.getField("options").value;
if(a == "a"){
event.value = "model a underlined title ..... model a paragraph....";
}
else if(a == "b"){
event.value = "model b paragraph....";
}
in the custom validation I have
var span1 = {};
span1.text = event.value;
span1.underline = true;
event.richValue = [span1];
what do I do to get just the first few words underlined ?
Copy link to clipboard
Copied
Create a separate span for just those two words.
Copy link to clipboard
Copied
How do I do that ?
var span1 = {};
span1.text = event.value;
span1.underline = true;
event.richValue = [span1];
var span2 = {};
span2.text = event.value;
span2.underline = false;
event.richValue = [span2];
I dont know if this is right ? but what would the custom calulation script look like then ?
Copy link to clipboard
Copied
No, that's not right. You return only the second span, because setting event.richValue replaces it, not adds to it. You have to generate an erray and return the array. Look at the example under the superscript property, to give an idea of how it is done.
Copy link to clipboard
Copied
var f = this.getField("myRichField");
// Create an array to hold the Span objects
var spans = new Array();
// Each Span object is an object, so we must create one
spans[0] = new Object();
spans[0].alignment = "center";
spans[0].text = "The answer is x";
spans[1] = new Object();
spans[1].text = "2/3";
spans[1].superscript = true;
spans[2] = new Object();
spans[2].superscript = false;
spans[2].text = ". ";
spans[3] = new Object();
spans[3].underline = true;
spans[3].text = "Did you get it right?";
spans[3].fontStyle = "italic";
spans[3].textColor = color.red;
// Now assign our array of Span objects to the field using
// field.richValue
f.richValue = spans;
this is the example and I do not understand it ... how do I dictate that just those two wrods should be underlined in the custome validation script field ?
Copy link to clipboard
Copied
event.richValue = [span1,span2];
That's what arrays are for, making lists of things 😉
Copy link to clipboard
Copied
var span1 = {};
span1.text = event.value;
span1.underline = true;
var span2 = {};
span2.text = event.value;
span2.underline = false;
event.richValue = [span1,span2];
this works but its duplicating everything, so everything is underlined and then repeated not underlined
Copy link to clipboard
Copied
It's duplicating everything yes. Please carefully read your code and study the effect of each line in it: copying things you don't understand can never work. You set span1.text to event.value (all the text) and then you set span2.text to event.value (all the text again). You need to actually split event.value up into two parts, and set each part in a different span. You need to study JavaScript string processing to do this.