使用C#代码向Word中的段落或指定文字添加评论

Microsoft Word中的评论功能为用户提供了一个很好的方式,可以在不更改或干扰文档内容的情况下,向Word文档添加自己的见解或意见。如果有人对文档进行评论,文档作者或其他用户可以回复评论,与其进行讨论,即使他们不在同一时间查看文档。本文将演示如何使用Spire.Doc for .NET库在C#和VB.NET中对Word中的段落或指定文字添加评论。

安装Spire.Doc for .NET

首先,您需要将Spire.Doc for .NET包中包含的DLL文件添加为您.NET项目的引用。您可以通过以下链接下载DLL文件,或者通过NuGet进行安装。

PM> Install-Package Spire.Doc

在C#和VB.NET中向Word段落添加评论

Spire.Doc for .NET提供了Paragraph.AppendComment()方法,可以向特定段落添加评论。以下是详细步骤:

using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
namespace AddComments
{
    internal class Program
    {
        static void Main(string[] args)
        {
            //初始化Document类的实例
            Document document = new Document();

            //Load a Word document
            document.LoadFromFile(@"Sample.docx");

            //获取文档中的第一个章节
            Section section = document.Sections[0];

            //获取章节中的第一个段落
            Paragraph paragraph = section.Paragraphs[0];

            //向段落添加评论
            Comment comment = paragraph.AppendComment("This comment is added using Spire.Doc for .NET.");

            //设置评论作者
            comment.Format.Author = "Eiceblue";
            comment.Format.Initial = "CM";

            //保存结果文档
            document.SaveToFile("AddCommentToParagraph.docx", FileFormat.Docx2013);
            document.Close();
        }
    }
}

在C#和VB.NET中向Word文本添加评论

Paragraph.AppendComment()方法用于向整个段落添加评论。默认情况下,评论标记会被放置在段落的末尾。要向特定文本添加评论,您需要使用Document.FindString()方法搜索文本,然后将评论标记放置在文本的开头和结尾。以下是详细步骤:

using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
namespace AddCommentsToText
{
    internal class Program
    {
        static void Main(string[] args)
        {
            //初始化Document类的实例
            Document document = new Document();

            //加载Word文档
            document.LoadFromFile(@"CommentTemplate.docx");

            //查找特定字符串
            TextSelection find = document.FindString("Microsoft Office", false, true);

            //创建评论开始标记和评论结束标记
            CommentMark commentmarkStart = new CommentMark(document);
            commentmarkStart.Type = CommentMarkType.CommentStart;
            CommentMark commentmarkEnd = new CommentMark(document);
            commentmarkEnd.Type = CommentMarkType.CommentEnd;

            //创建评论并设置其内容和作者
            Comment comment = new Comment(document);
            comment.Body.AddParagraph().Text = "Developed by Microsoft.";
            comment.Format.Author = "Shaun";

            //将找到的文本作为一个单一的文本范围获取
            TextRange range = find.GetAsOneRange();

            //获取文本范围所在的段落
            Paragraph para = range.OwnerParagraph;

            //将评论添加到段落
            para.ChildObjects.Add(comment);

            //获取文本范围在段落中的索引
            int index = para.ChildObjects.IndexOf(range);

            //为评论开始标记和评论结束标记设置评论ID
            commentmarkStart.CommentId = comment.Format.CommentId;
            commentmarkEnd.CommentId = comment.Format.CommentId;

            //在文本范围之前插入评论开始标记
            para.ChildObjects.Insert(index, commentmarkStart);

            //Insert the comment end mark after the text range
            para.ChildObjects.Insert(index + 2, commentmarkEnd);

            //保存结果文档
            document.SaveToFile("AddCommentForText.docx", FileFormat.Docx2013);
            document.Close();
        }
    }
}

申请临时许可证

如果您希望从生成的文档中移除评估消息,或解除功能限制,请申请一个30天的试用许可证。

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容