com.groupdocs.parser.data

Class TocItem



  • public class TocItem
    extends Object
    Represents the item which is used in the table of contents extraction functionality.

    An instance of TocItem class is used as return value of Parser.getToc() method. See the usage examples there.

    • Constructor Detail

      • TocItem

        public TocItem(int depth,
                       String text,
                       Integer pageIndex)
        Initializes a new instance of the TocItem class.
        Parameters:
        depth - The depth level of the item.
        text - The text of the item.
        pageIndex - The index of the page referenced by the item
    • Method Detail

      • getDepth

        public int getDepth()
        Gets the depth level.
        Returns:
        An integer value that represents the depth level of the item.
      • getText

        public String getText()
        Gets the text.
        Returns:
        A string value that represents the text of the item.
      • getPageIndex

        public Integer getPageIndex()
        Gets the page index.
        Returns:
        An integer value that represents the index of the page referenced by the item; null if it isn't set.
      • extractText

        public TextReader extractText()
        Extracts a text from the document to which TocItem object refers.

        The following example how to extract a text by the an item of table of contents:

         // Create an instance of Parser class
         try (Parser parser = new Parser(Constants.SampleDocxWithToc)) {
             // Get table of contents
             Iterable<TocItem> tocItems = parser.getToc();
        
             // Check if toc extraction is supported
             if (tocItems == null) {
                 System.out.println("Table of contents extraction isn't supported");
             }
        
             // 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());
                 }
             }
         }
         
        Returns:
        An instance of TextReader class with the extracted text.