Search and Add Comment with OpenXML for Word

1k views Asked by At

I'm newbie with the OpenXML SDK.

I need to perform the following tasks on word docx on server side, and I believe the OpenXML SDK is the first place I should look into.

So my requirements are as follows:

  1. Look for a specific text in the document

  2. Add a comment in the location of the found text.

Is it possible with the OpenXML SDK? I tried to go through the docs, and I found a way to search and replace text, but couldn't find a way to add a comment in the appropriate location.

Thanks!

1

There are 1 answers

0
Maxime Porté On BEST ANSWER

The link Ron gave indicate how to insert the comment. To insert it where you want, you can find the paragraph with something like

Paragraph p = Body.Descendants<Paragraph>().Where(p.InnerText.Contains(searchString)).FirstOrDefault();
if (p != null)
{
    // Here follow the link Ron post to create a comment
    // insert the comment into p with p.InsertAfter() as said in the link
}

Maybe you have to adapt it a bit depending what are the matching criteria