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
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.
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"); } } }
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:
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.