Aspose.Words 是一种高级Word文档处理API,用于执行各种文档管理和操作任务。API支持生成,修改,转换,呈现和打印文档,而无需在跨平台应用程序中直接使用Microsoft Word。此外,API支持所有流行的Word处理文件格式,并允许将Word文档导出或转换为固定布局文件格式和最常用的图像/多媒体格式。
Aspose API支持流行文件格式处理,并允许将各类文档导出或转换为固定布局文件格式和最常用的图像/多媒体格式。
Aspose.words 最新下载(qun:761297826)https://www.evget.com/product/4116/download
Microsoft Word 使您能够向 Word 文档添加注释。在建议改进文档或分享对文本的想法等情况下,评论可能会有所帮助。在某些情况下,您可能需要以编程方式管理评论。为此,本文将教您如何使用 C++ 处理 Word 文档中的注释。
一、下载用于处理 Word 文档中的注释的 C++ API
Aspose.Words for C++是一个原生 C++ 库,允许您创建、阅读、修改和转换 Microsoft Word 文档。此外,它还支持处理DOCX和DOC文件中的注释。您可以通过NuGet安装 API,也可以直接从“下载”部分下载。
PM> Install-Package Aspose.Words.Cpp
二、使用 C++ 向 Word 文档添加注释
Aspose.Words for C++ API 提供了添加带有作者姓名、首字母缩写和日期/时间的评论的能力。以下是向 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 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");
以下是示例代码生成的输出图像。
三、从 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 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); }
四、使用 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 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");
五、使用 C++ 从 Word 文档中删除注释
Aspose.Words for C++ API 提供了多种从 Word 文档中删除注释的方法。在本节中,您将学习如何使用 C++ 删除特定评论、作者评论和所有评论。
- 删除特定评论
- 按作者删除评论
- 删除所有评论
删除特定评论
以下是删除特定评论的步骤。
- 使用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 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");
按作者删除评论
以下是按作者删除评论的步骤。
- 首先,使用Document类加载 Word 文档。
- 使用Document->GetChildNodes(Aspose::Words::NodeType nodeType, bool isDeep)方法检索注释。
- 循环浏览评论。
- 在循环中,检索评论并比较其作者。如果作者匹配,请删除评论。
- 最后,使用Document->Save(System::String fileName)方法保存文档。
以下是作者使用 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");
删除所有评论
您可以使用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 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");
以上便是使用 C++ 处理 Word 文档中的注释详细步骤 ,要是您还有其他关于产品方面的问题,欢迎咨询我们,或者加入我们官方技术交流群。