翻譯|使用教程|編輯:胡濤|2022-10-12 13:32:25.767|閱讀 226 次
概述:本文將教您如何使用 C++ 處理 Word 文件中的目錄,歡迎查閱~
# 界面/圖表報(bào)表/文檔/IDE等千款熱門(mén)軟控件火熱銷(xiāo)售中 >>
相關(guān)鏈接:
Aspose.Words 是一種高級(jí)Word文檔處理API,用于執(zhí)行各種文檔管理和操作任務(wù)。API支持生成,修改,轉(zhuǎn)換,呈現(xiàn)和打印文檔,而無(wú)需在跨平臺(tái)應(yīng)用程序中直接使用Microsoft Word。此外,API支持所有流行的Word處理文件格式,并允許將Word文檔導(dǎo)出或轉(zhuǎn)換為固定布局文件格式和最常用的圖像/多媒體格式。
目錄 (TOC) 是 Word 文檔的重要組成部分。它提供文檔內(nèi)容的概述,并允許您快速導(dǎo)航到所需的部分。您可能會(huì)發(fā)現(xiàn)自己需要以編程方式從 Word 文檔中添加、提取、更新或刪除目錄。為此,本文將教您如何使用 C++ 處理 Word 文件中的目錄。
Aspose.Words for C++是一個(gè)原生 C++ 庫(kù),允許您創(chuàng)建、閱讀、修改和轉(zhuǎn)換 Microsoft Word 文檔。此外,它還支持處理 Word 文件中的目錄。您可以通過(guò)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; } } } }
如果文檔的內(nèi)容已經(jīng)更新,并且您需要在目錄中反映這些更改,您只需加載 Word 文件并調(diào)用方法。該方法會(huì)根據(jù)修改后的內(nèi)容更新目錄。在此之后,保存更新的 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 文檔中添加或刪除頁(yè)眉和頁(yè)腳詳細(xì)步驟 ,要是您還有其他關(guān)于產(chǎn)品方面的問(wèn)題,歡迎咨詢我們,或者加入我們官方技術(shù)交流群。
歡迎下載|體驗(yàn)更多Aspose產(chǎn)品
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請(qǐng)務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請(qǐng)郵件反饋至chenjj@fc6vip.cn