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

Free Support Forum - aspose.com

How can I get a unique list style for a given name?

There is a multi-level list style in my document,its name is:li2108,but I opened the file and got it doca.Lists.Count=22,and the name is li2108 list style,there are three, not one

doca = new Aspose.Words.Document("wina.docx");
foreach (Aspose.Words.Lists.List  ff in doca.Lists)
{
    if (ff.Style != null)
    { 
        if(ff.Style.Name == "li2108")
        {
            NewList = ff;
        }
    }
}

I have two questions:
1、How can I get a unique list style for a given name(li2108)?
2、How can I customize a list style in the program and add it to the document(doca)
wina.docx (27.4 KB)

@lovecomputer

Please get the desired style using following code snippet.

Document doc = new Document(MyDir + "wina.docx");
Style style = doc.Styles["li2108"];

You can use the above code snippet to get the style and update it according to your requirement. If you face any issue, please share some more detail about your requirement along with input and expected output documents. We will then guide you according to your requirement.

I use two methods to get the list style of the document
1、The method you recommend:but error:throw Message:The list is a definition of a list style.this is code:

Aspose.Words.Lists.List NewList1;
try
{
    doca = new Aspose.Words.Document("wina.docx");
    Aspose.Words.Style style = doca.Styles["li2108"];
    NewList1 = style.List;
    foreach (Aspose.Words.Paragraph p in doca.FirstSection.Body.Paragraphs)
    {
        if (p.Range.Text.Contains("一"))
        {
            p.ListFormat.List = NewList1;
            p.ListFormat.ListLevelNumber = 0;
        }

    }
    doca.Save("wina2108.docx");
}
catch (Exception ee)
{
    throw ee;
}

2、My original method,successful,but I opened the file and got it doca.Lists.Count is 21,and the name is li2108 list style,there are four, not one,doca.lists[20].Style.Name,doca.lists[19].Style.Name,doca.lists[18].Style.Name,doca.lists[17].Style.Name,Their names are li208,this code:

Aspose.Words.Lists.List NewList1;
try
{
    doca = new Aspose.Words.Document("wina.docx");
    Aspose.Words.Style style = doca.Styles["li2108"];
    NewList1 = style.List;
    foreach (Aspose.Words.Paragraph p in doca.FirstSection.Body.Paragraphs)
    {
        if (p.Range.Text.Contains("一"))
        {
            p.ListFormat.List = NewList1;
            p.ListFormat.ListLevelNumber = 0;
            //		Message	"The list is a definition of a list style.

        }

    }
    doca.Save("wina2108.docx");
}
catch (Exception ee)
{
    throw ee;
}

I upload two files, one is the source file(wina), one is the target file(Target-wina)
Target-wina.docx (20.1 KB)
wina.docx (20.0 KB)

@lovecomputer

We have logged this problem in our issue tracking system as WORDSNET-22454. You will be notified via this forum thread once this issue is resolved. We apologize for your inconvenience.

You are getting the expected behavior of Aspose.Words. Please unzip your document and check the li2108 in numbering.xml.

@lovecomputer

Please use following code example to avoid the shared issue. Hope this helps you.

Aspose.Words.Lists.List NewList1;
Document doca = new Aspose.Words.Document(MyDir + "wina (1).docx");
Aspose.Words.Style style = doca.Styles["li2108"];
NewList1 = style.List;

if (NewList1.IsListStyleDefinition)
    NewList1 = doca.Lists.AddCopy(NewList1);

foreach (Aspose.Words.Paragraph p in doca.FirstSection.Body.Paragraphs)
{
    if (p.Range.Text.Contains("一"))
    {
        p.ListFormat.List = NewList1;
        p.ListFormat.ListLevelNumber = 0;
    }

}
doca.Save(MyDir + "output.docx");