【发布时间】:2018-11-20 02:53:00
【问题描述】:
我正在尝试使用 OpenXml 插入评论作为回复。如果不可能,我想在所选评论之后插入评论。到目前为止,我可以在我想要的位置插入评论,但是当我打开文档时我无法显示评论。
下面是插入注释的代码。
using (WordprocessingDocument document = WordprocessingDocument.Open(path + fileName + ".docx", true)){
// Locate the first paragraph in the document.
//XMLParagraphAlias firstParagraph = document.MainDocumentPart.Document.Descendants<XMLParagraphAlias>().First();
XMLCommentsAlias comments = document.MainDocumentPart.WordprocessingCommentsPart.Comments;
string id = comments.Descendants<DocumentFormat.OpenXml.Wordprocessing.Comment>()
.Where(i => i.Id.Value == reply.CurrentCommentID.ToString())
.Select(e => e.Id.Value)
.First();
// Compose a new Comment and add it to the Comments part.
XMLParagraphAlias p = new XMLParagraphAlias(new Run(new Text(reply.ReplyText)));
string newCommentID = comments.Descendants<DocumentFormat.OpenXml.Wordprocessing.Comment>().Select(e => e.Id.Value).Max();
XMLCommentAlias cmt = new XMLCommentAlias()
{
Id = newCommentID,
Author = reply.CurrentUserName,
Date = DateTime.Now.Date
};
XMLCommentAlias comment = comments.Elements<XMLCommentAlias>()
.Where(n => n.Id.Value == reply.CurrentCommentID.ToString())
.First();
XMLParagraphAlias test2 = comment.Descendants<XMLParagraphAlias>().First();
cmt.AppendChild(p);
comments.AppendChild(cmt);
comments.Save();
// Specify the text range for the Comment.
// Insert the new CommentRangeStart before the first run of paragraph.
test2.InsertBefore(new CommentRangeStart() { Id = reply.CurrentCommentID.ToString() }, test2.GetFirstChild<Run>());
// Insert the new CommentRangeEnd after last run of paragraph.
var cmtEnd = test2.InsertAfter(new CommentRangeEnd() { Id = reply.CurrentCommentID.ToString() }, test2.Elements<Run>().Last());
// Compose a run with CommentReference and insert it.
test2.InsertAfter(new Run(new CommentReference() { Id = reply.CurrentCommentID.ToString() }), cmtEnd);
}
我可以看到使用VS中的调试器将注释放入文档中,但是当我打开文档时它没有显示出来。
执行保存命令后,注释被添加到文档中,但不显示。
这里的总体目标是在文档中包含的 cmets 列表中的特定注释之后插入注释。有人可以帮我找到解决办法吗?
【问题讨论】:
-
我建议你创建一个简单的文档,带有一个注释,保存它。现在添加另一个评论(或回复)并将其保存在不同的名称下。在 Open XML Productivity Tool 中打开其中的第一个,然后使用其“比较”功能将其与第二个文档进行比较。该工具将 1) 向您展示底层 XML 的差异和 2) 将第一个文档更改为第二个文档所需的代码。 IOW 它应该向您展示如何添加回复(或其他评论)。
标签: c# ms-word openxml openxml-sdk