I am trying to set list level 1 to number style Bullet Disk but it's not working

I am trying to set list level 1 to number style Bullet Disk but it’s not working. Kindly help me.
Please find the below mentioned input and expected output.
Expected_output111.docx (14.3 KB)
ListInput.docx (14.4 KB)

Please find the below mentioned code I tried and also find the below mentioned code output I got

List<Paragraph> listItems = doc.GetChildNodes(NodeType.Paragraph, true).Cast<Paragraph>().Where(p => p.IsListItem && p.ListFormat.ListLevelNumber == 0).ToList();

var hanging = 0.25;
var indentation = 0.25;

foreach (Paragraph listBullet in listItems)
{
    listBullet.ListFormat.List = doc.Lists.Add(Aspose.Words.Lists.ListTemplate.BulletDisk);


}

foreach (Paragraph listBullet in listItems)
{
    if (listBullet.ParagraphFormat.LeftIndent.ToString() != ConvertUtil.InchToPoint(Convert.ToDouble(hanging)).ToString())
    {

        listBullet.ParagraphFormat.FirstLineIndent = ConvertUtil.InchToPoint(-Convert.ToDouble(hanging));

        // Clear explicit formatting set to heading 1 paragraphs.

        doc.GetChildNodes(NodeType.Paragraph, true).Cast<Paragraph>().Where(p => p.IsListItem && p.ListFormat.ListLevelNumber == 0).ToList().ForEach(p =>
        {
            p.ParagraphFormat.ClearFormatting();
        });
    }
}

foreach (Paragraph listBullet in listItems)
{
    if (listBullet.ParagraphFormat.LeftIndent.ToString() != ConvertUtil.InchToPoint(Convert.ToDouble(indentation)).ToString())
    {

        listBullet.ParagraphFormat.LeftIndent = ConvertUtil.InchToPoint(Convert.ToDouble(indentation)) - listBullet.ParagraphFormat.FirstLineIndent;

        // Clear explicit formatting set to heading 1 paragraphs.

        doc.GetChildNodes(NodeType.Paragraph, true).Cast<Paragraph>().Where(p => p.IsListItem && p.ListFormat.ListLevelNumber == 0).ToList().ForEach(p =>
        {
            p.ParagraphFormat.ClearFormatting();
        });
    }
}

@Manasahr Your input and expected output documents look exactly the same. So it is not quite clear what is required to achieve. However, while inspecting your code I have discovered few strange things:

  1. The items in the listItems collection is looped three times. You can do everything that is required in one loop.
  2. After making a modification in the loop, you immediately clear the formatting of all paragraphs that are selected into listItems, so your modifications does not have any effect.

I have refactored your code to avoid the above mentioned issues.

List<Paragraph> listItems = doc.GetChildNodes(NodeType.Paragraph, true).Cast<Paragraph>()
    .Where(p => p.IsListItem && p.ListFormat.ListLevelNumber == 0).ToList();

var hanging = 0.25;
var indentation = 0.25;

foreach (Paragraph listBullet in listItems)
{
    // Clear explicit formatting set to list item paragraphs.
    listBullet.ParagraphFormat.ClearFormatting();
    listBullet.ListFormat.List = doc.Lists.Add(ListTemplate.BulletDisk);

    if (listBullet.ParagraphFormat.LeftIndent.ToString() != ConvertUtil.InchToPoint(Convert.ToDouble(hanging)).ToString())
        listBullet.ParagraphFormat.FirstLineIndent = ConvertUtil.InchToPoint(-Convert.ToDouble(hanging));
    if (listBullet.ParagraphFormat.LeftIndent.ToString() != ConvertUtil.InchToPoint(Convert.ToDouble(indentation)).ToString())
        listBullet.ParagraphFormat.LeftIndent = ConvertUtil.InchToPoint(Convert.ToDouble(indentation)) - listBullet.ParagraphFormat.FirstLineIndent;
}

@alexey.noskov Your given code is clearing all the previously applied property. so I cant use it.

I am trying to apply the Bullet Disk, hanging, and indentation for the list level=0 .but hanging and indentation are applied but the bullet disk is not applied.
kindly consider the below-mentioned code I used.

After executing the below-mentioned code I got the output here

List<Paragraph> listItems = doc.GetChildNodes(NodeType.Paragraph, true).Cast<Paragraph>().Where(p => p.IsListItem && p.ListFormat.ListLevelNumber == 0).ToList();

var hanging = 0.25;
var indentation = 0.25;

foreach (Paragraph listBullet in listItems)
{
    listBullet.ListFormat.List = doc.Lists.Add(Aspose.Words.Lists.ListTemplate.BulletDisk);


}

