public static IText GetTextAtLocation(this ISegment segment, int startIndex)
        {
            var counter = segment.GetCharacterCountingIterator(startIndex);

            if (counter.CharacterCount < startIndex)
            {
                // the actual count is between this location and the next
                //  - if this is a text node we can point to the exact location inside the text
                IText text = counter.CurrentLocation.ItemAtLocation as IText;
                return(text);
            }
            return(null);
        }
        public static void Replace(this ISegment segment, int startIndex, int endPosition, string replacementText)
        {
            var counter = segment.GetCharacterCountingIterator(startIndex);

            if (counter.CharacterCount < startIndex)
            {
                // the actual count is between this location and the next
                //  - if this is a text node we can point to the exact location inside the text
                IText text = counter.CurrentLocation.ItemAtLocation as IText;
                if (text != null)
                {
                    var startLocationInsideTextItem = new TextLocation(counter.CurrentLocation, startIndex - counter.CharacterCount);
                    var sb = new StringBuilder(text.Properties.Text);
                    sb.Remove(startIndex, endPosition - startIndex);
                    sb.Insert(startIndex, replacementText);
                    text.Properties.Text = sb.ToString();
                }
            }
        }
        public static string Substring(this ISegment segment, int startIndex, int endPosition)
        {
            var counter = segment.GetCharacterCountingIterator(startIndex);

            if (counter.CharacterCount < startIndex)
            {
                // the actual count is between this location and the next
                //  - if this is a text node we can point to the exact location inside the text
                IText text = counter.CurrentLocation.ItemAtLocation as IText;
                if (text != null)
                {
                    var startLocationInsideTextItem = new TextLocation(counter.CurrentLocation, startIndex - counter.CharacterCount);
                    var segmentText = text.Properties.Text;
                    return(segmentText.Substring(startLocationInsideTextItem.TextOffset,
                                                 endPosition - startLocationInsideTextItem.TextOffset));
                }
            }

            return(string.Empty);
        }