Click here to Skip to main content
15,868,141 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi
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"



How to do that...?

Please help to get this...


thanks $ regards,
P.SARAVANAN
Posted

1 solution

from msdn :

C#
static void Main(string[] args)
{
    byte[] docByteArray = File.ReadAllBytes("Test.docx");
    using (MemoryStream memoryStream = new MemoryStream())
    {
        memoryStream.Write(docByteArray, 0, docByteArray.Length);
        using (WordprocessingDocument doc =
            WordprocessingDocument.Open(memoryStream, true))
        {
            RevisionAccepter.AcceptRevisions(doc);
            XElement root = doc.MainDocumentPart.GetXDocument().Root;
            XElement body = root.LogicalChildrenContent().First();
            foreach (XElement blockLevelContentElement in body.LogicalChildrenContent())
            {
                if (blockLevelContentElement.Name == W.p)
                {
                    var text = blockLevelContentElement
                        .LogicalChildrenContent()
                        .Where(e => e.Name == W.r)
                        .LogicalChildrenContent()
                        .Where(e => e.Name == W.t)
                        .Select(t => (string)t)
                        .StringConcatenate();
                    Console.WriteLine("Paragraph text >{0}<", text);
                    continue;
                }
                // If element is not a paragraph, it must be a table.
                Console.WriteLine("Table");
            }
        }
    }
}
 
Share this answer
 
v2
Comments
saravanan6 21-Mar-11 13:43pm    
Thanks Piccadilly Yum,

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.In your code, I saw there is no code to get the search Text.? Please Guide me to get out of this issue...

I am very much expecting your reply...


Thanks & Regards,
P.SARAVANAN
Piccadilly Yum Yum 22-Mar-11 0:37am    
if (text.IndexOf("TEST",0)>-1) then 'paragraph contains TEST'
saravanan6 22-Mar-11 1:22am    
hi,What is RevisionAccepter here?... the Error says The Name RevisionAccepter does not exist in the current context.Do i need download anything for that?
saravanan6 22-Mar-11 1:38am    
i have downloaded the RevisionAccepter.zip file.But i don't know how to inlcude this source code into my project... Please Guide me to get out of this issue... Please...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900