Word开发工具Aspose.Words功能演示:使用C ++以编程方式在Word文档中使用注释

在某些情况下,需要以编程方式管理评论。为此,本文将介绍如何使用C ++在Word文档中使用注释。

Microsoft Word使您能够向Word文档添加注释。在诸如建议改进文档或共享文本思想等情况下,注释可能会有所帮助。在某些情况下,需要以编程方式管理评论。为此,本文将教您如何使用C ++在Word文档中使用注释。

让我们探索以下有关的内容:

  • 使用C ++向Word文档添加注释
  • 阅读Word文档中的注释
  • 使用C ++修改Word文档中的注释
  • 使用C ++从Word文档中删除注释

Aspose.Words for C ++ 是本机C ++库,允许您创建,读取,修改和转换Microsoft Word文档。此外,它还支持处理DOCX和DOC文件中的注释。

>>你可以点击这里下载Aspose.Words for C ++ 最新版测试体验。


使用C ++向Word文档添加注释

Aspose.Words for C ++ API提供了添加带有作者姓名,缩写和日期/时间的注释的功能。以下是将注释添加到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开发工具Aspose.Words功能演示:使用C ++以编程方式在Word文档中使用注释

阅读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进行处理,非常感谢!

上一篇 2021年4月5日
下一篇 2021年4月5日

相关推荐

发表回复

登录后才能评论