TocItem

Inheritance: java.lang.Object

public class TocItem

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.

Constructors

Constructor Description
TocItem(int depth, String text, Integer pageIndex) Initializes a new instance of the TocItem class.

Methods

Method Description
getDepth() Gets the depth level.
getText() Gets the text.
getPageIndex() Gets the page index.
extractText() Extracts a text from the document to which TocItem object refers.

TocItem(int depth, String text, Integer pageIndex)

public TocItem(int depth, String text, Integer pageIndex)

Initializes a new instance of the TocItem class.

Parameters:

Parameter Type Description
depth int The depth level of the item.
text java.lang.String The text of the item.
pageIndex java.lang.Integer The index of the page referenced by the item

getDepth()

public int getDepth()

Gets the depth level.

Returns: int - An integer value that represents the depth level of the item.

getText()

public String getText()

Gets the text.

Returns: java.lang.String - A string value that represents the text of the item.

getPageIndex()

public Integer getPageIndex()

Gets the page index.

Returns: java.lang.Integer - 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 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: TextReader - An instance of TextReader class with the extracted text.