# Extract Table of Contents from Microsoft Office Word Documents Learn how to extract and work with table of contents from Word documents (.doc, .docx) using GroupDocs.Parser for .NET. ## Overview This guide explains how to extract the table of contents (TOC) from Microsoft Office Word documents (`.doc`, `.docx`) using GroupDocs.Parser for .NET. The TOC is automatically generated by detecting paragraphs with built-in heading styles (H1-H9) ## How TOC Extraction Works The [GetToc](https://reference.groupdocs.com/net/parser/groupdocs.parser/parser/methods/gettoc) method analyzes your Word document and identifies all headings formatted with built-in heading styles. It returns a structured collection of TOC items that you can navigate programmatically. To extract table of contents from Microsoft Office Word document [GetToc](https://reference.groupdocs.com/net/parser/groupdocs.parser/parser/methods/gettoc) method is used. Table of contents is generated by paragraphs with H1-H9 build-in styles. {{< alert style="warning" >}} [GetToc](https://reference.groupdocs.com/net/parser/groupdocs.parser/parser/methods/gettoc) method returns *null* value if table of contents extraction isn't supported for the document. For example, table of contents extraction isn't supported for TXT files. Therefore, for TXT file [GetToc](https://reference.groupdocs.com/net/parser/groupdocs.parser/parser/methods/gettoc) method returns *null*. If Microsoft Office Word document has no table of contents, [GetToc](https://reference.groupdocs.com/net/parser/groupdocs.parser/parser/methods/gettoc) method returns an empty collection. {{< /alert >}} [GetToc](https://reference.groupdocs.com/net/parser/groupdocs.parser/parser/methods/gettoc) method returns a collection of [TocItem](https://reference.groupdocs.com/net/parser/groupdocs.parser.data/tocitem) objects. [TocItem.GetText](https://reference.groupdocs.com/net/parser/groupdocs.parser.data/tocitem/methods/gettext) method returns a text from the chapter to which table of contents item refers (without sub-chapters). For example, "Heading 1.2" from the page ![](https://docs.groupdocs.com/parser/net/images/extract-table-of-contents-from-microsoft-office-word-documents.png) returns the following text: ![](https://docs.groupdocs.com/parser/net/images/extract-table-of-contents-from-microsoft-office-word-documents_1.png) "Heading 2" from the page: ![](https://docs.groupdocs.com/parser/net/images/extract-table-of-contents-from-microsoft-office-word-documents_2.png) returns the following text: ![](https://docs.groupdocs.com/parser/net/images/extract-table-of-contents-from-microsoft-office-word-documents_3.png) {{< alert style="warning" >}} [InvalidOperationException](https://docs.microsoft.com/en-us/dotnet/api/system.invalidoperationexception?view=netframework-2.0) is thrown if [tocItem.PageIndex](https://reference.groupdocs.com/net/parser/groupdocs.parser.data/tocitem/properties/pageindex) is *null*. {{< /alert >}} Here are the steps to extract a text by an item of table of contents: * Instantiate [Parser](https://reference.groupdocs.com/net/parser/groupdocs.parser/parser) object for the initial document; * Call [GetToc](https://reference.groupdocs.com/net/parser/groupdocs.parser/parser/methods/gettoc) method and obtain the collection of [TocItem](https://reference.groupdocs.com/net/parser/groupdocs.parser.data/tocitem) objects; * Iterate through the collection and extract a text. The following example shows how to extract a text by an item of table of contents: ```csharp // Create an instance of Parser class using (Parser parser = new Parser(Constants.SampleDocxWithToc)) { // Get table of contents IEnumerable tocItems = parser.GetToc(); // Iterate over items foreach (TocItem tocItem in tocItems) { // Print the text of the chapter using (TextReader reader = tocItem.ExtractText()) { Console.WriteLine("----"); Console.WriteLine(reader.ReadToEnd()); } } } ``` ## Practical Use Cases * Document analysis: Quickly scan document structure and content * Content extraction: Pull specific sections based on headings * Navigation building: Create custom navigation systems for documents * Content validation: Verify document structure and heading hierarchy ## More resources ### GitHub examples You may easily run the code above and see the feature in action in our GitHub examples: * [GroupDocs.Parser for .NET examples](https://github.com/groupdocs-parser/GroupDocs.Parser-for-.NET) * [GroupDocs.Parser for Java examples](https://github.com/groupdocs-parser/GroupDocs.Parser-for-Java) ### Free online document parser App Along with full featured .NET library we provide simple, but powerful free Apps. You are welcome to parse documents and extract data from PDF, DOC, DOCX, PPT, PPTX, XLS, XLSX, Emails and more with our free online [Free Online Document Parser App](https://products.groupdocs.app/parser).