一、下载用于处理 Word 文档中的注释的 C++ API
Aspose.Words for C++是一个原生 C++ 库,允许您创建、阅读、修改和转换 Microsoft Word 文档。此外,它还支持处理DOCX和DOC文件中的注释。您可以通过NuGet安装 API,也可以直接从“下载”部分下载。
PM> Install-Package Aspose.Words.Cpp
二、使用 C++ 向 Word 文档添加注释
- 首先,使用Document类加载 Word 文档。
- 通过传递在上一步中创建的Document对象来创建DocumentBuilder类的实例。
- 使用Comment类创建评论。
- 使用DocumentBuilder->get_CurrentParagraph()->AppendChild(System::SharedPtr Aspose::Words::Node newChild) 方法向文档添加注释。
- 最后,使用Document->Save(System::String fileName)方法保存文档。
以下示例代码演示了如何使用 C++ 向 Word 文档添加注释。
// Directory paths.System::String sourceDataDir = u"SourceDirectory\";System::String outputDataDir = u"OutputDirectory\";// Load the Word fileSystem::SharedPtr<Document> doc = System::MakeObject<Document>(sourceDataDir + u"Sample 1.docx");// Create an instance of the DocumentBuilder classSystem::SharedPtr<DocumentBuilder> builder = System::MakeObject<DocumentBuilder>(doc);// Add commentSystem::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");
以下是示例代码生成的输出图像。

三、从 Word 文档中读取评论
以下是从 Word 文档中读取注释的步骤。
- 使用Document类加载 Word 文档。
- 使用Document->GetChildNodes(Aspose::Words::NodeType nodeType, bool isDeep)方法检索注释。
- 遍历评论并检索各个评论的信息。
以下是使用 C++ 从 Word 文档中读取注释的示例代码。
// Directory paths.System::String sourceDataDir = u"SourceDirectory\";System::String outputDataDir = u"OutputDirectory\";// Load the Word fileSystem::SharedPtr<Document> doc = System::MakeObject<Document>(sourceDataDir + u"SampleComments.docx");// Retrieve commentsSystem::SharedPtr<NodeCollection> comments = doc->GetChildNodes(NodeType::Comment, true);// Loop through all commentsfor (System::SharedPtr<Comment> comment : System::IterateOver<System::SharedPtr<Comment>>(comments)){// Print comment informationstd::cout << comment->get_Author() + u" " + comment->get_DateTime() + u" " + System::StaticCast<Node>(comment)->ToString(SaveFormat::Text);}
四、使用 C++ 修改 Word 文档中的注释
要修改评论,请使用NodeCollection->idx_get(int32_t index)方法检索它并根据需要进行更改。以下是修改 Word 文档中的注释的步骤。
- 首先,使用Document类加载 Word 文档。
- 使用Document->GetChildNodes(Aspose::Words::NodeType nodeType, bool isDeep)方法检索注释。
- 使用NodeCollection->idx_get(int32_t index)方法获取所需的评论并将结果转换为Comment。
- 更新评论。
- 最后,使用Document->Save(System::String fileName)方法保存文档。
以下示例代码展示了如何使用 C++ 修改 Word 文档中的注释。
// Directory paths.System::String sourceDataDir = u"SourceDirectory\";System::String outputDataDir = u"OutputDirectory\";// Load the Word fileSystem::SharedPtr<Document> doc = System::MakeObject<Document>(sourceDataDir + u"SampleComments.docx");// Retrieve commentsSystem::SharedPtr<NodeCollection> comments = doc->GetChildNodes(NodeType::Comment, true);// Get commentSystem::SharedPtr<Comment> comment = System::DynamicCast<Comment>(comments->idx_get(0));// Update comment textcomment->SetText(u"Updated Text");// Save the document.doc->Save(outputDataDir + u"UpdatedComment.docx");
五、使用 C++ 从 Word 文档中删除注释
- 删除特定评论
- 删除所有评论
删除特定评论
以下是删除特定评论的步骤。
- 使用Document类加载 Word 文档。
- 使用Document->GetChildNodes(Aspose::Words::NodeType nodeType, bool isDeep)方法检索注释。
- 使用NodeCollection->idx_get(int32_t index)方法获取要删除的评论并将结果转换为Comment。
- 使用Comment->Remove()方法删除评论。
- 使用Document->Save(System::String fileName)方法保存文档。
以下示例代码显示如何使用 C++ 从 Word 文档中删除特定注释。
// Directory paths.System::String sourceDataDir = u"D:\Work\Aspose\01_SourceDirectory\";System::String outputDataDir = u"D:\Work\Aspose\02_OutputDirectory\";// Load the Word fileSystem::SharedPtr<Document> doc = System::MakeObject<Document>(sourceDataDir + u"SampleComments.docx");// Retrieve commentsSystem::SharedPtr<NodeCollection> comments = doc->GetChildNodes(NodeType::Comment, true);// Get commentSystem::SharedPtr<Comment> comment = System::DynamicCast<Comment>(comments->idx_get(2));// Remove commentcomment->Remove();// Save the document.doc->Save(outputDataDir + u"DeleteSpecificComments.docx");
- 首先,使用Document类加载 Word 文档。
- 使用Document->GetChildNodes(Aspose::Words::NodeType nodeType, bool isDeep)方法检索注释。
- 循环浏览评论。
- 最后,使用Document->Save(System::String fileName)方法保存文档。
// Directory paths.System::String sourceDataDir = u"SourceDirectory\";System::String outputDataDir = u"OutputDirectory\";// Load the Word fileSystem::SharedPtr<Document> doc = System::MakeObject<Document>(sourceDataDir + u"SampleComments.docx");// Retrieve commentsSystem::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");
删除所有评论
您可以使用NodeCollection->Clear()方法一次删除所有评论,而不是删除单个评论。以下是从 Word 文档中删除所有评论的步骤。
- 使用Document类加载 Word 文档。
- 使用Document->GetChildNodes(Aspose::Words::NodeType nodeType, bool isDeep)方法检索注释。
- 使用NodeCollection->Clear()方法删除所有注释。
- 最后,使用Document->Save(System::String fileName)方法保存文档。
以下示例代码演示了如何使用 C++ 从 Word 文档中删除所有注释。
// Directory paths.System::String sourceDataDir = u"SourceDirectory\";System::String outputDataDir = u"OutputDirectory\";// Load the Word fileSystem::SharedPtr<Document> doc = System::MakeObject<Document>(sourceDataDir + u"SampleComments.docx");// Retrieve commentsSystem::SharedPtr<NodeCollection> comments = doc->GetChildNodes(NodeType::Comment, true);// Remove all comments.comments->Clear();// Save the document.doc->Save(outputDataDir + u"DeleteAllComments.docx");
以上便是使用 C++ 处理 Word 文档中的注释详细步骤,要是您还有其他关于产品方面的问题,欢迎咨询我们,或者加入我们官方技术交流群。
欢迎下载|体验更多Aspose产品
点此获取更多Aspose产品信息 或 加入Aspose技术交流群()
标签:
声明:本站部分文章及图片源自用户投稿,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!