翻譯|使用教程|編輯:李顯亮|2021-05-12 10:38:35.443|閱讀 285 次
概述:有時會遇到需要以編程方式從Word文檔中添加,提取,更新或刪除目錄的情況。為此,本文將教您如何使用C ++處理Word文件中的目錄。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關鏈接:
目錄(TOC)是Word文檔的重要組成部分。它提供了文檔內容的概述,并允許您快速導航到所需的部分。您可能會遇到需要以編程方式從Word文檔中添加,提取,更新或刪除目錄的情況。為此,本文將教您如何使用C ++處理Word文件中的目錄。
讓我們探索以下有關的內容:
Aspose.Words for C ++ 是本機C ++庫,允許您創建,讀取,修改和轉換Microsoft Word文檔。此外,它還支持使用Word文件中的目錄。
>>你可以點擊這里下載Aspose.Words for C ++ 最新版測試體驗。
以下是在Word文檔中添加目錄的步驟。
下面的示例代碼顯示了如何使用C ++在Word文檔中添加目錄。
// Source and output directory paths. System::String sourceDataDir = u"SourceDirectory\\"; System::String outputDataDir = u"OutputDirectory\\"; // Load the Word file System::SharedPtrdoc = System::MakeObject(sourceDataDir + u"Sample 5.docx"); // Create an instance of the DocumentBuilder class System::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 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::SharedPtrdoc = System::MakeObject(inputDataDir + u"SampleTOC.docx");
// Loop through the fields
for (System::SharedPtrfield : System::IterateOver(doc->get_Range()->get_Fields()))
{
// Get FieldHyperlink fields
if (field->get_Type() == FieldType::FieldHyperlink)
{
System::SharedPtrhyperlink = System::DynamicCast(field);
// Check if field belongs to TOC
if (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 to
System::SharedPtrpointer = System::DynamicCast(bm->get_BookmarkStart()->GetAncestor(NodeType::Paragraph));
std::cout << System::StaticCast(pointer)->ToString(SaveFormat::Text).ToUtf8String() << std::endl; } } } }
如果文檔的內容已更新,并且您需要在目錄中反映這些更改,則只需加載Word文件并調用 Document-> UpdateFields() 方法。此方法將根據修改后的內容更新目錄。之后,保存更新的Word文檔。
以下是從Word文檔中刪除目錄的步驟。
下面的示例代碼顯示了如何使用C ++從Word文檔中刪除目錄。
void RemoveTableOfContents(const System::SharedPtr& doc, int32_t index)
{
// Store the FieldStart nodes of TOC fields in the document for quick access.
std::vectorfieldStarts;
// This is a list to store the nodes found inside the specified TOC. They will be removed
// at the end of this method.
std::vectornodeList;
for (System::SharedPtrstart : System::IterateOver(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::SharedPtrcurrentNode = 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::SharedPtrfieldEnd = System::DynamicCast(currentNode);
if (fieldEnd->get_FieldType() == FieldType::FieldTOC)
{
isRemoving = false;
}
}
}
// Remove all nodes found in the specified TOC.
for (System::SharedPtrnode : 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::SharedPtrdoc = System::MakeObject(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);
}
如果你想試用Aspose的全部完整功能,可聯系在線客服獲取30天臨時授權體驗。
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@ke049m.cn