How to Identify ordered lists and unordered lists seperately

Hii

  1. I need to identify ordered lists and unordered lists in the document separately. Is that possible??
  2. After identifying them separately, I need to apply keepWithNext and keepTogether to avoid page breaks inside them.

Could you please help me to build a solution ??

@nethmi In your case you can check whether list neighbor list items belong to the same list. For example see the following code:

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

// Get all paragraphs, which are list items.
List<Paragraph> listItems = doc.GetChildNodes(NodeType.Paragraph, true)
    .Cast<Paragraph>().Where(p => p.IsListItem).ToList();

// Set keep with next flag if the next paragraph belongs to the same list.
foreach (Paragraph item in listItems)
{
    // Set keep lines togather for all list items.
    item.ParagraphFormat.KeepTogether = true;

    Paragraph nextPara = item.NextSibling as Paragraph;
    if (nextPara != null && nextPara.IsListItem && nextPara.ListFormat.List == item.ListFormat.List)
        item.ParagraphFormat.KeepWithNext = true;
}

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

Hiii

I need to identify whether it is a ordered lists or unordered list. Is it possible?

Because I have to achieve two rules to avoid breaks inside lists. rules are,

  1. don’t break ordered list
    2.don’t break unordered list.

I need to achieve both of the rules separately. is it possible???

@nethmi You can use ListLeve.NumberStyle property.

if (item.ListFormat.ListLevel.NumberStyle == NumberStyle.Bullet)
{
    // Process as unordered list.
}
1 Like

Thank you very much

1 Like