|
---------------------------------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&#34;Sample 1.docx&#34;);
// 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&#34;Aspose&#34;, u&#34;AFFA&#34;, 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&#34;Comment text.&#34;));
// Save the document.
doc->Save(outputDataDir + u&#34;AddCommentsToExistingDoc.docx&#34;);下面是示例代码生成的输出图像。

阅读Word文档中的注释
以下是从Word文档中读取注释的步骤。
- 使用Document类加载Word文档。
- 使用Document-> GetChildNodes(Aspose :: Words :: NodeType nodeType,bool isDeep)方法检索注释。
- 遍历注释并检索单个注释的信息。
以下是使用C ++从Word文档中读取注释的示例代码。
// Directory paths.
System::String sourceDataDir = u&#34;SourceDirectory\\&#34;;
System::String outputDataDir = u&#34;OutputDirectory\\&#34;;
// Load the Word file
System::SharedPtrdoc = System::MakeObject(sourceDataDir + u&#34;SampleComments.docx&#34;);
// 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&#34; &#34; + comment->get_DateTime() + u&#34; &#34; + 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&#34;SourceDirectory\\&#34;;
System::String outputDataDir = u&#34;OutputDirectory\\&#34;;
// Load the Word file
System::SharedPtrdoc = System::MakeObject(sourceDataDir + u&#34;SampleComments.docx&#34;);
// 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&#34;Updated Text&#34;);
// Save the document.
doc->Save(outputDataDir + u&#34;UpdatedComment.docx&#34;);使用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&#34;SourceDirectory\\&#34;;
System::String outputDataDir = u&#34;OutputDirectory\\&#34;;
// Load the Word file
System::SharedPtrdoc = System::MakeObject(sourceDataDir + u&#34;SampleComments.docx&#34;);
// Retrieve comments
System::SharedPtrcomments = doc->GetChildNodes(NodeType::Comment, true);
// Remove all comments.
comments->Clear();
// Save the document.
doc->Save(outputDataDir + u&#34;DeleteAllComments.docx&#34;);以下是删除特定注释的步骤。
- 使用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&#34;D:\\Work\\Aspose\\01_SourceDirectory\\&#34;;
System::String outputDataDir = u&#34;D:\\Work\\Aspose\\02_OutputDirectory\\&#34;;
// Load the Word file
System::SharedPtrdoc = System::MakeObject(sourceDataDir + u&#34;SampleComments.docx&#34;);
// 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&#34;DeleteSpecificComments.docx&#34;); |
|