Skip to main content
Xenocity
Participant
December 12, 2017
Answered

Bold text dynamically in a text form field

  • December 12, 2017
  • 1 reply
  • 8925 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.

Xenocity
XenocityAuthor
Participant
December 13, 2017

You are correct about the more general solution, use separate edit boxes. 

I didn't mention before that I think using a calculation script is a bad idea. In fact, for initial development you should do this from the console window without any scripts on the PDF at all. The calc script runs so often that it may be screwing things up.

Then the script that you eventually use should be executed from a document script. and be setup in such a way that it only runs the first time after the form is loaded from your external application.

So let me repeat, Don't do this with a calculation script


I'm still a bit "green" with acrobat and javascript so this is really helpful. I opened up the console and ran the code there and I can see how that can be helpful.

I also noticed the console picked up the script running a number of times (i.e. now I can see what you mean with the calculation script).

For Document level Javascripts, do you have a good link to understand this better? I'm not sure exactly how to set this up, as I typically have run scripts off action items (mouse up, etc.). Do I create a function and then call it on document load...

I have much to learn..