Extract table of contents from EPUB eBooks

View as MarkdownMarkdown

Set up your AI agent

Give your coding agent live access to the GroupDocs documentation and API reference through the GroupDocs Docs MCP server — no install, no API key. Learn more

claude mcp add --transport http groupdocs-docs https://docs.groupdocs.com/mcp

Also process documents locally

The GroupDocs.Parser MCP server lets your agent work with files on your machine; they are never uploaded. It runs in Docker: replace /path/to/documents with the folder it may use.

claude mcp add groupdocs-parser -- docker run --rm -i -v /path/to/documents:/data ghcr.io/groupdocs-parser/parser-net-mcp:latest
Folders, licensing and more →

To extract table of contents from EPUB e-books getToc method is used.

Warning
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 method returns null. If EPUB e-book has no table of contents, getToc method returns an empty collection.

Here are the steps to extract extract table of contents from EPUB e-book:

  • Instantiate Parser object for the initial e-book;
  • Call getToc method and obtain collection of TocItem objects;
  • Iterate through the collection and get page index to extract a page text from the document.

The following example shows how to extract table of contents from EPUB e-book:

// Create an instance of Parser class
try (Parser parser = new Parser(Constants.SampleEpub)) {
    // Get table of contents
    Iterable<TocItem> 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());
        }
    }
}

More resources

GitHub examples

You may easily run the code above and see the feature in action in our GitHub examples:

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.