How to add comments to word documents in C# for WPF applications

With the help of Spire.Doc for WPF, developers can add word documents easily for their WPF applications. When we want to show opinions or additional information about words, phrases or paragraphs, we can add the comments to the word documents to give more information about the contents. Spire.Doc offers a class of Spire.Doc.Fields.Comments to enable developers to work with comments easily. This article will demonstrate how to add the comments to the word documents in C# for WPF applications.

Note: Before Start, please download the latest version of Spire.Doc and add Spire.Doc.Wpf.dll in the bin folder as the reference of Visual Studio.

Here comes to the steps of how to add comments to word documents in C#:

Step 1: Create a new word document and load the document from file.

Document document = new Document();
document.LoadFromFile("sample.docx");

Step 2: Get Paragraph to Insert Comment.

Section section = document.Sections[0];
Paragraph paragraph = section.Paragraphs[2];

Step 3: Insert the comment.

string str = "This is the first comment";
Comment comment = paragraph.AppendComment(str);
comment.Format.Author = "E-iceblue";
comment.Format.Initial = "CM";

Step 4: Save the document to file.

document.SaveToFile("Addcomment.docx", FileFormat.Docx);
System.Diagnostics.Process.Start("Addcomment.docx");

Effective screenshot of adding the word comments in C#:

How to add comments to word documents in C# for WPF applications

Full codes:

using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
using System.Windows;

namespace WpfApplication1
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            {
                Document document = new Document();
                document.LoadFromFile("sample.docx");

                Section section = document.Sections[0];
                Paragraph paragraph = section.Paragraphs[2];

                string str = "This is the first comment";
                Comment comment = paragraph.AppendComment(str);
                comment.Format.Author = "E-iceblue";
                comment.Format.Initial = "CM";

                document.SaveToFile("Addcomment.docx", FileFormat.Docx);
                System.Diagnostics.Process.Start("Addcomment.docx");

            }



        }
    }
}