Skip to main content
Xenocity
Participant
December 12, 2017
Answered

Bold text dynamically in a text form field

  • December 12, 2017
  • 1 reply
  • 8875 views

I've created a form with a text field, lets call it Inbd_Data.

The text field pulls data from another source and populates it (i.e. the data is dynamic).

Example:

Name: Bob|Address: 234 Apple St.|Ste 230|

(yes, the data is pipe delimited and the field headers will always be the same [that's what I'm trying to bold])

I've been trying to write a script that will search the field for certain words and bold them. (i.e. "Name:" and "Address:" would be bold if found in the data).

I've seen people suggesting the use of spans but that doesn't seem to work for dynamic data. Any ideas?

This topic has been closed for replies.
Correct answer Thom Parker

Rich Text Spans are what you need. But they have to be defined dynamically. 

I don't know how you are applying the data to the field. But lets say it's being applied by a script not on the field itself.

var theTextData = ... where ever it comes from ...

var aPieces = theTextData.split("|");  // Split on pipes

// Set all to spans

var aSpans = [];

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

{

   if(i)  aSpans.push({text:"|"});

  aSpans.push({text:aPieces});

}

// Set second bit to bold

aSpans[1].fontWeight = 800;

// Set rich text to field

this.getField("Inbd_Data").richValue = aSpans;

Be sure to set the field option to Allow Rich Text Formatting

1 reply

Thom Parker
Community Expert
Thom ParkerCommunity ExpertCorrect answer
Community Expert
December 12, 2017

Rich Text Spans are what you need. But they have to be defined dynamically. 

I don't know how you are applying the data to the field. But lets say it's being applied by a script not on the field itself.

var theTextData = ... where ever it comes from ...

var aPieces = theTextData.split("|");  // Split on pipes

// Set all to spans

var aSpans = [];

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

{

   if(i)  aSpans.push({text:"|"});

  aSpans.push({text:aPieces});

}

// Set second bit to bold

aSpans[1].fontWeight = 800;

// Set rich text to field

this.getField("Inbd_Data").richValue = aSpans;

Be sure to set the field option to Allow Rich Text Formatting

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
Xenocity
XenocityAuthor
Participant
December 12, 2017

I still can't quite get it to work.

I have a few follow-up questions and I guess more information that will maybe help you answer my question.

This is a unique scenario where my PDF form is uploaded into a software program that recognizes textbox field names, so I name my fields specifically (i.e. this field is called "Inbd_Data"). This program interacts with another software program using an API (I assume) to pull the data into the textbox field I created called "Inbd_Data". Unfortunately, I can't see how the data gets pulled into the field but as soon as I open the document in the program, the data is pulled and my java script manipulates what is contained in the field.

Currently, I have a simple custom calculation script running on the field:

event.value = getField("Inbd_Data").valueAsString.replace("|","\n");

This is simply replacing the pipe delimiters with a paragraph break. Anyways, I removed this script and tried adding your suggestion to get the bolding to work:

var theTextData = this.getField("Inbd_Data");

var aPieces = theTextData.split("|");  // Split on pipes

// Set all to spans

var aSpans = [];

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

{

   if(i)  aSpans.push({text:"|"});

  aSpans.push({text:aPieces});

}

// Set second bit to bold

aSpans[1].fontWeight = 800;

// Set rich text to field

this.getField("Inbd_Data").richValue = aSpans;

________________________________________________

Nothing occurs when I add this. Interestingly, if I change:

var theTextData = this.getField("Inbd_Data"); to

var theTextData = this.getField("Inbd_Data").valueAsString; then everything is removed from the field.

Thanks for your help/patience.

Thom Parker
Community Expert
Community Expert
December 12, 2017

The first line of code returns the field object, not the value, so your correction at the bottom of the message is correct.

Did you set the text box properties to allow Rich Text?

The code I provided sets the "richValue" property of the field to an array of spans.  This is the only way to make a section of the text bold.

But I noticed you say that:  "as soon as I open the document in the program".  Do you mean that after the processing that fills in the form fields, you open the PDF in Acrobat? Or in another PDF viewer?

The whole spans thing is Acrobat Specific.  Other PDF viewers are unlikely to support this script.

BTW: are there any errors displayed when you run the code?

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