How to enable the Numbering Style: 1, 2, 3, … + property

@alexey.noskov /Team,
How to enable the Numbering Style: 1, 2, 3, … + property.
I used below mentioned code but its not working kindly help me asap.

Kindly find the input and expected output word document.
Expected_output_word_document.docx (20.0 KB)
Input_Word_document96.docx (23.4 KB)

NodeCollection nodes = doc.GetChildNodes(NodeType.Paragraph, true);
foreach (Paragraph para in nodes)
{
    if (para.ParagraphFormat.StyleIdentifier == StyleIdentifier.Heading1)
    {
        para.ListFormat.List = doc.Lists.Add(ListTemplate.NumberDefault);
        para.ListFormat.ListLevel.StartAt = 1;
    }
}

Any update on this?

@Princeshivananjappa Numbering is already applied to the heading paragraphs through Heading1 style. But explicit formatting applied to the paragraphs overrides this. In your case you should simply clear explicit formatting of the heading paragraphs. For example see the following code:

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

doc.GetChildNodes(NodeType.Paragraph, true).Cast<Paragraph>()
    .Where(p => p.ParagraphFormat.StyleIdentifier == StyleIdentifier.Heading1)
    .ToList().ForEach(p => { p.ParagraphFormat.ClearFormatting(); p.ParagraphFormat.StyleIdentifier = StyleIdentifier.Heading1; });

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

@alexey.noskov Your given code working fine for hedding1 but I tried same code for the Hedding2 its not working, Kindly help me.

        doc.GetChildNodes(NodeType.Paragraph, true).Cast<Paragraph>()
.Where(p => p.ParagraphFormat.StyleIdentifier == StyleIdentifier.Heading2)
.ToList().ForEach(p => { p.ParagraphFormat.ClearFormatting(); p.ParagraphFormat.StyleIdentifier = StyleIdentifier.Heading2; });
  1. I tried to used the top stop property but its not working. Please find the below code.

     NodeCollection nodes = doc.GetChildNodes(NodeType.Paragraph, true);
         foreach (Paragraph para in nodes)
         {
             if (para.ParagraphFormat.StyleIdentifier == StyleIdentifier.Heading2)
             {                   
                 para.ParagraphFormat.FirstLineIndent = ConvertUtil.InchToPoint(-0.4);
                 para.ParagraphFormat.LeftIndent = -para.ParagraphFormat.FirstLineIndent;
                 para.ParagraphFormat.SpaceBefore = 10;
                 para.ParagraphFormat.SpaceAfter = 0;
                 para.ParagraphFormat.KeepWithNext = true;
                 para.ParagraphFormat.KeepTogether = true;
                 para.ParagraphFormat.TabStops.Add(0.4, TabAlignment.Left, TabLeader.None);
    
             }
         }

@Princeshivananjappa As I can see the following code produces proper result for all headings in your input document:

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

doc.GetChildNodes(NodeType.Paragraph, true).Cast<Paragraph>()
    .Where(p => p.ParagraphFormat.IsHeading)
    .ToList().ForEach(p => { 
        StyleIdentifier tmp = p.ParagraphFormat.StyleIdentifier; 
        p.ParagraphFormat.ClearFormatting(); 
        p.ParagraphFormat.StyleIdentifier = tmp; });

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

@alexey.noskov In my case your given code is not working kindly help me.
Please find the below input document. Input_Hedding.docx (18.1 KB)

Any update on this?

@Princeshivananjappa As I can see the code works fine with the attached document. Please see the output document produced on my side: out.docx (15.2 KB). As yoou can see numbering is properly applied.

Please note, the code will work if numbering is applied through Heading styles. You can check whether numbering s applied in Heading 1 style using the following code

Console.WriteLine(doc.Styles[StyleIdentifier.Heading1].ListFormat.IsListItem);