Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
Bedrock Mission!

Learn more

View all

Sign in to view all badges

SOLVED

Editing text field that supports rich text using script

Avatar

Level 2

I have a text field with rich text format. I need to add a new line character if the length of a sentence exceeds 65 characters. When I edit the text value using javascript the formating breaks and the signature event fails eventually. Is there a way to edit the body of the text field value? I realized that the value is store in text/html.

below is sample:

Before edit:

<txtOtherDocuments><body xmlns="http://www.w3.org/1999/xhtml" xfa:APIVersion="Acroform:2.7.0.0" xfa:spec="2.1">

     <p style="margin-top:0pt;margin-bottom:0pt;font-family:Arial;font-size:8 pt;text-decoration:none">Test Data</p></body>

Using field.rawValue="...." fails. Is there a different set of commands?

Please help.

Thanks

1 Accepted Solution

Avatar

Correct answer by
Level 10

If you want to access the data and its styling in a richtext field you have to use the command Fieldname.value.exData.saveXML("pretty"). This will give you the xHTML format of the data. Then you can manipulate the data the way you wish and to get it back into the field you have to use the Fieldname.value.exData.loadXML("xml to load", parm1, parm2). The parms are boolean values that will control whether to replace or append the xml to the existing xml. Refer to the Help/Scripting reference for a more detailed description.

Note that this will not be easy if there is lots of rich Text in your field as the text will be spread out between tags. You may have to use the rawValue (no formatting to know where to put the CR then find it in the HTML and add it there.

Make sense?

Paul

View solution in original post

8 Replies

Avatar

Correct answer by
Level 10

If you want to access the data and its styling in a richtext field you have to use the command Fieldname.value.exData.saveXML("pretty"). This will give you the xHTML format of the data. Then you can manipulate the data the way you wish and to get it back into the field you have to use the Fieldname.value.exData.loadXML("xml to load", parm1, parm2). The parms are boolean values that will control whether to replace or append the xml to the existing xml. Refer to the Help/Scripting reference for a more detailed description.

Note that this will not be easy if there is lots of rich Text in your field as the text will be spread out between tags. You may have to use the rawValue (no formatting to know where to put the CR then find it in the HTML and add it there.

Make sense?

Paul

Avatar

Level 2

Thanks Paul. Having the field set to accept Rich Text, doesn't allow control of font in a consistent manner. I switched back to Plain Text.

Could you please tell me how to add a new line in a Plain Text field? I did an export of the form and found that &#xD; is used as the new line character.

When I add a new line character, the text appears as is (dave&#xD;rita, instead of dave in one line and rita in the next line). I guess the characters are not recognized.

Here's what I'm doing:

var str = field1.rawValue;

//logic to check if the length is more that 65 characters. This result is an array with strings less than 65 characters

//finally I join the strings

field1.rawValue = lines.join("&#xD;");

I tried to escape characters and such but no luck.

Thanks,

RT

Avatar

Level 10

\n is the newline char in javascript

Paul

Avatar

Level 2

Hi Paul,

  However, the following doesn't work. I'm not able to split based on the new line character. Please help.

var value = field1.rawValue;

var lines = value.split("\n");

Thanks,

Ram

Avatar

Level 10

Hi Ram,

I think you want the carriage return character.

Try;

var lines = value.split("\r");

which is the same as;

var lines = value.split("\x0D");

Bruce

Avatar

Level 10

Works fine for me ......I did not use the var name value .....that is a reserved word .....try changing it to Fvalue instead and see if it works for you.

Paul

Avatar

Level 10

Hi Paul,

Both the new line and carriage return will cause the text to be displayed on multiple lines in Designer.

But it is only the carriage return which is escaped as &#xD; in the XML.

The new line (with a character code of 10) does not need to be escaped in the XML and in the xml view I use I get the value broken over two lines.

Regards

Bruce