让我们探索以下有关的内容:
- 使用C ++向Word文档添加注释
- 阅读Word文档中的注释
- 使用C ++修改Word文档中的注释
- 使用C ++从Word文档中删除注释
Aspose.Words for C ++ 是本机C ++库,允许您创建,读取,修改和转换Microsoft Word文档。此外,它还支持处理DOCX和DOC文件中的注释。
>>你可以点击这里下载Aspose.Words for C ++ 最新版测试体验。
使用C ++向Word文档添加注释
- 首先,使用Document类加载Word文档。
- 通过传递在上一步中创建的Document对象,创建DocumentBuilder类的实例。
- 使用Comment类创建注释。
- 使用DocumentBuilder-> get_CurrentParagraph()-> AppendChild(System :: SharedPtrnewChild)方法将注释添加到文档中。
- 最后,使用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::SharedPtrdoc = System::MakeObject(sourceDataDir + u"SampleComments.docx");// Retrieve commentsSystem::SharedPtrcomments = doc->GetChildNodes(NodeType::Comment, true);// Loop through all commentsfor (System::SharedPtrcomment : System::IterateOver(comments)){// Print comment informationstd::cout << comment->get_Author() + u" " + comment->get_DateTime() + u" " + System::StaticCast(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::SharedPtrdoc = System::MakeObject(sourceDataDir + u"SampleComments.docx");// Retrieve commentsSystem::SharedPtrcomments = doc->GetChildNodes(NodeType::Comment, true);// Get commentSystem::SharedPtrcomment = System::DynamicCast(comments->idx_get(0));// Update comment textcomment->SetText(u"Updated Text");// Save the document.doc->Save(outputDataDir + u"UpdatedComment.docx");
使用C ++从Word文档中删除注释
可以使用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::SharedPtrdoc = System::MakeObject(sourceDataDir + u"SampleComments.docx");// Retrieve commentsSystem::SharedPtrcomments = doc->GetChildNodes(NodeType::Comment, true);// Remove all comments.comments->Clear();// Save the document.doc->Save(outputDataDir + u"DeleteAllComments.docx");
以下是删除特定注释的步骤。
- 使用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::SharedPtrdoc = System::MakeObject(sourceDataDir + u"SampleComments.docx");// Retrieve commentsSystem::SharedPtrcomments = doc->GetChildNodes(NodeType::Comment, true);// Get commentSystem::SharedPtrcomment = System::DynamicCast(comments->idx_get(2));// Remove commentcomment->Remove();// Save the document.doc->Save(outputDataDir + u"DeleteSpecificComments.docx");
如果你想试用Aspose的全部完整功能,可联系在线客服获取30天临时授权体验。
还想要更多吗可以点击阅读【Aspose最新资源在线文库】,查找需要的教程资源。如果您有任何疑问或需求,请随时加入Aspose技术交流群(),我们很高兴为您提供查询和咨询。
标签:
声明:本站部分文章及图片源自用户投稿,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!