C# 在Docx文件*中插入注释*围绕*运行中的文本

C# 在Docx文件*中插入注释*围绕*运行中的文本,c#,openxml,docx,wordml,C#,Openxml,Docx,Wordml,如何在文本运行中搜索特定文本(在Docx中使用openxmlsdk2.0),一旦找到,如何在“搜索文本”周围插入注释。“搜索文本”可以是现有运行的子字符串。示例中的所有示例都会在第一段或类似的简单内容周围插入注释。。。不是我要找的 谢谢您必须将其分为几次运行。尝试使用DocumentReflector(它甚至生成C代码)查看用word创建的文档。结构应如下所示(简化): ... 搜索文本 ... 对于仍在寻找答案的人: 代码是否为: private void AddComment( Parag

如何在文本运行中搜索特定文本(在Docx中使用openxmlsdk2.0),一旦找到,如何在“搜索文本”周围插入注释。“搜索文本”可以是现有运行的子字符串。示例中的所有示例都会在第一段或类似的简单内容周围插入注释。。。不是我要找的


谢谢

您必须将其分为几次运行。尝试使用DocumentReflector(它甚至生成C代码)查看用word创建的文档。结构应如下所示(简化):


...
搜索文本
...

对于仍在寻找答案的人:

代码是否为:

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 );
        }
private void AddComment(段落,字符串文本)
{
字符串commentId=GetNextCommentId();
Comment Comment=new Comment(){Id=commentId,Date=DateTime.Now};
段落commentPara=新段落(新运行(新文本)(GetCommentsString(文本)){RunProperties=新运行属性(新运行样式(){Val=“CommentReference”})});
commentPara.ParagraphProperties=新段落属性(新段落样式ID(){Val=“CommentText”});
评论.附件(第7段);
_comments.AppendChild(comment);//comments对象
_comments.Save();
段落.InsertBefore(新的CommentRangeStart(){Id=commentId},段落.GetFirstChild());
var commentEnd=段落.InsertAfter(新的CommentRangeEnd(){Id=commentId},段落.Elements().Last());
段落.InsertAfter(新运行(newcommentreference(){Id=commentId}),commentEnd);
}
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 );
        }