OpenXML在word文档C#中插入评论回复
OpenXML insert comment reply into word document C#
我正在尝试使用 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中的调试器可以看到注释被放到了文档中,但是打开文档时却没有显示出来。
执行保存命令后,注释添加到文档中,但不显示。
此处的总体目标是在文档中包含的评论列表中的特定评论之后插入评论。有人可以帮我找到解决办法吗?
在听从了 Meister 的建议后,我解决了我的问题。标记为回复的评论位于 commentsExtended.xml 文件中。要与评论建立关系,您需要创建 CommentEX 对象和 link 您要插入的评论回复到您要回复的评论。实现评论回复的代码位于下方。 ParaIdParent 属性 是您回复的评论的 paraId。
private void BuildCommentExtendXML(WordprocessingDocument document, string randomHexBinaryValue, HexBinaryValue commentsParagraphDescendantId)
{
var commentsEx = document.MainDocumentPart.WordprocessingCommentsExPart.CommentsEx;
CommentEx commentEx = new CommentEx()
{
ParaId = randomHexBinaryValue,
ParaIdParent = commentsParagraphDescendantId,
Done = new OnOffValue(false)
};
commentsEx.Append(commentEx);
}
我发现创建回复评论需要以下内容
- CommentsEx 部分添加到文档中
- 回复评论段落链接到其父
- CommentRange 和 CommentReference 以正确的顺序添加
- 回复评论还是要加在评论部分
下面的示例代码将向文档中的现有单词添加注释
foreach (var paragraph in document.MainDocumentPart.Document.Descendants<Paragraph>())
{
foreach (var run in paragraph.Elements<Run>())
{
var item = run.Elements<Text>().FirstOrDefault(b => b.Text.Contains("DTT"));
if (item != null)
{
if (document.MainDocumentPart.GetPartsCountOfType<WordprocessingCommentsPart>() > 0)
{
comments = document.MainDocumentPart.WordprocessingCommentsPart.Comments;
commentsEx = document.MainDocumentPart.WordprocessingCommentsExPart.CommentsEx;
if (comments.HasChildren)
{
// Obtain an unused ID.
id = comments.Descendants<Comment>().Select(e => e.Id.Value).Max();
}
}
else
{
// No WordprocessingCommentsPart part exists, so add one to the package.
WordprocessingCommentsPart commentPart = document.MainDocumentPart.AddNewPart<WordprocessingCommentsPart>();
commentPart.Comments = new Comments();
comments = commentPart.Comments;
WordprocessingCommentsExPart commentsExPart = document.MainDocumentPart.AddNewPart<WordprocessingCommentsExPart>();
commentsExPart.CommentsEx = new CommentsEx();
commentsEx = commentsExPart.CommentsEx;
}
Comment comment1 = new Comment() { Initials = "GS", Author = "Tony, Stark", Date = System.Xml.XmlConvert.ToDateTime("2018-11-19T14:54:00Z", System.Xml.XmlDateTimeSerializationMode.RoundtripKind), Id = "1" };
Paragraph paragraph1 = new Paragraph() { ParagraphId = "68DAFED3", TextId = "77777777" };
paragraph1.Append(new Run(new Text("fsdfas")));
comment1.Append(paragraph1);
Comment comment2 = new Comment() { Initials = "GS", Author = "Tony, Stark", Date = System.Xml.XmlConvert.ToDateTime("2018-11-19T14:54:00Z", System.Xml.XmlDateTimeSerializationMode.RoundtripKind), Id = "2" };
Paragraph paragraph2 = new Paragraph() { ParagraphId = "346EE35B", TextId = "77777777" };
paragraph2.Append(new Run(new Text("sadfsad")));
comment2.Append(paragraph2);
comments.Append(comment1);
comments.Append(comment2);
comments.Save();
CommentRangeStart commentRangeStart1 = new CommentRangeStart() { Id = "1" };
CommentRangeStart commentRangeStart2 = new CommentRangeStart() { Id = "2" };
CommentRangeEnd commentRangeEnd1 = new CommentRangeEnd() { Id = "1" };
CommentReference commentReference1 = new CommentReference() { Id = "1" };
CommentRangeEnd commentRangeEnd2 = new CommentRangeEnd() { Id = "2" };
CommentReference commentReference2 = new CommentReference() { Id = "2" };
run.InsertBefore(commentRangeStart1, item);
run.InsertBefore(commentRangeStart2, item);
var cmtEnd = run.InsertAfter(commentRangeEnd1, item);
var cmtEnd2 = run.InsertAfter(commentRangeEnd2, cmtEnd);
run.InsertAfter(new Run(commentReference1), cmtEnd);
run.InsertAfter(new Run(commentReference2), cmtEnd2);
CommentEx commentEx1 = new CommentEx() { ParaId = "68DAFED3", Done = false };
CommentEx commentEx2 = new CommentEx() { ParaId = "346EE35B", ParaIdParent = "68DAFED3", Done = false };
commentsEx.Append(commentEx1);
commentsEx.Append(commentEx2);
commentsEx.Save();
}
}
}
我正在尝试使用 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中的调试器可以看到注释被放到了文档中,但是打开文档时却没有显示出来。
执行保存命令后,注释添加到文档中,但不显示。
此处的总体目标是在文档中包含的评论列表中的特定评论之后插入评论。有人可以帮我找到解决办法吗?
在听从了 Meister 的建议后,我解决了我的问题。标记为回复的评论位于 commentsExtended.xml 文件中。要与评论建立关系,您需要创建 CommentEX 对象和 link 您要插入的评论回复到您要回复的评论。实现评论回复的代码位于下方。 ParaIdParent 属性 是您回复的评论的 paraId。
private void BuildCommentExtendXML(WordprocessingDocument document, string randomHexBinaryValue, HexBinaryValue commentsParagraphDescendantId)
{
var commentsEx = document.MainDocumentPart.WordprocessingCommentsExPart.CommentsEx;
CommentEx commentEx = new CommentEx()
{
ParaId = randomHexBinaryValue,
ParaIdParent = commentsParagraphDescendantId,
Done = new OnOffValue(false)
};
commentsEx.Append(commentEx);
}
我发现创建回复评论需要以下内容
- CommentsEx 部分添加到文档中
- 回复评论段落链接到其父
- CommentRange 和 CommentReference 以正确的顺序添加
- 回复评论还是要加在评论部分
下面的示例代码将向文档中的现有单词添加注释
foreach (var paragraph in document.MainDocumentPart.Document.Descendants<Paragraph>())
{
foreach (var run in paragraph.Elements<Run>())
{
var item = run.Elements<Text>().FirstOrDefault(b => b.Text.Contains("DTT"));
if (item != null)
{
if (document.MainDocumentPart.GetPartsCountOfType<WordprocessingCommentsPart>() > 0)
{
comments = document.MainDocumentPart.WordprocessingCommentsPart.Comments;
commentsEx = document.MainDocumentPart.WordprocessingCommentsExPart.CommentsEx;
if (comments.HasChildren)
{
// Obtain an unused ID.
id = comments.Descendants<Comment>().Select(e => e.Id.Value).Max();
}
}
else
{
// No WordprocessingCommentsPart part exists, so add one to the package.
WordprocessingCommentsPart commentPart = document.MainDocumentPart.AddNewPart<WordprocessingCommentsPart>();
commentPart.Comments = new Comments();
comments = commentPart.Comments;
WordprocessingCommentsExPart commentsExPart = document.MainDocumentPart.AddNewPart<WordprocessingCommentsExPart>();
commentsExPart.CommentsEx = new CommentsEx();
commentsEx = commentsExPart.CommentsEx;
}
Comment comment1 = new Comment() { Initials = "GS", Author = "Tony, Stark", Date = System.Xml.XmlConvert.ToDateTime("2018-11-19T14:54:00Z", System.Xml.XmlDateTimeSerializationMode.RoundtripKind), Id = "1" };
Paragraph paragraph1 = new Paragraph() { ParagraphId = "68DAFED3", TextId = "77777777" };
paragraph1.Append(new Run(new Text("fsdfas")));
comment1.Append(paragraph1);
Comment comment2 = new Comment() { Initials = "GS", Author = "Tony, Stark", Date = System.Xml.XmlConvert.ToDateTime("2018-11-19T14:54:00Z", System.Xml.XmlDateTimeSerializationMode.RoundtripKind), Id = "2" };
Paragraph paragraph2 = new Paragraph() { ParagraphId = "346EE35B", TextId = "77777777" };
paragraph2.Append(new Run(new Text("sadfsad")));
comment2.Append(paragraph2);
comments.Append(comment1);
comments.Append(comment2);
comments.Save();
CommentRangeStart commentRangeStart1 = new CommentRangeStart() { Id = "1" };
CommentRangeStart commentRangeStart2 = new CommentRangeStart() { Id = "2" };
CommentRangeEnd commentRangeEnd1 = new CommentRangeEnd() { Id = "1" };
CommentReference commentReference1 = new CommentReference() { Id = "1" };
CommentRangeEnd commentRangeEnd2 = new CommentRangeEnd() { Id = "2" };
CommentReference commentReference2 = new CommentReference() { Id = "2" };
run.InsertBefore(commentRangeStart1, item);
run.InsertBefore(commentRangeStart2, item);
var cmtEnd = run.InsertAfter(commentRangeEnd1, item);
var cmtEnd2 = run.InsertAfter(commentRangeEnd2, cmtEnd);
run.InsertAfter(new Run(commentReference1), cmtEnd);
run.InsertAfter(new Run(commentReference2), cmtEnd2);
CommentEx commentEx1 = new CommentEx() { ParaId = "68DAFED3", Done = false };
CommentEx commentEx2 = new CommentEx() { ParaId = "346EE35B", ParaIdParent = "68DAFED3", Done = false };
commentsEx.Append(commentEx1);
commentsEx.Append(commentEx2);
commentsEx.Save();
}
}
}