We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Change style of List Items to H1,H2,H3 etc depending on the nesting level

Hello I would like to change the styling of List Items which have bold style to Heading 1, Heading 2, Heading 3 depending on the level. Can you please guide me. Have attached a sample document unordered-test.docx (23.8 KB)

@harshvardhan.scindia You can use ListFormat.ListLevelNumber property to check the level of the list. For example see the following code:

Document doc = new Document(@"C:\Temp\unordered-test.docx");

NodeCollection paragraphs = doc.GetChildNodes(NodeType.Paragraph, true);
foreach (Paragraph p in paragraphs)
{
    if (p.IsListItem)
    {
        int listLevel = p.ListFormat.ListLevelNumber;
        switch (listLevel)
        {
            case 0:
                p.ParagraphFormat.StyleIdentifier = StyleIdentifier.Heading1;
                break;
            case 1:
                p.ParagraphFormat.StyleIdentifier = StyleIdentifier.Heading2;
                break;
            case 2:
                p.ParagraphFormat.StyleIdentifier = StyleIdentifier.Heading3;
                break;
            default:
                break;
        }
    }
}

doc.Save(@"C:\Temp\out.docx");

Hi, Thank You for the quick reply. I would like to apply Heading 1, Only on the List Items that have bold formatting. tempsnip-unordered-list.png (27.3 KB)

Have highlighted the part in red boxes which should be Heading 1 or Heading 2. etc. In the above solution it is highlighting the paragraphs as well.

@harshvardhan.scindia You can use code like this to check whether paragraph is bold.

Document doc = new Document(@"C:\Temp\unordered-test.docx");

NodeCollection paragraphs = doc.GetChildNodes(NodeType.Paragraph, true);
foreach (Paragraph p in paragraphs)
{
    if (p.IsListItem)
    {
        int listLevel = p.ListFormat.ListLevelNumber;
        if (listLevel == 0 && p.ParagraphBreakFont.Bold)
            p.ParagraphFormat.StyleIdentifier = StyleIdentifier.Heading1;
    }
}

doc.Save(@"C:\Temp\out.docx");

Thank You it is working as expected.

1 Like