6 Replies Latest reply on May 16, 2013 1:32 PM by cweger79

    [JS/CS4+] Best way to add formatted text?

    [Jongware] Most Valuable Participant

      What is the best way to add formatted text to a document?


      I'm using two different methods:


      1. Building a string of any length, including hard returns, in memory, and setting the contents of a Story immediately. Pro: pretty fast, even with (moderately) long strings. Con: you cannot specify any local formatting, only 'global' ones.


      2. Use the last InsertionPoint.contents to insert one text fragment at a time. Pro: each separate fragment can have its own formatting. Con: extremely slow -- as in: you can see the text appear one fragment at a time.


      I've done a few random experiments with stuff like "a = new Story", but ID doesn't seem to allow this for any type of formatted text at all ...


      I'm thinking perhaps an XML approach might work -- add tags and then remap these to character styles (or something -- I'd have to try), but only if inserting those XML tags doesn't make ID crawl as well. Another possibility would be to keep a stack in memory, recording where text attributes would need to be changed, then insert everything as plain text, and only then looping over the attribute stack to assig the formatting 'immediately' to the correct ranges. Sounds like a lot of paperwork, but it ought to work pretty fast. At least, theoretically.


      Other ideas?

        • 1. Re: [JS/CS4+] Best way to add formatted text?
          John Hawkinson Level 5

          Write to a file and place the file as InDesign Tagged Text?

          1 person found this helpful
          • 2. Re: [JS/CS4+] Best way to add formatted text?
            Harbs. Level 6

            I was going to suggest tagged text as well. Tagged text does seem to 

            be somewhat finicky though.


            Another option would be to write your own tags, and use S/R to apply 

            the formatting post-facto...



            1 person found this helpful
            • 3. Re: [JS/CS4+] Best way to add formatted text?
              [Jongware] Most Valuable Participant

              Thanks guys -- both options were hovering somewhere in the back of my mind as well.


              John's Tagged Text:

              Pro: put anything in there you want. Con: perhaps (somewhat) slow? It'll depend on how fast disk i/o is. I don't have much experience with using temp files -- under what circumstances can it fail? (Disk full? No write rights?)


              Harbs' S&R:

              Yup -- could be pretty fast, especially if I keep a list of "used" tags to search for (rather than plunking in any and all possible combinations -- my PrepText is performing quite poorly because of that!).


              Both options come down to outputting formatting codes right where I need them; Tagged text needs nothing else (well, it needs to be placed) and S&R requires one single replace loop for each of the codes I inserted. Thanks to Adobe for GREP! Removing tags and replacing them with formatting in one go!

              And for sure, both options are way, way easier to implement than my "keeping a list of formatting to apply", which does sound like it's the fastest way possible but needs quite a bit of careful coding.

              • 4. Re: [JS/CS4+] Best way to add formatted text?
                John Hawkinson Level 5

                Not to toot my own horn, but another advantage of Tagged Text is that it supports all the possible InDesign markup, and if Adobe adds something, you get it for free... 

                • 5. Re: [JS/CS4+] Best way to add formatted text?

                  Hi Jongware,


                  Would you mind discussing which option you did go with? I am currently working with XML in the scenario you suggest in your top post. I import an XML file, map it to paragraph styles and when I place it onto a page my INDD crashes every time. I've also tried placing unformatted XML on a page then mapping the styles and INDD crashes. Finally, I tried placing unformatted XML on a page then find/replacing paragraph styles using changeGrep() and it still crashes.


                  Did you experiment with the XML option? Did you have a similar experience? If you chose another option, if you don't mind sharing what worked well I'd appreciate it.




                  • 6. Re: [JS/CS4+] Best way to add formatted text?
                    cweger79 Level 1

                    Hi folks,


                    I needed to do this exact thing back in the CS3 days, and back then I found that tagged text was by far the fastest alternative. I did not try Harbs' idea, though.


                    I wrote the tagged text using a .NET app, then imported it. Worked perfectly. But you have to read the docs for tagged text to make sure you get it right. Another alternative: manually format some text using every possible style you might need, then export as tagged text so you can see what you need to build for import.