# 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 Java. ## Overview This guide explains how to extract the table of contents (TOC) from Microsoft Office Word documents (`.doc`, `.docx`) using GroupDocs.Parser for Java. 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/java/parser/com.groupdocs.parser/Parser#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. {{< alert style="warning" >}}[getToc](https://reference.groupdocs.com/java/parser/com.groupdocs.parser/Parser#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/java/parser/com.groupdocs.parser/Parser#getToc()) method returns null. If Microsoft Office Word document has no table of contents, [getToc](https://reference.groupdocs.com/java/parser/com.groupdocs.parser/Parser#getToc()) method returns an empty collection.{{< /alert >}} [getToc](https://reference.groupdocs.com/java/parser/com.groupdocs.parser/Parser#getToc()) method returns a collection of [TocItem](https://reference.groupdocs.com/java/parser/com.groupdocs.parser.data/TocItem) objects. [extractText](https://reference.groupdocs.com/java/parser/com.groupdocs.parser.data/TocItem#extractText()) 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/java/images/extract-table-of-contents-from-microsoft-office-word-documents.png) returns the following text: ![](https://docs.groupdocs.com/parser/java/images/extract-table-of-contents-from-microsoft-office-word-documents_1.png) "Heading 2" from the page: ![](https://docs.groupdocs.com/parser/java/images/extract-table-of-contents-from-microsoft-office-word-documents_2.png) returns the following text: ![](https://docs.groupdocs.com/parser/java/images/extract-table-of-contents-from-microsoft-office-word-documents_3.png) {{< alert style="warning" >}}java.lang.UnsupportedOperationException is thrown if getPageIndex() is null.{{< /alert >}} Here are the steps to extract a text by an item of table of contents: * Instantiate [Parser](https://reference.groupdocs.com/java/parser/com.groupdocs.parser/Parser) object for the initial document; * Call [getToc](https://reference.groupdocs.com/java/parser/com.groupdocs.parser/Parser#getToc()) method and obtain the collection of [TocItem](https://reference.groupdocs.com/java/parser/com.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: ```java // Create an instance of Parser class try (Parser parser = new Parser(Constants.SampleDocxWithToc)) { // Get table of contents Iterable tocItems = parser.getToc(); // Iterate over items for (TocItem tocItem : tocItems) { // Print the text of the chapter try (TextReader reader = tocItem.extractText()) { System.out.println("----"); System.out.println(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 Java 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).