没有找到合适的产品?
联系客服协助选型:023-68661681
提供3000多款全球软件/控件产品
针对软件研发的各个阶段提供专业培训与技术咨询
根据客户需求提供定制化的软件开发服务
全球知名设计软件,显著提升设计质量
打造以经营为中心,实现生产过程透明化管理
帮助企业合理产能分配,提高资源利用率
快速打造数字化生产线,实现全流程追溯
生产过程精准追溯,满足企业合规要求
以六西格玛为理论基础,实现产品质量全数字化管理
通过大屏电子看板,实现车间透明化管理
对设备进行全生命周期管理,提高设备综合利用率
实现设备数据的实时采集与监控
利用数字化技术提升油气勘探的效率和成功率
钻井计划优化、实时监控和风险评估
提供业务洞察与决策支持实现数据驱动决策
翻译|使用教程|编辑:胡涛|2022-10-12 13:32:25.767|阅读 237 次
概述:本文将教您如何使用 C++ 处理 Word 文件中的目录,欢迎查阅~
# 界面/图表报表/文档/IDE等千款热门软控件火热销售中 >>
相关链接:
Aspose.Words 是一种高级Word文档处理API,用于执行各种文档管理和操作任务。API支持生成,修改,转换,呈现和打印文档,而无需在跨平台应用程序中直接使用Microsoft Word。此外,API支持所有流行的Word处理文件格式,并允许将Word文档导出或转换为固定布局文件格式和最常用的图像/多媒体格式。
目录 (TOC) 是 Word 文档的重要组成部分。它提供文档内容的概述,并允许您快速导航到所需的部分。您可能会发现自己需要以编程方式从 Word 文档中添加、提取、更新或删除目录。为此,本文将教您如何使用 C++ 处理 Word 文件中的目录。
Aspose.Words for C++是一个原生 C++ 库,允许您创建、阅读、修改和转换 Microsoft Word 文档。此外,它还支持处理 Word 文件中的目录。您可以通过NuGet安装 API,也可以直接从“下载”部分下载。
PM> Install-Package Aspose.Words.Cpp
以下是在 Word 文档中添加目录的步骤。
以下示例代码展示了如何使用 C++ 在 Word 文档中添加目录。
// Source and output 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 5.docx"); // Create an instance of the DocumentBuilder class System::SharedPtr<DocumentBuilder> builder = System::MakeObject<DocumentBuilder>(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 path System::String outputPath = outputDataDir + u"AddTOC.docx"; // Save the Word file doc->Save(outputPath);
以下是从 Word 文档中提取目录的步骤。
以下示例代码演示了如何使用 C++ 从 Word 文档中提取目录。
// Source direvctory
System::String inputDataDir = u"SourceDirectory\\";
// Load the Word file
System::SharedPtr<Document> doc = System::MakeObject<Document>(inputDataDir + u"SampleTOC.docx");
// Loop through the fields
for (System::SharedPtr<Field> field : System::IterateOver(doc->get_Range()->get_Fields()))
{
// Get FieldHyperlink fields
if (field->get_Type() == FieldType::FieldHyperlink)
{
System::SharedPtr<FieldHyperlink> hyperlink = System::DynamicCast<FieldHyperlink>(field);
// Check if field belongs to TOC
if (hyperlink->get_SubAddress() != nullptr && hyperlink->get_SubAddress().StartsWith(u"_Toc"))
{
System::SharedPtr<Paragraph> tocItem = System::DynamicCast<Paragraph>(field->get_FieldStart()->GetAncestor(NodeType::Paragraph));
std::cout << System::StaticCast<Node>(tocItem)->ToString(SaveFormat::Text).Trim().ToUtf8String() << std::endl;
std::cout << "------------------" << std::endl;
if (tocItem != nullptr)
{
System::SharedPtr<Bookmark> bm = doc->get_Range()->get_Bookmarks()->idx_get(hyperlink->get_SubAddress());
// Get the location this TOC Item is pointing to
System::SharedPtr<Paragraph> pointer = System::DynamicCast<Paragraph>(bm->get_BookmarkStart()->GetAncestor(NodeType::Paragraph));
std::cout << System::StaticCast<Node>(pointer)->ToString(SaveFormat::Text).ToUtf8String() << std::endl;
}
}
}
}
如果文档的内容已经更新,并且您需要在目录中反映这些更改,您只需加载 Word 文件并调用方法。该方法会根据修改后的内容更新目录。在此之后,保存更新的 Word 文档。
以下是从 Word 文档中删除目录的步骤。
以下示例代码显示如何使用 C++ 从 Word 文档中删除目录。
void RemoveTableOfContents(const System::SharedPtr<Document>& doc, int32_t index)
{
// Store the FieldStart nodes of TOC fields in the document for quick access.
std::vector<System::SharedPtr<FieldStart>> fieldStarts;
// This is a list to store the nodes found inside the specified TOC. They will be removed
// at the end of this method.
std::vector<System::SharedPtr<Node>> nodeList;
for (System::SharedPtr<FieldStart> start : System::IterateOver<System::SharedPtr<FieldStart>>(doc->GetChildNodes(NodeType::FieldStart, true)))
{
if (start->get_FieldType() == FieldType::FieldTOC)
{
// Add all FieldStarts which are of type FieldTOC.
fieldStarts.push_back(start);
}
}
// Ensure that the TOC specified by the passed index exists.
if (index > fieldStarts.size() - 1)
{
throw System::ArgumentOutOfRangeException(u"TOC index is out of range");
}
bool isRemoving = true;
// Get the FieldStart of the specified TOC.
System::SharedPtr<Node> currentNode = fieldStarts[index];
while (isRemoving)
{
// It is safer to store these nodes and delete them all at once later.
nodeList.push_back(currentNode);
currentNode = currentNode->NextPreOrder(doc);
// Once we encounter a FieldEnd node of type FieldTOC then we know we are at the end
// of the current TOC and we can stop here.
if (currentNode->get_NodeType() == NodeType::FieldEnd)
{
System::SharedPtr<FieldEnd> fieldEnd = System::DynamicCast<FieldEnd>(currentNode);
if (fieldEnd->get_FieldType() == FieldType::FieldTOC)
{
isRemoving = false;
}
}
}
// Remove all nodes found in the specified TOC.
for (System::SharedPtr<Node> node : nodeList)
{
node->Remove();
}
}
int main()
{
// Source and output directory paths.
System::String sourceDataDir = u"SourceDirectory\\";
System::String outputDataDir = u"OutputDirectory\\";
// Open a Word document
System::SharedPtr<Document> doc = System::MakeObject<Document>(sourceDataDir + u"SampleTOC.docx");
// Remove the first table of contents from the document.
RemoveTableOfContents(doc, 0);
// Output file path
System::String outputPath = outputDataDir + u"RemoveTOC.docx";
// Save the Word file
doc->Save(outputPath);
}
以上便是使用 C++ 在 Word 文档中添加或删除页眉和页脚详细步骤 ,要是您还有其他关于产品方面的问题,欢迎咨询我们,或者加入我们官方技术交流群。
欢迎下载|体验更多Aspose产品
本站文章除注明转载外,均为本站原创或翻译。欢迎任何形式的转载,但请务必注明出处、不得修改原文相关链接,如果存在内容上的异议请邮件反馈至chenjj@ke049m.cn




