启明办公

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 58|回复: 0

Word开发工具Aspose.Words功能演示:使用C ++以编程 ...

[复制链接]

1

主题

2

帖子

4

积分

新手上路

Rank: 1

积分
4
发表于 2022-12-17 20:28:43 | 显示全部楼层 |阅读模式
​---------------------------------Aspose技术交流群(761297826)----------------------------—
Microsoft Word使您能够向Word文档添加注释。在诸如建议改进文档或共享文本思想等情况下,注释可能会有所帮助。在某些情况下,需要以编程方式管理评论。为此,本文将教您如何使用C ++在Word文档中使用注释。
让我们探索以下有关的内容:

  • 使用C ++向Word文档添加注释
  • 阅读Word文档中的注释
  • 使用C ++修改Word文档中的注释
  • 使用C ++从Word文档中删除注释
Aspose.Words for C ++ 是本机C ++库,允许您创建,读取,修改和转换Microsoft Word文档。此外,它还支持处理DOCX和DOC文件中的注释。
使用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 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::SharedPtrdoc = System::MakeObject(sourceDataDir + u"SampleComments.docx");

// Retrieve comments
System::SharedPtrcomments = doc->GetChildNodes(NodeType::Comment, true);

// Loop through all comments
for (System::SharedPtrcomment : System::IterateOver(comments))
{
        // Print comment information
        std::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 file
System::SharedPtrdoc = System::MakeObject(sourceDataDir + u"SampleComments.docx");

// Retrieve comments
System::SharedPtrcomments = doc->GetChildNodes(NodeType::Comment, true);

// Get comment
System::SharedPtrcomment = System::DynamicCast(comments->idx_get(0));

// Update comment text
comment->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 file
System::SharedPtrdoc = System::MakeObject(sourceDataDir + u"SampleComments.docx");

// Retrieve comments
System::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 file
System::SharedPtrdoc = System::MakeObject(sourceDataDir + u"SampleComments.docx");

// Retrieve comments
System::SharedPtrcomments = doc->GetChildNodes(NodeType::Comment, true);

// Get comment
System::SharedPtrcomment = System::DynamicCast(comments->idx_get(2));

// Remove comment
comment->Remove();

// Save the document.
doc->Save(outputDataDir + u"DeleteSpecificComments.docx");
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|启明办公

Copyright © 2001-2013 Comsenz Inc.Template by Comsenz Inc.All Rights Reserved.

Powered by Discuz!X3.4

快速回复 返回顶部 返回列表