Created
October 20, 2022 11:11
-
-
Save conholdate-docs-gists/ffe75c341b0c907513d8ae08f6d4d3d2 to your computer and use it in GitHub Desktop.
Retrieve Table of Contents in C# | https://docs.conholdate.com/net/retrieve-table-of-contents/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Create an instance of Parser class | |
using (Parser parser = new Parser(filePath)) | |
{ | |
// Check if text extraction is supported | |
if (!parser.Features.Text) | |
{ | |
Console.WriteLine("Text extraction isn't supported."); | |
return; | |
} | |
// Check if toc extraction is supported | |
if (!parser.Features.Toc) | |
{ | |
Console.WriteLine("Toc extraction isn't supported."); | |
return; | |
} | |
// Get table of contents | |
IEnumerable<TocItem> toc = parser.GetToc(); | |
// Iterate over items | |
foreach (TocItem i in toc) | |
{ | |
// Print the Toc text | |
Console.WriteLine(i.Text); | |
// Check if page index has a value | |
if (i.PageIndex == null) | |
{ | |
continue; | |
} | |
// Extract a page text | |
using (TextReader reader = parser.GetText(i.PageIndex.Value)) | |
{ | |
Console.WriteLine(reader.ReadToEnd()); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Read complete article here: https://docs.conholdate.com/net/retrieve-table-of-contents/