0
votes

I am using c#4.0 and open xml sdk 2.0 for accessing Word file.For that, Now i want to Retrieve a paragraph based on the given text.If the paragraph contains my text then retrieve the paragraph containing that text...

FOR EXAMPLE: Given Word is: TEST

Retrieve the paragraphs that containing the word "TEST"

I want to search the given Word in the paragraph.If any matches found, Then i want to display that methods.If matches not found,no need to get the paragraph.

How i do?

1

1 Answers

5
votes

The main content of a word document is stored in the body element. At the simplest level, paragraphs can be located using Linq queries performed on the document:

using(WordprocessingDocument document = WordprocessingDocument.Open(documentStream, true)){
    foreach(Paragraph p in document.MainDocumentPart.Document.Body.Descendants<Paragraph>().Where<Paragraph>(p => p.InnerText.Equals("SOME TEXT")){
        // Do something with the Paragraphs.
    }
}

However I would advise that the problem is a little more complicated than this. As under each paragraph there may be more than one Run (essentially a sentence) containing a string of words. It is quite likely that where the user entered the word "SOME TEXT" also contains other runs.

But this should be able to point you in the correct direction.