private void CreateFlatTextRanges() { foreach (XDocument document in this.documents) { FlatTextRange currentRange = null; foreach (XElement run in document.Descendants(FlatConstants.RunElementName)) { if (!run.HasElements) { continue; } FlatText flatText = FlattenRunElement(run); if (flatText == null) { continue; } // If current Run doesn't belong to same parent (like paragraph, hyperlink, etc.) // create new FlatTextRange, otherwise use current one. if (currentRange == null || currentRange.Parent != run.Parent) { currentRange = this.CreateFlatTextRange(run.Parent); } currentRange.AddFlatText(flatText); } } }
private static void ReplaceText(FlatText flatText, int searchStartIndex, int searchEndIndex, string replace) { string text = string.Empty; if (flatText.StartIndex < searchStartIndex) { // Leave text that is before the searched text. text = flatText.Text.Remove(searchStartIndex - flatText.StartIndex); } text += replace; if (flatText.EndIndex > searchEndIndex) { // Leave text that is after the searched text. text += flatText.Text.Substring(searchEndIndex - flatText.StartIndex + 1); } flatText.Text = text; }
public void FindAndReplace(string find, string replace, StringComparison comparisonType) { int searchStartIndex = -1, searchEndIndex = -1, searchPosition = 0; while ((searchStartIndex = this.rangeText.ToString().IndexOf(find, searchPosition, comparisonType)) != -1) { searchEndIndex = searchStartIndex + find.Length - 1; // Find FlatText that contains the beginning of the searched text. LinkedListNode <FlatText> node = this.FindNode(searchStartIndex); FlatText flatText = node.Value; ReplaceText(flatText, searchStartIndex, searchEndIndex, replace); // Remove next FlatTexts that contain parts of the searched text. this.RemoveNodes(node, searchEndIndex); this.ResetRangeText(); searchPosition = searchStartIndex + replace.Length; } }
private void AddRangeText(FlatText flatText) { flatText.SetIndexes(this.rangeText.Length); this.rangeText.Append(flatText.Text); }
public void AddFlatText(FlatText flatText) { this.range.AddLast(flatText); this.AddRangeText(flatText); }