News Category

Comment

Comment (5)

In Word 2013 and later version, you can reply to a comment, so it's easier to follow the whole conversation. Spire.Doc also allows programmers to insert a comment as a reply to a selected comment through ReplyToComment method.

Step 1: Create an object of Document and load a Word document containing comments.

Document doc = new Document();
doc.LoadFromFile("Comment.docx");

Step 2: Get the first comment.

Comment comment = doc.Comments[0];

Step 3: Create a new comment and specify the author and content.

Comment replyComment = new Comment(doc);
replyComment.Format.Author = "Adam";
replyComment.Body.AddParagraph().AppendText("Exactly.");

Step 4: Add the new comment as a reply to the selected comment.

comment.ReplyToComment(replyComment);

Step 5: Save to file.

doc.SaveToFile("ReplyToComment.docx", FileFormat.Docx2013);

How to Reply to a Comment in Word in C#, VB.NET

Full Code:

[C#]
using Spire.Doc;
using Spire.Doc.Fields;
namespace ReplyComment
{
    class Program
    {

        static void Main(string[] args)
        {

            Document doc = new Document();
            doc.LoadFromFile("Comment.docx");
            Comment comment = doc.Comments[0];

            Comment replyComment = new Comment(doc);
            replyComment.Format.Author = "Adam";
            replyComment.Body.AddParagraph().AppendText("Exactly.");
            comment.ReplyToComment(replyComment);

            doc.SaveToFile("ReplyToComment.docx", FileFormat.Docx2013);

        }
    }
}
[VB.NET]
Imports Spire.Doc
Imports Spire.Doc.Fields
Namespace ReplyComment
	Class Program

		Private Shared Sub Main(args As String())

			Dim doc As New Document()
			doc.LoadFromFile("Comment.docx")
			Dim comment As Comment = doc.Comments(0)

			Dim replyComment As New Comment(doc)
			replyComment.Format.Author = "Adam"
			replyComment.Body.AddParagraph().AppendText("Exactly.")
			comment.ReplyToComment(replyComment)

			doc.SaveToFile("ReplyToComment.docx", FileFormat.Docx2013)

		End Sub
	End Class
End Namespace

A comment in Word can contain rich elements such as text, image, OLE object, and etc. This article presents how we can insert a picture to a comment in Word using Spire.Doc.

Step 1: Initialize an instance of Document class and load a sample document.

Document doc = new Document();
doc.LoadFromFile(@"C:\Users\Administrator\Desktop\sample.docx");

Step 2: Get the third paragraph from section one.

Paragraph paragraph = doc.Sections[0].Paragraphs[2];

Step 3: Append a comment to the paragraph.

Comment comment = paragraph.AppendComment("This is a comment.");
comment.Format.Author = "E-iceblue";   

Step 4: Insert an image to comment body.

DocPicture docPicture = new DocPicture(doc);
Image img = Image.FromFile(@"C:\Users\Administrator\Desktop\logo.png");
docPicture.LoadImage(img);
comment.Body.AddParagraph().ChildObjects.Add(docPicture);

Step 5: Save the file.

doc.SaveToFile("result.docx",FileFormat.Docx2013);

Output:

How to Insert Picture into Comment in Word in C#, VB.NET

Full Code:

[C#]
using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
using System.Drawing;
namespace InsertPicture
{
    class Program
    {

        static void Main(string[] args)
        {

            Document doc = new Document();
            doc.LoadFromFile(@"C:\Users\Administrator\Desktop\sample.docx");
            Paragraph paragraph = doc.Sections[0].Paragraphs[2];

            Comment comment = paragraph.AppendComment("This is a comment.");
            comment.Format.Author = "E-iceblue";
            DocPicture docPicture = new DocPicture(doc);
            Image img = Image.FromFile(@"C:\Users\Administrator\Desktop\logo.png");
            docPicture.LoadImage(img);
            comment.Body.AddParagraph().ChildObjects.Add(docPicture);

            doc.SaveToFile("result.docx", FileFormat.Docx2013);
        }
    }
}
[VB.NET]
Imports Spire.Doc
Imports Spire.Doc.Documents
Imports Spire.Doc.Fields
Imports System.Drawing
Namespace InsertPicture
	Class Program

		Private Shared Sub Main(args As String())

			Dim doc As New Document()
			doc.LoadFromFile("C:\Users\Administrator\Desktop\sample.docx")
			Dim paragraph As Paragraph = doc.Sections(0).Paragraphs(2)

			Dim comment As Comment = paragraph.AppendComment("This is a comment.")
			comment.Format.Author = "E-iceblue"
			Dim docPicture As New DocPicture(doc)
			Dim img As Image = Image.FromFile("C:\Users\Administrator\Desktop\logo.png")
			docPicture.LoadImage(img)
			comment.Body.AddParagraph().ChildObjects.Add(docPicture)

			doc.SaveToFile("result.docx", FileFormat.Docx2013)
		End Sub
	End Class
End Namespace

Word comments can be reviews or thoughts about part of contents or explanations or references of specified phrase, sentence or paragraph given by author. Also, the existing comments can be extracted from document and solution in this guide demonstrates how to extract Word comments and save to TXT file in C# and Visual Basic via Spire.Doc for .NET.

Spire.Doc for .NET, one easy-to-use .NET Word component to preform Word tasks, provides a Comment class to enable users to get comments in Word and paragraphs of comment body. The screenshot below shows the original documents with two comments.

Extract Word Commnet

Download and install Spire.Doc for .NET and follow the steps to extract Word comments. Firstly, initialize a StringBuilder instance to save extracted comments. Secondly, use a foreach statement to get all comments in Word and use another foreach statement to get each paragraph of body of each comment. Then, invoke StringBuilder.AppendLine method to append a copy of comment string followed by default line terminator to the end of the current StringBuilder object. The parameter passed to this method is string value which is comment paragraph text. Thirdly, invoke File.WrtieAllText method to create a new TXT file with comments text as contents. The parameters passed to this method are string path and string contents. Code as following:

[C#]
using System.Text;
using System.IO;
using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;

namespace ExtractComments
{
    class Program
    {
        static void Main(string[] args)
        {
            //Load Document
            Document doc = new Document();
            doc.LoadFromFile(@"E:\Work\Document\A GOOD MAN IS HARD TO FIND.docx");

            //Extract Comment
            StringBuilder SB = new StringBuilder();

            foreach(Comment comment in doc.Comments)
            {
                foreach (Paragraph p in comment.Body.Paragraphs)
                {
                    SB.AppendLine(p.Text);
                }
            }

            //Save to TXT File
            File.WriteAllText("CommentExtraction.txt", SB.ToString());
            System.Diagnostics.Process.Start("CommentExtraction.txt");
        }
    }
}

 

[Visual Basic]
Imports System.Text
Imports System.IO
Imports Spire.Doc
Imports Spire.Doc.Documents
Imports Spire.Doc.Fields

Namespace ExtractComments
    Friend Class Program
        Shared Sub Main(ByVal args() As String)
            'Load Document
            Dim doc As New Document()
            doc.LoadFromFile("E:\Work\Document\A GOOD MAN IS HARD TO FIND.docx")

            'Extract Comment
            Dim SB As New StringBuilder()

            For Each comment As Comment In doc.Comments
                For Each p As Paragraph In comment.Body.Paragraphs
                    SB.AppendLine(p.Text)
                Next p
            Next comment

            'Save to TXT File
            File.WriteAllText("CommentExtraction.txt", SB.ToString())
            System.Diagnostics.Process.Start("CommentExtraction.txt")
        End Sub
    End Class
End Namespace

After debugging, the following result will be presented:

Spire.Doc, a professional Word component, enables developers/programmers to operate Word document, for example, generating, opening, saving and modifying on .NET, WPF and Silverlight applications

Comments in Word are author reviews about specified contents. It can be removed and replaced with other comments for showing different reviews. Spire.Doc for .NET provides several methods with users to remove and replace comments in different situations. The following screenshot is a document with comments without removing and replacing.

Remove Word Comment

Remove

There are three possibilities to remove comments.

  • Remove One Comment: Method Document.Comments.RemoveAt enables users to remove one specified comment item. The index number is passed to this method to confirm which comment will be removed.
  • Remove All Comments: Method Document.Comments.Clear() enables users to remove all comments in loaded Word document.
  • Remove Part of One Comment (Body Paragraph): Firstly, get the specified comment item in document. Secondly, get paragraph collection in comments body. Thirdly, use method ParagraphCollection.RemoveAt method to remove body paragraph. The index number is passed to this method to confirm which paragraph will be removed.

Replace

It means to replace contents of body paragraphs in one specified comments. So at the beginning, the comment item and paragraph item in comment body should be gotten. Method Paragraph.Replace method can be used to replace comments. There are six possibilities this method provides.

  • Replace regular expression pattern with replace string (the new comment).
  • Replace regular expression pattern with text selection.
  • Replace regular expression pattern with text selection taking into consideration of preserving comment formatting.
  • Replace given string (original comment contents) with replace string, taking into consideration of case-sensitive and whole word option.
  • Replace given string with text selection, taking into consideration of case-sensitive and whole word option.
  • Replace give string with text selection, taking into consideration of case-sensitive, whole word options and formatting preservation.

The following example shows how to replace body paragraph of the first comment and remove the whole second comment in the loaded document. Download and install Spire.Doc for .NET and use the following code.

[C#]
using Spire.Doc;

namespace RemoveandReplace
{
    class Program
    {
        static void Main(string[] args)
        {
            //Load Document
            Document document = new Document();
            document.LoadFromFile(@"E:\work\Documents\WordDocuments\A GOOD MAN IS HARD TO FIND.docx");

            //Replace Contents of The First Comment
            document.Comments[0].Body.Paragraphs[0].Replace("It’s a title with Mistral font style.", "This comment is changed.", false, false);

            //Remove The Second Comment
            document.Comments.RemoveAt(1);

            //Save and Launch
            document.SaveToFile("RemoveandReplace.docx", FileFormat.Docx);
            System.Diagnostics.Process.Start("RemoveandReplace.docx");
        }
    }
}
[VB.NET]
Imports Spire.Doc

Namespace RemoveandReplace
    Friend Class Program
        Shared Sub Main(ByVal args() As String)
            'Load Document
            Dim document As New Document()
            document.LoadFromFile("E:\work\Documents\WordDocuments\A GOOD MAN IS HARD TO FIND.docx")

            'Replace Contents of The First Comment
            document.Comments(0).Body.Paragraphs(0).Replace("It’s a title with Mistral font style.", "This comment is changed.", False, False)

            'Remove The Second Comment
            document.Comments.RemoveAt(1)

            'Save and Launch
            document.SaveToFile("RemoveandReplace.docx", FileFormat.Docx)
            System.Diagnostics.Process.Start("RemoveandReplace.docx")
        End Sub
    End Class
End Namespace

After running, you can get result below:

Remove Word Comment

Spire.Doc, a professional Word component, enables developers/programmers perform a wide range of processing tasks, such as generate, write, modify and save for their customize .NET, Silverlight and WPF applications.

The comment feature in Microsoft Word provides an excellent way for people to add their insights or opinions to a Word document without having to change or interrupt the content of the document. If someone comments on a document, the document author or other users can reply to the comment to have a discussion with him, even if they're not viewing the document at the same time. This article will demonstrate how to add, reply to or delete comments in Word in C# and VB.NET using Spire.Doc for .NET library.

Install Spire.Doc for .NET

To begin with, you need to add the DLL files included in the Spire.Doc for.NET package as references in your .NET project. The DLL files can be either downloaded from this link or installed via NuGet.

PM> Install-Package Spire.Doc

Add a Comment to Paragraph in Word in C# and VB.NET

Spire.Doc for .NET provides the Paragraph.AppendComment() method to add a comment to a specific paragraph. The following are the detailed steps:

  • Initialize an instance of the Document class.
  • Load a Word document using Document.LoadFromFile() method.
  • Access a specific section in the document by its index through Document.Sections[int] property.
  • Access a specific paragraph in the section by its index through Section.Paragraphs[int] property.
  • Add a comment to the paragraph using Paragraph.AppendComment() method.
  • Set the author of the comment through Comment.Format.Author property.
  • Save the result document using Document.SaveToFile() method.
  • C#
  • VB.NET
using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;

namespace AddComments
{
    internal class Program
    {
        static void Main(string[] args)
        {
            //Initialize an instance of the Document class
            Document document = new Document();
            //Load a Word document
            document.LoadFromFile(@"Sample.docx");

            //Get the first section in the document
            Section section = document.Sections[0];

            //Get the first paragraph in the section
            Paragraph paragraph = section.Paragraphs[0];
            //Add a comment to the paragraph
            Comment comment = paragraph.AppendComment("This comment is added using Spire.Doc for .NET.");
            //Set comment author
            comment.Format.Author = "Eiceblue";
            comment.Format.Initial = "CM";

            //Save the result document
            document.SaveToFile("AddCommentToParagraph.docx", FileFormat.Docx2013);
            document.Close();
        }
    }
}

C#/VB.NET: Add, Reply to or Delete Comments in Word

Add a Comment to Text in Word in C# and VB.NET

The Paragraph.AppendComment() method is used to add comments to an entire paragraph. By default, the comment marks will be placed at the end of the paragraph. To add a comment to a specific text, you need to search for the text using Document.FindString() method, then place the comment marks at the beginning and end of the text. The following are the detailed steps:

  • Initialize an instance of the Document class.
  • Load a Word document using Document.LoadFromFile() method.
  • Find the specific text in the document using Document.FindString() method.
  • Create a comment start mark and a comment end mark, which will be placed at the beginning and end of the found text respectively.
  • Initialize an instance of the Comment class to create a new comment. Then set the content and author for the comment.
  • Get the owner paragraph of the found text. Then add the comment to the paragraph as a child object.
  • Insert the comment start mark before the text range and the comment end mark after the text range.
  • Save the result document using Document.SaveToFile() method.
  • C#
  • VB.NET
using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;

namespace AddCommentsToText
{
    internal class Program
    {
        static void Main(string[] args)
        {
            //Initialize an instance of the Document class
            Document document = new Document();
            //Load a Word document
            document.LoadFromFile(@"CommentTemplate.docx");

            //Find a specific string
            TextSelection find = document.FindString("Microsoft Office", false, true);

            //Create the comment start mark and comment end mark
            CommentMark commentmarkStart = new CommentMark(document);
            commentmarkStart.Type = CommentMarkType.CommentStart;
            CommentMark commentmarkEnd = new CommentMark(document);
            commentmarkEnd.Type = CommentMarkType.CommentEnd;

            //Create a comment and set its content and author
            Comment comment = new Comment(document);
            comment.Body.AddParagraph().Text = "Developed by Microsoft.";
            comment.Format.Author = "Shaun";

            //Get the found text as a single text range
            TextRange range = find.GetAsOneRange();

            //Get the owner paragraph of the text range
            Paragraph para = range.OwnerParagraph;

            //Add the comment to the paragraph
            para.ChildObjects.Add(comment);

            //Get the index of text range in the paragraph
            int index = para.ChildObjects.IndexOf(range);

            //Set comment ID for the comment mark start and comment mark end
            commentmarkStart.CommentId = comment.Format.CommentId;
            commentmarkEnd.CommentId = comment.Format.CommentId;

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

            //Save the result document
            document.SaveToFile("AddCommentForText.docx", FileFormat.Docx2013);
            document.Close();
        }
    }
}

C#/VB.NET: Add, Reply to or Delete Comments in Word

Reply to a Comment in Word in C# and VB.NET

To add a reply to an existing comment, you can use the Comment.ReplyToComment() method. The following are the detailed steps:

  • Initialize an instance of the Document class.
  • Load a Word document using Document.LoadFromFile() method.
  • Get a specific comment in the document through Document.Comments[int] property.
  • Initialize an instance of the Comment class to create a new comment. Then set the content and author for the comment.
  • Add the new comment as a reply to the specific comment using Comment.ReplyToComment() method.
  • Save the result document using Document.SaveToFile() method.
  • C#
  • VB.NET
using Spire.Doc;
using Spire.Doc.Fields;

namespace ReplyToComments
{
    internal class Program
    {
        static void Main(string[] args)
        {
            //Initialize an instance of the Document class
            Document document = new Document();
            //Load a Word document
            document.LoadFromFile(@"AddCommentToParagraph.docx");

            //Get the first comment in the document
            Comment comment1 = document.Comments[0];

            //Create a new comment and specify its author and content
            Comment replyComment1 = new Comment(document);
            replyComment1.Format.Author = "Michael";
            replyComment1.Body.AddParagraph().AppendText("Spire.Doc is a wonderful Word library.");

            //Add the comment as a reply to the first comment
            comment1.ReplyToComment(replyComment1);

            //Save the result document
            document.SaveToFile("ReplyToComment.docx", FileFormat.Docx2013);
            document.Close();
        }
    }
}

C#/VB.NET: Add, Reply to or Delete Comments in Word

Delete Comments in Word in C# and VB.NET

Spire.Doc for .NET offers the Document.Comments.RemoveAt(int) method to remove a specific comment from a Word document and the Document.Comments.Clear() method to remove all comments from a Word document. The following are the detailed steps:

  • Initialize an instance of the Document class.
  • Load a Word document using Document.LoadFromFile() method.
  • Delete a specific comment or all comments in the document using Document.Comments.RemoveAt(int) method or Document.Comments.Clear() method.
  • Save the result document using Document.SaveToFile() method.
  • C#
  • VB.NET
using Spire.Doc;

namespace DeleteComments
{
    internal class Program
    {
        static void Main(string[] args)
        {
            //Initialize an instance of the Document class
            Document document = new Document();
            //Load a Word document
            document.LoadFromFile(@"AddCommentToParagraph.docx");

            //Delete the first comment in the document
            document.Comments.RemoveAt(0);

            //Delete all comments in the document
            //document.Comments.Clear();

            //Save the result document
            document.SaveToFile("DeleteComment.docx", FileFormat.Docx2013);
            document.Close();
        }
    }
}

C#/VB.NET: Add, Reply to or Delete Comments in Word

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.