/** * Get a Text * @param segment */ public String GetText(TextSegment segment) { int RunBegin = segment.BeginRun; int textBegin = segment.BeginText; int charBegin = segment.BeginChar; int RunEnd = segment.EndRun; int textEnd = segment.EndText; int charEnd = segment.EndChar; StringBuilder text = new StringBuilder(); for (int i = RunBegin; i <= RunEnd; i++) { int startText = 0, endText = paragraph.GetRList()[i].GetTList().Count - 1; if (i == RunBegin) startText = textBegin; if (i == RunEnd) endText = textEnd; for (int j = startText; j <= endText; j++) { String tmpText = paragraph.GetRList()[i].GetTArray(j).Value; int startChar = 0, endChar = tmpText.Length - 1; if ((j == textBegin) && (i == RunBegin)) startChar = charBegin; if ((j == textEnd) && (i == RunEnd)) { endChar = charEnd; } text.Append(tmpText.Substring(startChar, endChar - startChar + 1)); } } return text.ToString(); }
/// <summary> /// this methods parse the paragraph and search for the string searched. /// If it finds the string, it will return true and the position of the String will be saved in the parameter startPos. /// </summary> /// <param name="searched"></param> /// <param name="startPos"></param> /// <returns></returns> public TextSegment SearchText(String searched, PositionInParagraph startPos) { int startRun = startPos.Run, startText = startPos.Text, startChar = startPos.Char; int beginRunPos = 0, candCharPos = 0; bool newList = false; for (int runPos = startRun; runPos < paragraph.GetRList().Count; runPos++) { int beginTextPos = 0, beginCharPos = 0, textPos = 0, charPos = 0; CT_R ctRun = paragraph.GetRList()[runPos]; foreach (object o in ctRun.Items) { if (o is CT_Text) { if (textPos >= startText) { String candidate = ((CT_Text)o).Value; if (runPos == startRun) charPos = startChar; else charPos = 0; for (; charPos < candidate.Length; charPos++) { if ((candidate[charPos] == searched[0]) && (candCharPos == 0)) { beginTextPos = textPos; beginCharPos = charPos; beginRunPos = runPos; newList = true; } if (candidate[charPos] == searched[candCharPos]) { if (candCharPos + 1 < searched.Length) { candCharPos++; } else if (newList) { TextSegment segement = new TextSegment(); segement.BeginRun = (beginRunPos); segement.BeginText = (beginTextPos); segement.BeginChar = (beginCharPos); segement.EndRun = (runPos); segement.EndText = (textPos); segement.EndChar = (charPos); return segement; } } else candCharPos = 0; } } textPos++; } else if (o is CT_ProofErr) { //c.RemoveXml(); } else if (o is CT_RPr) { //do nothing } else candCharPos = 0; } } return null; }