Word处理控件Aspose.Words功能演示:使用 C++ 处理 Word 文档中的注释

Aspose.Words 是一种高级Word文档处理API,用于执行各种文档管理和操作任务。API支持生成,修改,转换,呈现和打印文档,而无需在跨平台应用程序中直接使用Microsoft Word。此外,API支持所有流行的Word处理文件格式,并允许将Word文档导出或转换为固定布局文件格式和最常用的图像/多媒体格式。 

Aspose API支持流行文件格式处理,并允许将各类文档导出或转换为固定布局文件格式和最常用的图像/多媒体格式。

Aspose.words 最新下载(qun:761297826)icon-default.png?t=N0U7https://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 文档中的注释详细步骤 ,要是您还有其他关于产品方面的问题,欢迎咨询我们,或者加入我们官方技术交流群。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
“com.aspose.words.filecorruptedexception: the document appears to be corrupte” 是一个在使用Aspose.Words操作文档时可能会遇到的异常错误。这个错误表示文档看起来是损坏的。 当我们使用Aspose.Words处理文档时,可能会发生一些不可预料的问题。如果文档的结构或内容不符合Aspose.Words所期望的格式,就会引发“文件损坏异常”。这可能是因为文档本身已经损坏或损坏,或者文档格式不符合Aspose的规范要求。 要解决这个问题,可以尝试以下几个方案: 1. 确保你的Aspose.Words的版本是最新的。有时候这个问题可能是由于旧版本的Aspose.Words引起的,更新到最新版本可能会解决该问题。 2. 检查文档本身是否损坏。你可以尝试使用其他文档查看器或编辑器打开该文档,看是否可以正常访问。如果是损坏的,那么你可能需要找到一个可用的备份文件。 3. 检查文档的格式是否符合Aspose.Words的要求。Aspose.Words文档的格式和内容有一些特定的要求,例如支持的文件类型、支持的文档版本等。确保你的文档符合Aspose.Words的要求,如果不符合,可以尝试转换文档格式或升级文档版本。 4. 如果以上方法仍然无法解决问题,可以联系Aspose的技术支持团队,向他们提供详细的错误信息和问题描述,以便他们能够更深入地帮助解决。 总之,“com.aspose.words.filecorruptedexception: the document appears to be corrupte” 这个异常表示文档看起来是损坏的,可能是由于文档本身损坏或格式不符合Aspose.Words的要求所致。通过更新Aspose.Words版本、检查文档的完整性和格式,或与Aspose的技术支持团队联系,我们有望解决该问题。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值