Hi Vinutha,
I have created sample please refer the below code.
HTML
<div>
<table>
<tr>
<td>
Search Text:
</td>
<td>
<asp:TextBox runat="server" ID="txtSearchWord" Width="75px" />
</td>
</tr>
<tr>
<td>
No of paragraph to display:
</td>
<td>
<asp:DropDownList runat="server" ID="ddlNoOfPara" AutoPostBack="true" OnSelectedIndexChanged="ddlNoOfParaIndexChanged">
<asp:ListItem Text="Select" />
<asp:ListItem Text="1" />
<asp:ListItem Text="2" />
<asp:ListItem Text="3" />
<asp:ListItem Text="4" />
<asp:ListItem Text="5" />
<asp:ListItem Text="6" />
<asp:ListItem Text="7" />
<asp:ListItem Text="8" />
<asp:ListItem Text="9" />
</asp:DropDownList>
</td>
</tr>
</table>
<br />
<asp:Label ID="lblText" runat="server" />
</div>
Code
private string ReadFileContent(string path, int noOfPara, string searchText)
{
StringBuilder sb = new StringBuilder();
Application wordApp = new Application();
object file = path;
object nullObj = System.Reflection.Missing.Value;
Document doc = wordApp.Documents.Open(
ref file, ref nullObj, ref nullObj,
ref nullObj, ref nullObj, ref nullObj,
ref nullObj, ref nullObj, ref nullObj,
ref nullObj, ref nullObj, ref nullObj);
Paragraphs documentParagraph = doc.Paragraphs;
ArrayList Pararray = new ArrayList();
for (int i = 1; i <= documentParagraph.Count; i++)
{
sb.Append(documentParagraph[i].Range.Text);
Pararray.Add(sb);
}
string[] newWordList = { };
if (Pararray[0].ToString().ToLower().Contains(searchText.ToLower()))
{
newWordList = Pararray[0].ToString().Substring(Pararray[0].ToString().IndexOf(searchText), Pararray[0].ToString().Length - Pararray[0].ToString().IndexOf(searchText)).Split('\r');
}
StringBuilder sbNewWordList = new StringBuilder();
if (newWordList.Length > noOfPara)
{
for (int j = 1; j <= noOfPara; j++)
{
string word = newWordList[j].ToString();
sbNewWordList.Append(word + "\r");
}
}
((_Document)doc).Close();
((_Application)wordApp).Quit();
return sbNewWordList.ToString().Replace("\r", "<br/>");
}
protected void ddlNoOfParaIndexChanged(object sender, EventArgs e)
{
lblText.Text = ReadFileContent(@"F:\Test.docx", int.Parse(ddlNoOfPara.SelectedItem.Text.Trim()), txtSearchWord.Text.Trim());
}
Screenshot
Input Word Document.
I have a program that reads a word document and displays it on the webpage.
I am storing all the paragraphs in the word document in an array list.
Now I want to use arraylist.contains method to search for a particular word and then display the next 8-10 paragraphs after the string match.
Can anyone help me on this?
Here is the code so far.
Hello. I am already able to read paragraphs.
I have put all the paragraphs in an array list.
Now I have to use "arraylist.contains" method to find a specific word and read the next 8 to 10 paragraphs.
Something like.
Suppose all the paragraphs are stored in an Array list.
OutPut