BarTender 标签管理系统,正是帮助企业轻松实现 GS1 标准化标签设计、编码生成与信息联动的强大工具。
Parasoft C/C++test 是一款功能强大的 C/C++ 软件测试工具,集成了静态代码分析、单元测试、集成测试和覆盖率分析等功能,单元测试作为其关键功能之一,为了适配多样化的目标部署环境,C/C++test 设计了灵活的测试结果收集机制。通过Socket通讯方式来收集单元测试结果,从而扩展其测试覆盖范围与应用场景。
Parasoft C/C++test作为一款功能全面的自动化代码质量保障工具,为C/C++开发者提供了静态分析、单元测试和运行时错误检测等核心功能。通过将其与轻量级且广受欢迎的VScode编辑器集成,开发团队可以在熟悉的编码环境中实时获得代码质量反馈,有效提升开发效率与代码可靠性。
本文主要介绍如何在MVVM应用程序中使用虚拟源,欢迎下载最新版组件体验!
相关产品
专业的电子表格控件,无需MS Excel也可满足一切Excel表格功能。
Aspose.Words for .NET无需Microsoft Word也可在任何平台上满足Word文档的一切操作需求。
Aspose.PDF for .NETPDF文档创建组件,无需Adobe Acrobat,也可以在任何平台上操作PDF文档。
最新文章 MORE
半岛外围网上直营相关的文章 MORE
服务电话
重庆/ 023-68661681
华东/ 13452821722
华南/ 18100878085
华北/ 17347785263
客户支持
技术支持咨询服务
服务热线:400-700-1020
邮箱:sales@ke049m.cn
关注我们
地址 : 重庆市九龙坡区火炬大道69号6幢
半岛外围网上直营