foreach (Paragraph listBullet in listItems)
{
    if (listBullet.ParagraphFormat.LeftIndent.ToString() != ConvertUtil.InchToPoint(Convert.ToDouble(hanging)).ToString())
    {

        listBullet.ParagraphFormat.FirstLineIndent = ConvertUtil.InchToPoint(-Convert.ToDouble(hanging));

        // Clear explicit formatting set to heading 1 paragraphs.

        doc.GetChildNodes(NodeType.Paragraph, true).Cast<Paragraph>().Where(p => p.IsListItem && p.ListFormat.ListLevelNumber == 0).ToList().ForEach(p =>
        {
            p.ParagraphFormat.ClearFormatting();
        });
    }
}

foreach (Paragraph listBullet in listItems)
{
    if (listBullet.ParagraphFormat.LeftIndent.ToString() != ConvertUtil.InchToPoint(Convert.ToDouble(indentation)).ToString())
    {

        listBullet.ParagraphFormat.LeftIndent = ConvertUtil.InchToPoint(Convert.ToDouble(indentation)) - listBullet.ParagraphFormat.FirstLineIndent;

        // Clear explicit formatting set to heading 1 paragraphs.

        doc.GetChildNodes(NodeType.Paragraph, true).Cast<Paragraph>().Where(p => p.IsListItem && p.ListFormat.ListLevelNumber == 0).ToList().ForEach(p =>
        {
            p.ParagraphFormat.ClearFormatting();
        });
    }
}

@Manasahr The code you have provide is exactly the same as you have provide in your initial post and the comments are the same. In your code you select paragraphs, which are list items:

List<Paragraph> listItems = doc.GetChildNodes(NodeType.Paragraph, true).Cast<Paragraph>().Where(p => p.IsListItem && p.ListFormat.ListLevelNumber == 0).ToList();

Then you loop through the items in the resulting collection three times and in the last two loops you clear the formatting applied to the paragraphs:

doc.GetChildNodes(NodeType.Paragraph, true).Cast<Paragraph>().Where(p => p.IsListItem && p.ListFormat.ListLevelNumber == 0).ToList().ForEach(p =>
{
    p.ParagraphFormat.ClearFormatting();
});

So the changes you made does not have any effect.

The code I have provided in my previous answer is simply refactoring of your code. If you do not need to clear paragraphs formatting, remove this line of code.

By the way, the code I have provided produces your expected output: out.docx (12.0 KB)

@alexey.noskov I want to extract only bullet list style: Bullet Square .Kindly help me asap.
I don’t want extract any other list like number list style ,Bullet Disk, Bullet Circle.
Expected_output122.docx (14.7 KB)
Input_list12.docx (15.3 KB)

Hi @Manasahr,
According to your latest input/output files, this should do the job.

public void MainCode()
{
    PartialPath = $@"{prefixPath}\Lists\RemoveBulletStyleInList"; // Location of your input/output files

    LoadLicence(); // Load your License

    var doc = DocumentGet(PartialPath); // Read your document

    Logic(doc);

    doc.Save($"{PartialPath}_output.docx");
}

private void Logic(Document doc)
{
    List<Paragraph> listItems = doc.GetChildNodes(NodeType.Paragraph, true).Cast<Paragraph>().Where(p => p.IsListItem).ToList();

    foreach (Paragraph paragraph in listItems)
    {
        // We only Keep Paragraph from a specific style
        if (paragraph.ListFormat.ListLevelNumber != 2)
        {                
            paragraph.Remove();
        }
    }            
}

RemoveBulletStyleInList_input.docx (15.3 KB)
RemoveBulletStyleInList_output.docx (12.1 KB)

Now if you want to delete everything, not just the lists, you can use this:

private void Logic(Document doc)
{
    List<Paragraph> listItems = doc.GetChildNodes(NodeType.Paragraph, true).Cast<Paragraph>().ToList();

    foreach (Paragraph paragraph in listItems)
    {
        // We only Keep Paragraph from a specific style
        if (!paragraph.IsListItem || paragraph.ListFormat.ListLevelNumber != 2)
        {
            paragraph.Remove();
        }
    }
}

RemoveAllButBulletStyle_input.docx (15.3 KB)
RemoveAllButBulletStyle_output.docx (11.7 KB)

Hopefully, this is the code you need.

FYI @alexey.noskov

@Manasahr You can select the required paragraphs using list level number as Carlos suggested or using list label, for example see the following code:

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

// Update list labels so they can be accessed using Paragraph.ListLabel.
doc.UpdateListLabels();

// Select paragraphs which are list items and their label is '\xf0a7' - Square character.
List<Paragraph> paragraphs = doc.GetChildNodes(NodeType.Paragraph, true).Cast<Paragraph>()
    .Where(p => (p.IsListItem && p.ListLabel.LabelString.Equals("\xf0a7"))).ToList();