I have a requirement to iterate through a praragraph and generate an XML of all styles.
For example, I have one paragraph as following:
Code for generating above:
paragraph1.contents = "abcdef" + String.fromCharCode (0x000D);
paragraph1.justification = Justification.leftAlign;
paragraph1.pointSize = 10;
//first 3 charachters as underline
var underchar10 = paragraph1.characters.itemByRange(0,2);
//2nd and 3rd charachters as bold
var boldchar10 = paragraph1.characters.itemByRange(1,2);
How can I iterate through paragraph object and retrieve all styles that were set in it? For example, if I get an instance of above paragraph object, I want to retrieve all styling information, so that I can generate some string like:
<paragraph content="abcdef" align="Justification.leftAlign" pointSize="10" fillColor="black">
<characterStyle Range="0,2" underline="true" />
<characterStyle Range="1,2" bold="true" />
Any hint, example, tutorial or link would help!
Take a look at TextStyleRange. It seems to do exactly that -- reporting the length of each character span with the exact same attributes.
There are two drawbacks: (1) TextStyleRange happily runs over the end of paragraphs and into the next paragraph(s) if the formatting is the same. You need some trick to determine when that happens. I've used both index (of the first character of the next paragraph) and blindly testing for a hard return in the TextStyleRange contents. There has been some talk on this forum on the subject -- try to find it.
(2) (A major one!) ID tells you the next fragment has a different formatting ... but it does not tell you what the difference is. You'll have to test all of the attributes you are interested in. If there are no differences in those, you might as well consider the formatting "the same".
That said, these drawbacks are nothing compared to "manually" comparing formatting of each and every single character to the next one.