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.
如何在文本运行中搜索特定文本(使用OpenXML SDK 2.0在Docx中),一旦找到它,如何在“搜索文本”周围插入注释。 “搜索文本”可以是现有运行的子字符串。样本中的所有示例都会在第一段中插入注释或类似的简单...而不是我正在寻找的内容。
Thanks
2 个解决方案
#1
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):
你必须把它分解成单独的运行。尝试使用DocumentReflector - 它甚至生成C#代码 - 来查看用word创建的文档。结构应该看起来像这样(简化):
<paragraph>
<run>...</run>
<commentRangeStart />
<run>search text</run>
<commentRangeEnd />
<run>...</run>
</paragraph>
#2
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 );
}
#1
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):
你必须把它分解成单独的运行。尝试使用DocumentReflector - 它甚至生成C#代码 - 来查看用word创建的文档。结构应该看起来像这样(简化):
<paragraph>
<run>...</run>
<commentRangeStart />
<run>search text</run>
<commentRangeEnd />
<run>...</run>
</paragraph>
#2
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 );
}