8 Replies Latest reply on Feb 7, 2017 12:24 PM by MarkWalsh

    Setting RichValue with multiple line text

    MarkWalsh Level 4

      I need to set/replace some values into a richText field, and I'm having a problem with the line endings.

       

      I am able to set the text, looping through the span objects, but I end up with a single paragraph, all of the line endings are gone.

       

      Even just setting the value to itself, without the code to do the text replacements, doesn't work:

       

      var spans = event.richValue;

      event.richValue = spans;

       

      or

       

      event.richValue = event.richValue;

       

      I end up with the correct content, but all of the line endings are missing. And this is code that I modified from the javascript reference documentation. The code is in the Custom Format Script for the field.

       

       

      Can someone help me with an example of how to replace text (i.e replace '[NAME]' with 'John') in a richtext field and retain the line endings?

        • 1. Re: Setting RichValue with multiple line text
          MarkWalsh Level 4

          Ok, I've made some (debatable) progress: while looping through the spans, I can add a newline at the end of each span and that will fix the paragraph issues; but then it splits the paragraph if there are multiple spans within that paragraph (i.e. a section that is bold within the paragraph).

           

          So, unless there's a better/more proper way to keep the line endings,  is there a way to tell if the current span is the end of the paragraph so that I can add the newline only to those spans? I'm guessing I can keep track of the character count as I loop through the spans, and check the character at the next position in the total text.

           

          If anyone has any better suggestions, I'd be very thankful to hear them.

          • 2. Re: Setting RichValue with multiple line text
            Karl Heinz Kremer Adobe Community Professional

            You need to use the "endParagraph" property of the span object to determine if you need to add a "\r" at the end or not. See this Stack Overflow question for some sample code:

             

            javascript - Linespacing in multiline fields - Stack Overflow

            • 3. Re: Setting RichValue with multiple line text
              MarkWalsh Level 4

              Thanks, I'll take a look at that, looks like exactly what I need. My solution did work, but is a bit cumbersome if it's unnecessary.

              There's no mention of an 'endParagraph' property in the scripting reference file that I have, not sure why it isn't there if it works.

               

              Thanks.

              • 4. Re: Setting RichValue with multiple line text
                Karl Heinz Kremer Adobe Community Professional

                The Javascript API documentation is not perfect, and sometimes you need to reverse engineer things a bit. In this case, you can take your "spans" information and convert it to a JavaScript code and printing it to the console using this:

                 

                console.println(spans.toSource());
                

                 

                You will then see all the properties of a span object.

                1 person found this helpful
                • 5. Re: Setting RichValue with multiple line text
                  MarkWalsh Level 4

                  Thanks, that's very helpful!

                   

                  Is there a way to use this to actually build the richtext for a field? I have several forms which I need to build in the calculate script, but I'm having problems with spacing and certain characters.

                   

                  I can paste formatted text from Microsoft Word, and the text formatting mostly come in properly (indented bullets, line spacing before) but when I try to build the formatted text, I get characters which are not correct (e.g.bullets, typographer's quotes), and I can now see the values for those using your suggestion, so I think I can get those working.


                  But, I also have the paragraph line spacing (e.g. 3 pt before each paragraph) and the indents (bullet paragraphs indented 18 pts with first line -18 points and a tab position at 18 pt) that I don't see any information in the span properties.

                   

                  Are these not accessible via JavaScript, and thus not possible to recreate in code what I can create by copy/paste?

                  • 6. Re: Setting RichValue with multiple line text
                    try67 MVP & Adobe Community Professional

                    You can insert special characters using their unicode values, but you can't set things like the line spacing or the indentation.

                    1 person found this helpful
                    • 7. Re: Setting RichValue with multiple line text
                      Joel_Geraci Adobe Community Professional

                      You can't programmatically "paste" into a rich text value. I've gone down this path multiple times. The Acrobat Properties bar and pasted rich text can do way more formatting than the span object's properties. If you want to set the rich value programmatically, those are the styles you're stuck with.

                      1 person found this helpful
                      • 8. Re: Setting RichValue with multiple line text
                        MarkWalsh Level 4

                        Thanks, I figured the character codes out (String.fromCharCode(2022) for bullets).

                         

                        That kinda sucks that I can't set the spacing the way I need to via code to match what can be done via copy/paste.