We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Re number List item

Hi,

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

1. line1
2. line2
3. line3
5. line4
7. 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))
{
           <span style="color:blue;">if</span> (para.IsListItem)
           {
               <span style="color:#2b91af;">List</span> listCopy = RefinedDoc.Lists.AddCopy(para.ListFormat.List);
               <span style="color:#2b91af;">Paragraph</span> item = para;
               <span style="color:blue;">while</span> (item.IsListItem)
               {
                   item.ListFormat.List = listCopy;
                   item = item.NextSibling <span style="color:blue;">as</span> <span style="color:#2b91af;">Paragraph</span>;
                   <span style="color:blue;">if</span> (item == <span style="color:blue;">null</span>)
                       <span style="color:blue;">break</span>;
               }
           }
       }</pre><pre style="font-family: Consolas; background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial;">doc.save(docpath);</pre><pre style="font-family: Consolas; background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial;"><br></pre><pre style="font-family: Consolas; background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial;">Thanks</pre></pre><pre style="font-family: Consolas; font-size: 13px; background: white;"><br></pre></div>

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<o:p></o:p>

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)
{
                <span style="color: blue;">if</span> (ff.Name == <span style="color: rgb(163, 21, 21);">"pink1"</span>)
                    CurrentPara.Accept(<span style="color: blue;">new</span> <span style="color: rgb(43, 145, 175);">FontChanger</span>());
                <span style="color: blue;">if</span> (ff.Name == <span style="color: rgb(163, 21, 21);">"Pink2"</span>)
                    CurrentPara.Accept(<span style="color: blue;">new</span> <span style="color: rgb(43, 145, 175);">FontChanger</span>());
                <span style="color: blue;">if</span> (ff.Name == <span style="color: rgb(163, 21, 21);">"Pink3"</span>)
                    CurrentPara.Accept(<span style="color: blue;">new</span> <span style="color: rgb(43, 145, 175);">FontChanger</span>());
            }
        }

        doc.UpdateListLabels();
        <span style="color: rgb(43, 145, 175);">List</span> listCopy = <span style="color: blue;">null</span>;
        <span style="color: rgb(43, 145, 175);">Paragraph</span> firstItem = <span style="color: blue;">null</span>;
        <span style="color: blue;">foreach</span> (<span style="color: rgb(43, 145, 175);">Paragraph</span> para <span style="color: blue;">in</span> doc.GetChildNodes(<span style="color: rgb(43, 145, 175);">NodeType</span>.Paragraph, <span style="color: blue;">true</span>))
        {

            <span style="color: blue;">if</span> (para.IsListItem)
            { <span style="color: green;">//Create copy of list</span>
                listCopy = doc.Lists.AddCopy(para.ListFormat.List);
                firstItem = para;
                <span style="color: blue;">break</span>;
            }
        }
        <span style="color: blue;">while</span> (firstItem.IsListItem)
        {
            firstItem.ListFormat.List = listCopy;
            firstItem = firstItem.NextSibling <span style="color: blue;">as</span> <span style="color: rgb(43, 145, 175);">Paragraph</span>;
            <span style="color: blue;">if</span> (firstItem == <span style="color: blue;">null</span>)
                <span style="color: blue;">break</span>;
        }
        doc.Save(<span style="color: rgb(43, 145, 175);">Application</span>.StartupPath + <span style="color: rgb(163, 21, 21);">"\\doc1.doc"</span>);</pre><pre style="font-family: Consolas; font-size: 13px; background: white;"><br></pre><pre style="font-family: Consolas; font-size: 13px; background: white;">The font Changes is the class from this Forum.( https://forum.aspose.com/t/17291 )</pre><pre style="font-family: Consolas; font-size: 13px; background: white;">I am attaching document with this.</pre><pre style="font-family: Consolas; font-size: 13px; background: white;"><br></pre><pre style="font-family: Consolas; font-size: 13px; background: white;">Thanks</pre></div>

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.

……<o:p></o:p>

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