gpt4 book ai didi

c# - OpenXML在word文档C#中插入评论回复

转载 作者:行者123 更新时间:2023-11-30 23:26:44 24 4
gpt4 key购买 nike

我正在尝试使用 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中的调试器将注释放入文档中,但是当我打开文档时却没有显示。

执行保存命令后,注释被添加到文档中,但不显示。

这里的总体目标是在文档中包含的评论列表中的特定评论之后插入评论。有人可以帮我找到解决方案吗?

最佳答案

我发现创建回复评论需要以下内容

  • 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();

}
}
}

关于c# - OpenXML在word文档C#中插入评论回复,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36799675/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com