Free Support Forum - aspose.com

Parse table of contents using aspose.words for java

Hello,

I am parsing the toc of a document, and I am doing this paragraph by paragraph. What I need is to get for each toc entry( paragraph) the information related to the range of that entry.

For instance, if I have something like this:
Introduction…1
Scope, Approach and Methods… 2
Information Sources…4
Etc.
How can I say “Introduction…1” has range 1 and “Information Sources…4” has range 2?

Thanks,
Mircea

Hello

Thanks for your request. I think in this case you can try using the following code:

// Open document

Document doc = new Document("C:\\Temp\\in.docx");

// Get all paragraphs

NodeCollection nodeColl = doc.getChildNodes(NodeType.PARAGRAPH, true);

// Loop through all paragraphs

for(int i = 0; i < nodeColl.getCount(); i++){

switch (((Paragraph)nodeColl.get(i)).getParagraphFormat().getStyleIdentifier())

{

case StyleIdentifier.HEADING_1:

System.out.println(((Paragraph)nodeColl.get(i)).toTxt() + "Range 1");

break;

case StyleIdentifier.HEADING_2:

System.out.println(((Paragraph)nodeColl.get(i)).toTxt() + "Range 2");

break;

case StyleIdentifier.HEADING_3:

System.out.println(((Paragraph)nodeColl.get(i)).toTxt() + "Range 3");

break;

default:

// do nothing.

break;

}

}

Bets regards,

Hi there,

Thanks for your inquiry.

Just a quick alternation to Andrey's code, I think in your case you are looking for the TOC styles and not the Heading styles. Please use StyleIdentifer.TOC1 instead of StyleIdentifier.Heading1 etc.

Thanks,

Thanks guys,

indeed I am looking for TOC styles. I have managed to solve my problem using your examples.

Thanks again,
Mircea