Word开发工具Aspose.Words功能演示:使用C ++在Word文档中使用目录

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

  • 在Word文档中添加目录
  • 从Word文档中提取目录
  • 更新Word文档中的目录
  • 从Word文档中删除目录

Aspose.Words for C ++ 是本机C ++库,允许您创建,读取,修改和转换Microsoft Word文档。此外,它还支持使用Word文件中的目录。

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


在Word文档中添加目录

以下是在Word文档中添加目录的步骤。

  • 使用Document 类加载Word文件 。
  • 使用先前创建的Document对象创建DocumentBuilder类的实例 。
  • 使用DocumentBuilder-> InsertTableOfContents(System :: String开关)方法插入目录。
  • 使用Document-> UpdateFields()方法填充目录。
  • 使用Document-> Save(System :: String fileName)方法保存Word文档。

下面的示例代码显示了如何使用C ++在Word文档中添加目录。

// Source and output directory paths.System::String sourceDataDir = u"SourceDirectory\";System::String outputDataDir = u"OutputDirectory\";// Load the Word fileSystem::SharedPtrdoc = System::MakeObject(sourceDataDir + u"Sample 5.docx");// Create an instance of the DocumentBuilder classSystem::SharedPtrbuilder = System::MakeObject(doc);// Insert a table of contents at the beginning of the document.builder->InsertTableOfContents(u"\o "1-3"" \h \z \u"");// The newly inserted table of contents will be initially empty.// It needs to be populated by updating the fields in the document.doc->UpdateFields();// Output file pathSystem::String outputPath = outputDataDir + u""AddTOC.docx"";// Save the Word filedoc->Save(outputPath);

Word开发工具Aspose.Words功能演示:使用C ++在Word文档中使用目录

从Word文档中提取目录

以下是从Word文档中提取目录的步骤。

  • 使用Document 类加载Word文件 。
  • 使用Document-> get_Range()-> get_Fields()方法检索字段并在它们上循环。
  • 检查字段是否为FieldType :: FieldHyperlink类型。
  • 检查该字段是否属于目录。
  • 检索并打印字段信息。

下面的示例代码演示了如何使用C ++从Word文档中提取目录。

// Source direvctorySystem::String inputDataDir = u""SourceDirectory\"";// Load the Word fileSystem::SharedPtrdoc = System::MakeObject(inputDataDir + u""SampleTOC.docx"");// Loop through the fieldsfor (System::SharedPtrfield : System::IterateOver(doc->get_Range()->get_Fields())){// Get FieldHyperlink fieldsif (field->get_Type() == FieldType::FieldHyperlink){System::SharedPtrhyperlink = System::DynamicCast(field);// Check if field belongs to TOCif (hyperlink->get_SubAddress() != nullptr && hyperlink->get_SubAddress().StartsWith(u""_Toc"")){System::SharedPtrtocItem = System::DynamicCast(field->get_FieldStart()->GetAncestor(NodeType::Paragraph));std::cout << System::StaticCast(tocItem)->ToString(SaveFormat::Text).Trim().ToUtf8String() << std::endl; std::cout << ""------------------"" << std::endl; if (tocItem != nullptr) { System::SharedPtrbm = doc->get_Range()->get_Bookmarks()->idx_get(hyperlink->get_SubAddress());// Get the location this TOC Item is pointing toSystem::SharedPtrpointer = System::DynamicCast(bm->get_BookmarkStart()->GetAncestor(NodeType::Paragraph));std::cout << System::StaticCast(pointer)->ToString(SaveFormat::Text).ToUtf8String() << std::endl; } } } } 

更新Word文档中的目录

如果文档的内容已更新,并且您需要在目录中反映这些更改,则只需加载Word文件并调用 Document-> UpdateFields() 方法。此方法将根据修改后的内容更新目录。之后,保存更新的Word文档。

以下是从Word文档中删除目录的步骤。

  • 使用Document 类加载Word文件 。
  • 检索并存储FieldStart节点的列表。
  • 遍历节点,直到到达指定目录结尾的类型NodeType :: FieldEnd的节点。
  • 使用Node-> Remove()方法删除目录。
  • 使用Document-> Save(System :: String fileName)方法保存Word文档。

下面的示例代码显示了如何使用C ++从Word文档中删除目录。

void RemoveTableOfContents(const System::SharedPtr& doc

                                                        

声明:本站部分文章及图片源自用户投稿,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!

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

相关推荐

发表回复

登录后才能评论