Microsoft Word provides you the ability to add comments to Word documents. Comments can be helpful in cases such as suggesting improvements in documents or sharing thoughts on the text. There might be situations where you need to manage comments programmatically. To that end, this article will teach you how to work with comments in Word documents using C++.

C++ API for Working with Comments in Word Documents

Aspose.Words for C++ is a native C++ library that allows you to create, read, modify and convert Microsoft Word documents. In addition, it also supports working with comments in DOCX and DOC files. You can either install the API through NuGet or download it directly from the Downloads section.

PM> Install-Package Aspose.Words.Cpp

Add Comments to Word Documents using C++

Aspose.Words for C++ API provides the ability to add comments with the author name, initials, and date/time. The following are the steps to add comments to Word documents.

The following sample code demonstrates how to add comments to Word documents using C++.

// Directory paths.
System::String sourceDataDir = u"SourceDirectory\\";
System::String outputDataDir = u"OutputDirectory\\";
// Load the Word file
System::SharedPtr<Document> doc = System::MakeObject<Document>(sourceDataDir + u"Sample 1.docx");
// Create an instance of the DocumentBuilder class
System::SharedPtr<DocumentBuilder> builder = System::MakeObject<DocumentBuilder>(doc);
// Add comment
System::SharedPtr<Comment> comment = System::MakeObject<Comment>(doc, u"Aspose", u"AFFA", System::DateTime::get_Today());
builder->get_CurrentParagraph()->AppendChild(comment);
comment->get_Paragraphs()->Add(System::MakeObject<Paragraph>(doc));
comment->get_FirstParagraph()->get_Runs()->Add(System::MakeObject<Run>(doc, u"Comment text."));
// Save the document.
doc->Save(outputDataDir + u"AddCommentsToExistingDoc.docx");
view raw Add-Comment.cpp hosted with ❤ by GitHub

The following is the image of the output generated by the sample code.

Image of the output generated by the sample code

Read Comments from a Word Document

The following are the steps to read comments from Word documents.

The following is the sample code to read comments from a Word document using C++.

// Directory paths.
System::String sourceDataDir = u"SourceDirectory\\";
System::String outputDataDir = u"OutputDirectory\\";
// Load the Word file
System::SharedPtr<Document> doc = System::MakeObject<Document>(sourceDataDir + u"SampleComments.docx");
// Retrieve comments
System::SharedPtr<NodeCollection> comments = doc->GetChildNodes(NodeType::Comment, true);
// Loop through all comments
for (System::SharedPtr<Comment> comment : System::IterateOver<System::SharedPtr<Comment>>(comments))
{
// Print comment information
std::cout << comment->get_Author() + u" " + comment->get_DateTime() + u" " + System::StaticCast<Node>(comment)->ToString(SaveFormat::Text);
}

Modify Comments in a Word Document using C++

In order to modify a comment, retrieve it using the NodeCollection->idx_get(int32_t index) method and change it according to your needs. The following are the steps to modify comments in a Word document.

The following sample code shows how to modify comments in a Word document using C++.

// Directory paths.
System::String sourceDataDir = u"SourceDirectory\\";
System::String outputDataDir = u"OutputDirectory\\";
// Load the Word file
System::SharedPtr<Document> doc = System::MakeObject<Document>(sourceDataDir + u"SampleComments.docx");
// Retrieve comments
System::SharedPtr<NodeCollection> comments = doc->GetChildNodes(NodeType::Comment, true);
// Get comment
System::SharedPtr<Comment> comment = System::DynamicCast<Comment>(comments->idx_get(0));
// Update comment text
comment->SetText(u"Updated Text");
// Save the document.
doc->Save(outputDataDir + u"UpdatedComment.docx");

Delete Comments from a Word Document using C++

The Aspose.Words for C++ API provides multiple ways to delete comments from Word documents. In this section, you will learn how to delete a specific comment, comments by author, and all comments using C++.

Delete a Specific Comment

The following are the steps to delete a specific comment.

The following sample code shows how to delete a specific comment from a Word document using C++.

// Directory paths.
System::String sourceDataDir = u"D:\\Work\\Aspose\\01_SourceDirectory\\";
System::String outputDataDir = u"D:\\Work\\Aspose\\02_OutputDirectory\\";
// Load the Word file
System::SharedPtr<Document> doc = System::MakeObject<Document>(sourceDataDir + u"SampleComments.docx");
// Retrieve comments
System::SharedPtr<NodeCollection> comments = doc->GetChildNodes(NodeType::Comment, true);
// Get comment
System::SharedPtr<Comment> comment = System::DynamicCast<Comment>(comments->idx_get(2));
// Remove comment
comment->Remove();
// Save the document.
doc->Save(outputDataDir + u"DeleteSpecificComments.docx");

Deleting Comments by Author

The following are the steps to delete comments by author.

The following is the sample code to delete comments by author using C++.

// Directory paths.
System::String sourceDataDir = u"SourceDirectory\\";
System::String outputDataDir = u"OutputDirectory\\";
// Load the Word file
System::SharedPtr<Document> doc = System::MakeObject<Document>(sourceDataDir + u"SampleComments.docx");
// Retrieve comments
System::SharedPtr<NodeCollection> comments = doc->GetChildNodes(NodeType::Comment, true);
// Loop through all comments and remove those written by the "Aspose" author.
for (int32_t i = comments->get_Count() - 1; i >= 0; i--)
{
System::SharedPtr<Comment> comment = System::DynamicCast<Comment>(comments->idx_get(i));
if (comment->get_Author() == u"Aspose")
{
comment->Remove();
}
}
// Save the document.
doc->Save(outputDataDir + u"DeleteCommentsByAuthor.docx");

Delete All Comments

Instead of deleting individual comments, you can delete all the comments at once using the NodeCollection->Clear() method. The following are the steps to delete all comments from a Word document.

The following sample code demonstrates how to delete all the comments from a Word document using C++.

// Directory paths.
System::String sourceDataDir = u"SourceDirectory\\";
System::String outputDataDir = u"OutputDirectory\\";
// Load the Word file
System::SharedPtr<Document> doc = System::MakeObject<Document>(sourceDataDir + u"SampleComments.docx");
// Retrieve comments
System::SharedPtr<NodeCollection> comments = doc->GetChildNodes(NodeType::Comment, true);
// Remove all comments.
comments->Clear();
// Save the document.
doc->Save(outputDataDir + u"DeleteAllComments.docx");

Get a Free License

You can try the API without evaluation limitations by requesting a free temporary license.

Conclusion

In this article, you have learned how to work with comments in Word documents using C++. Specifically, you have learned how to add, read, edit and delete comments. Furthermore, you have seen how to delete a specific comment, comments by author, and all comments using Aspose.Words for C++ API. The API provides a vast number of additional features for automating your Word-related tasks. You can explore the API in detail by visiting the official documentation. In case of any questions, please feel free to reach us on our free support forum.

See Also