Last active
May 5, 2021 02:30
Working with comments in Word documents using C++
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Examples for working with comments in Word documents using C++ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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