• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

How to underline part of text Javascript

Explorer ,
Feb 11, 2020 Feb 11, 2020

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 ! 

 

 

TOPICS
Acrobat SDK and JavaScript

Views

2.5K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Explorer , Feb 11, 2020 Feb 11, 2020
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

Votes

Translate

Translate
Community Expert ,
Feb 11, 2020 Feb 11, 2020

Copy link to clipboard

Copied

Use 2 spans. One for the first two words. The other for the rest.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Feb 11, 2020 Feb 11, 2020

Copy link to clipboard

Copied

How would that script look written out ?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 11, 2020 Feb 11, 2020

Copy link to clipboard

Copied

You can see a sample in the Acrobat Javascript Reference.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Feb 11, 2020 Feb 11, 2020

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 ?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 11, 2020 Feb 11, 2020

Copy link to clipboard

Copied

Create a separate span for just those two words.

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Feb 11, 2020 Feb 11, 2020

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 ?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Feb 11, 2020 Feb 11, 2020

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Feb 11, 2020 Feb 11, 2020

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 ? 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 11, 2020 Feb 11, 2020

Copy link to clipboard

Copied

event.richValue = [span1,span2];

That's what arrays are for, making lists of things 😉

 

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Feb 11, 2020 Feb 11, 2020

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Feb 11, 2020 Feb 11, 2020

Copy link to clipboard

Copied

LATEST

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines