@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");