Skip to main content
Inspiring
May 24, 2020
Answered

Conditional formatting of numbers only in event value script (PDF forms)

  • May 24, 2020
  • 2 replies
  • 1937 views

Running this script in a custom calculation in a text field:

 

event.value = "Words" + this.getField("Number").valueAsString;

 

Would like the value for Number to come out as bold but not the "Words" portion.

If it helps the "Number" is always a numeric value so don't need to get it as a string.

 

Was wondering if that could be used to have it show as bold, that is, can the value type be used in a cusotm format script to make only numbers bold?

 

Thanks all for your help.

 

(P.S. thought asked this question last night, different wording, but is was 3am so maybe not) 

This topic has been closed for replies.
Correct answer XML1979

Answered the additional question  (thanks to the Adobe SDK document)

the command is ".textSize" not ".fontSize"  works great

now just to move the big text from the center to the bottom of the field

 

2 replies

XML1979Author
Inspiring
May 24, 2020

Another question, getting greedy I know,

 

Is it also possible to change the font size for spans[1]?

I tried mimmicing the code with the command 

spans[1].fontSize = 16; 

after the bold command but to no effect and it removes the bold.  Default font size for this field is 9pt in Times New Roman.

XML1979AuthorCorrect answer
Inspiring
May 24, 2020

Answered the additional question  (thanks to the Adobe SDK document)

the command is ".textSize" not ".fontSize"  works great

now just to move the big text from the center to the bottom of the field

 

Old_Salt
Participating Frequently
May 24, 2020

I know it will align to the top if you check the "Multi-line" "Allow Rich Text Formatting" checkboxes and uncheck the "Scroll long text" checkbox. Too bad there isn't a way in the properties to align to the bottom.

 

From an Adobe professional:

Although a solution has been provided for you, the fact is that Acrobat is absolutely not a layout program or word processor. The text features within Acrobat are primarily for after-the-fact text touch-up / edit. For what you are attempting, Adobe InDesign or Adobe Illustrator or even Microsoft Word would be a much better solution. And from each of those programs you can save/export PDF.

 

          - Dov

- Dov Isaacs, Principal Scientist, Adobe
 
Seems there should be a way to do it with javascript so that it defaults to bottom alignment but I need to research that.
Old_Salt
Participating Frequently
May 24, 2020

Try something like this:

Place the conditional script in the number field and have it output to the text field. You'll need to use an array() to get the conditional formatting you want.

I created a number field formatted as a number and created a text field with no formatting. In the number field I added this custom calculation script:

var v = this.getField("Number1").valueAsString;

var spans = new Array();

spans[0] = new Object();
spans[0].text = "Words: ";
spans[0].fontWeight = 400; // normal weight

spans[1] = new Object();
spans[1].text = v; // insert the value we read from the field
spans[1].fontWeight = 800; // bold

var f = this.getField("Text16");
f.richText = true; // make sure we are dealing with a rich text field
f.richValue = spans;

 

The array gets written to the text field as richtext.

 

Hope this helps.

 

XML1979Author
Inspiring
May 24, 2020

Wow!  That works all right, with a caveat/detail.

The field "Number1" had a simplified field notation calculation in it, if I try to move this calculation up above the script you provided then I get a format error in the value of that field.  If I turn off the number format and just leave it as "none" it gets worse.

 

Of course never testing code for the first time where there is a possibility for circular referncing, I made a new field, called Niumber2 and use in the custom claculation field of Number2 this

var v = this.getField("Number1").valueAsString;

plus the other parts of the script...and it works beautifully!  Thank You!

 

I have no problem with making "dummy" fields.  I actually prefer them, it is clunky but helps with debugging. 

 

One weird thing is the value in the "dummy" field Number2 never updates even though all works well in the output to Tex16...that is instance updates when Number1 changes.  Any reason why and should I worry about Number2 not updating?