Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aspose-com-gists/efb4e75bba0cdfe538fc9c701bc804af to your computer and use it in GitHub Desktop.
Save aspose-com-gists/efb4e75bba0cdfe538fc9c701bc804af to your computer and use it in GitHub Desktop.
Working with comments in Word documents using C++
Examples for working with comments in 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");
// 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");
// 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");
// 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");
// 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);
}
// 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");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment