[英]Add Comment in to selected Text in Word Document Using OpenXML c#
我需要使用OpenXML在Word文档中添加注释。 我需要添加注释到一个位置或一个单词(或多个单词)。 通常,在word文档openxml中,将这些文本作为运行元素返回。 但是我要添加注释的单词带有不同的运行元素。 因此,我无法在我真正想要的文档单词中添加注释。 这意味着我无法添加特定的CommentRangeStart
和CommentRangeEnd
对象。
我当前的实现如下。
foreach (var paragraph in document.MainDocumentPart.Document.Descendants<DocumentFormat.OpenXml.Wordprocessing.Paragraph>())
{
foreach (var run in paragraph.Elements<Run>())
{
var item = run.Elements<Text>().FirstOrDefault(b => b.Text.Trim() == "My words selection to add comment");
if (item != null)
{
run.InsertBefore(new CommentRangeStart() { Id = id }, item);
var cmtEnd = run.InsertAfter(new CommentRangeEnd() { Id = id }, item);
run.InsertAfter(new Run(new CommentReference() { Id = id }), cmtEnd);
}
}
}
更多详情..
<w:r><w:t>This </w:t></w:r>
<w:r><w:t>is </w:t></w:r>
<w:r><w:t>a first paragraph</w:t></w:r>
因此,在这种情况下,如何在文本“ is first first”中添加注释。
或者在某些情况下,openxml文档包含以下run元素。
<w:r><w:t>This is a first paragraph</w:t></w:r>
因此,在这两种情况下,如何在我的特定单词选择中添加注释。 我在这里添加了一个截图,正是我想要的。
如果样式没有变化,并且允许您操作文档,则可以轻松地将所有运行合并到一个段落中,然后隔离文本运行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.