public void TestSearchTextFound() { XWPFDocument xml = XWPFTestDataSamples.OpenSampleDocument("ThreeColHead.docx"); IList <XWPFParagraph> ps = xml.Paragraphs; Assert.AreEqual(10, ps.Count); XWPFParagraph p = ps[(0)]; TextSegement segment = p.SearchText("sample word document", new PositionInParagraph()); Assert.IsNotNull(segment); Assert.AreEqual("sample word document", p.GetText(segment)); Assert.IsTrue(p.RemoveRun(0)); }
/** * Get a Text * @param segment */ public String GetText(TextSegement 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()); }
/** * Get a Text * @param segment */ public String GetText(TextSegement 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 out1 = new StringBuilder(); //for (int i = RunBegin; i <= RunEnd; i++) //{ // int startText = 0, endText = paragraph.GetRArray(i).TList.Size() - 1; // if (i == RunBegin) // startText = textBegin; // if (i == RunEnd) // endText = textEnd; // for (int j = startText; j <= endText; j++) // { // String tmpText = paragraph.GetRArray(i).GetTArray(j).StringValue; // int startChar = 0, endChar = tmpText.Length() - 1; // if ((j == textBegin) && (i == RunBegin)) // startChar = charBegin; // if ((j == textEnd) && (i == RunEnd)) // { // endChar = charEnd; // } // out1.Append(tmpText.Substring(startChar, endChar + 1)); // } //} //return out1.ToString(); throw new NotImplementedException(); }
/** * 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. * @param searched * @param startPos */ public TextSegement 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) { TextSegement segement = new TextSegement(); 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; }
/** * Get a Text * @param segment */ public String GetText(TextSegement 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 TextSegement 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) { TextSegement segement = new TextSegement(); 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); }
public string GetText(TextSegement segment) { throw new NotImplementedException(); }