user48753
user48753

Reputation: 21

Inserting a Comment in Docx file *around* a text inside a run

How do you search for a specific text inside a text run (in Docx using the OpenXML SDK 2.0) and once you find it how do you insert a comment surrounding the 'search text'. The 'search text' can be a sub string of an existing run. All example in the samples insert comments around the first paragraph or something simple like that... not what I'm looking for.

Thanks

Upvotes: 2

Views: 1324

Answers (2)

Tarveen
Tarveen

Reputation: 161

For somebody who might be still looking for the answer:

Here is the code for that:

private void AddComment( Paragraph paragraph, string text )
        {
            string commentId = GetNextCommentId();
            Comment comment = new Comment() { Id= commentId, Date = DateTime.Now };
            Paragraph commentPara = new Paragraph( new Run( new Text( GetCommentsString( text ) ) ) { RunProperties = new RunProperties( new RunStyle() { Val = "CommentReference" } ) } );
            commentPara.ParagraphProperties = new ParagraphProperties( new ParagraphStyleId() { Val = "CommentText" } );
            comment.AppendChild( commentPara );
            _comments.AppendChild( comment );//Comments object
            _comments.Save();

            paragraph.InsertBefore( new CommentRangeStart() { Id = commentId }, paragraph.GetFirstChild<Run>() );
            var commentEnd = paragraph.InsertAfter( new CommentRangeEnd() { Id = commentId }, paragraph.Elements<Run>().Last() );
            paragraph.InsertAfter( new Run( new CommentReference() { Id = commentId } ), commentEnd );
        }

Upvotes: 0

herskinduk
herskinduk

Reputation: 1187

You have to break it up into separate runs. Try using the DocumentReflector - it even genereates C# code - to look at a document created with word. The structure should look something like this (simplified):

<paragraph>
  <run>...</run>
  <commentRangeStart />
  <run>search text</run>
  <commentRangeEnd />
  <run>...</run>
</paragraph>

Upvotes: 2

Related Questions