none
How to detect the formatting changes in the word document using open xml? RRS feed

  • Question

  • I want to compare two paragraphs in which i have to detect the font changes, font color changes, bold underline  and italics and the text changes using open xml sdk2.0 in C#.I have compared the text.Can any one help me to sort out this problem. Thanks in advance.
    Tuesday, May 31, 2011 5:39 AM

Answers

  • Hi Anisha,

    One way is to retrieve these properties of the paragraphs and then compare each properties of the two paragraphs one by one, but this is complicated. Another way is get the InnerXml of the two paragraphs and then compare the InnerXml text, for instance:

                string filename = "test.docx";
                using (WordprocessingDocument doc = WordprocessingDocument.Open(filename, true))
                {
                    MainDocumentPart mainPart = doc.MainDocumentPart;
                    IList<Paragraph> paragraphList = doc.MainDocumentPart.Document.Body.Elements<Paragraph>().ToList<Paragraph>();
     
                    foreach (Paragraph pr in paragraphList)
                    {
                        Console.WriteLine(pr.InnerXml);
                    }
                }

     The console applicaition can get the InnerXml of the paragraphs. You can adapt it to your project and then compared the retrieved the xml string.

    Hope the suggestion can help you and just feel free to follow  up after you have tried.

    Best Regards,


    Bruce Song [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Bruce Song Tuesday, June 21, 2011 1:47 PM
    Friday, June 3, 2011 11:27 AM

All replies

  • Hi Anisha,

    One way is to retrieve these properties of the paragraphs and then compare each properties of the two paragraphs one by one, but this is complicated. Another way is get the InnerXml of the two paragraphs and then compare the InnerXml text, for instance:

                string filename = "test.docx";
                using (WordprocessingDocument doc = WordprocessingDocument.Open(filename, true))
                {
                    MainDocumentPart mainPart = doc.MainDocumentPart;
                    IList<Paragraph> paragraphList = doc.MainDocumentPart.Document.Body.Elements<Paragraph>().ToList<Paragraph>();
     
                    foreach (Paragraph pr in paragraphList)
                    {
                        Console.WriteLine(pr.InnerXml);
                    }
                }

     The console applicaition can get the InnerXml of the paragraphs. You can adapt it to your project and then compared the retrieved the xml string.

    Hope the suggestion can help you and just feel free to follow  up after you have tried.

    Best Regards,


    Bruce Song [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Bruce Song Tuesday, June 21, 2011 1:47 PM
    Friday, June 3, 2011 11:27 AM
  • Thanks a lot.
    Wednesday, June 22, 2011 4:16 AM