Is it possible to detect the level of a Heading?

If I have a Paragraph object, I can look at its ParagraphFormat.isListItem() to determine if it is a list item, or its ParagraphFormat.isHeading() to determine (to some extent) if it is a Heading.

I can check ListFormat.getListLevelNumber() to get the level of the list item, but I cannot find any way to do the same with a HEADING.

Hi Alex,

Thanks for your inquiry. The Heading is Paragraph node in Aspose.Words DOM. The ParagraphFormat().isHeading() method return true when the paragraph style is one of the built-in Heading styles.

The methods ParagraphFormat.isListItem() and ListFormat.getListLevelNumber() works with Headings as well. Please see the following code snippet.

If you still face problem, please share your document here for investigation purposes.

Document doc = new Document(MyDir + "Paragraph.docx");

for (Paragraph para: (Iterable <Paragraph> ) doc.getChildNodes(NodeType.PARAGRAPH, true))
{
    if (para.getParagraphFormat().isHeading())
    {
        System.out.println(para.getParagraphFormat().isListItem());
        System.out.println(para.getListFormat().getListLevelNumber());
    }
}

Hi Tahir,

I know how to get the List Level, and I am aware a Heading is a Paragraph. How do I get the Heading level?

This is Heading Level 1
This is Heading Level 2
This is Heading Level 3
This is Heading Level 4

Assuming the above 4 lines use the Styles “Heading 1” through “Heading 4”, how do I determine what level Heading I am looking at (preferably without searching the ParagraphFormat.getStyleName() for an integer)?

Hi Alex,

Thanks for your inquiry. In case you are using an older version of Aspose.Words, I would suggest you please upgrade to the latest version (v13.1.0) from here.

alexmcmillan:
I can check ListFormat.getListLevelNumber() to get the level of the list item, but I cannot find any way to do the same with a HEADING.

You can also get the level of list item for Headings by ListFormat.getListLevelNumber method.

If you still face problem, please manually create Word document using Microsoft Word with different Heading Levels and attach it here for our reference. I will investigate the issue on my side and provide you more information along with code.

Hi Tahir,

I am using the latest version of Aspose.Words.

I think you have misunderstood my question. I used List Levels to try and explain my question, but my question does not relate to List Levels. I am asking about Heading Levels (the equivalent of the number from the HTML tag group h1, h2, h3, h4 etc)

My question is: given a paragraph, how do you tell what level Heading it is?

Please find attached the Word document holding the Headings as requested. It holds 2 lines, the first styled as “Heading 1” and the second styled as “Heading 2”. As I am iterating through the nodes in the document, how can I get the level (1 or 2) that each paragraph has?

Hi Alex,

Thanks for sharing the details. The tags
to
are imported into the Aspose.Words DOM as the built-in Heading styles: Heading 1 - Heading 6. Similarly, Paragraphs with built-in heading styles are exported as
-
elements.

You can check the Paragraph styles (Heading Level) as shown in following code snippet.

Document doc = new Document(MyDir + "in.docx");

for (Paragraph paragraph: (Iterable <Paragraph> ) doc.getFirstSection().getBody().getParagraphs())
{
    if (paragraph.getParagraphFormat().getStyleIdentifier() == StyleIdentifier.HEADING_1)
        System.out.println("Heading Level 1");
    else if (paragraph.getParagraphFormat().getStyleIdentifier() == StyleIdentifier.HEADING_2)
        System.out.println("Heading Level 2");
    else if (paragraph.getParagraphFormat().getStyleIdentifier() == StyleIdentifier.HEADING_3)
        System.out.println("Heading Level 3");
}

Hi Alex,

Thanks for your inquiry.

You can find the level of a heading by using the ParagraphFormat.OutlineLevel property. This number returns an enumeration representing the outline level of the paragraph in the document. You will just need to cast it to an integer and add one. Also note that a value of 10 represents body text and not a heading.

int level = (int) doc.FirstSection.Body.FirstParagraph.ParagraphFormat.OutlineLevel - 1;

This method will also work for paragraphs that are actually headings but are not in-built headings, for example if paragraphs that are imported from another document.

Thanks,

Thanks Adam - exactly what I was after!