Re number List item

Hi,
I have some list items in my document. for ex-

  1. line1
  2. line2
  3. line3
  4. line4
  5. line5

In this the list numbering is wrong. I want to change this numbering using Aspose. I tried the following code but it set 1 to all list items. Please check.

Document RefinedDoc = new Document(docpath);
RefinedDoc.UpdateListLabels();

foreach (Paragraph para in RefinedDoc.GetChildNodes(NodeType.Paragraph, true))
{
    if (para.IsListItem)
    {
        List listCopy = RefinedDoc.Lists.AddCopy(para.ListFormat.List);
        Paragraph item = para;
        while (item.IsListItem)
        {
            item.ListFormat.List = listCopy;
            item = item.NextSibling as Paragraph;
            if (item == null)
                break;
        }
    }
}
doc.save(docpath);

Hi there,

Thanks for your inquiry. Please check following code snippet for your requirements. Hopefully it will help you to accomplish to the task.

// Open document
Document doc = new Document(@"list_Test.doc");
List listCopy = null;
Paragraph firstItem = null;
foreach (Paragraph para in doc.GetChildNodes(NodeType.Paragraph, true))
{
    if (para.IsListItem)
    { //Create copy of list
        listCopy = doc.Lists.AddCopy(para.ListFormat.List);
        firstItem = para;
        break;
    }
}
while (firstItem.IsListItem)
{
    firstItem.ListFormat.List = listCopy;
    firstItem = firstItem.NextSibling as Paragraph;
    if (firstItem == null)
        break;
}
// save output document
doc.Save(@"Renumber_list_out.docx");

Best Regards,

Hi ,
It is not working. Actually i am deleting some lines and want to renumber the list.
I am using following code to delete and renumber.

Document doc = new Document(Application.StartupPath + "\doc.doc");

var GetParagraphs = doc.GetChildNodes(NodeType.Paragraph, true);
foreach (Paragraph CurrentPara in GetParagraphs)
{
    var Formfieldsarr = CurrentPara.GetChildNodes(NodeType.FormField, true);
    foreach (FormField ff in Formfieldsarr)
    {
        if (ff.Name == "pink1")
            CurrentPara.Accept(new FontChanger());
        if (ff.Name == "Pink2")
            CurrentPara.Accept(new FontChanger());
        if (ff.Name == "Pink3")
            CurrentPara.Accept(new FontChanger());
    }
}

doc.UpdateListLabels();
List listCopy = null;
Paragraph firstItem = null;
foreach (Paragraph para in doc.GetChildNodes(NodeType.Paragraph, true))
{

    if (para.IsListItem)
    { //Create copy of list
        listCopy = doc.Lists.AddCopy(para.ListFormat.List);
        firstItem = para;
        break;
    }
}
while (firstItem.IsListItem)
{
    firstItem.ListFormat.List = listCopy;
    firstItem = firstItem.NextSibling as Paragraph;
    if (firstItem == null)
        break;
}
doc.Save(Application.StartupPath + "\doc1.doc");

The font Changes is the class from this Forum.(https://forum.aspose.com/t/17291)
I am attaching document with this.

Thanks

Hi,

I am hidding them. I should have remove them.

Thanks.

Hi there,

Thanks for your feedback. Please note you are getting expected results. If you hide text using MS Word and re number the list items then it behaves similarly as Aspose.Word does.

You may remove the paragraph as following and re number the list items. Hopefully it will help you to accomplish the task.

if (ff.Name == "pink1")
    CurrentPara.Remove();
// CurrentPara.Accept(new FontChanger());
if (ff.Name == "Pink2")
    CurrentPara.Remove();
// CurrentPara.Accept(new FontChanger());
if (ff.Name == "Pink3")
    CurrentPara.Remove();
// CurrentPara.Accept(new FontChanger());

Best Regards,

Hi,

Yes, Done the same. It worked…

Thanks