How can I use Multilevel List and attach it to a certain style?

I want to use Multilevel List and attach it to a certain stly, how can I do that?
I mean, I want to create a style ( lets say use StyleIdentifier.Heading_1, i want it not only has the paragraphformat and font that I want, but also can generate list as well.

the demo code is here :

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
List list = doc.getLists().add(ListTemplate.OUTLINE_NUMBERS);

list.getListLevels().get(0).setNumberFormat("%1.");
list.getListLevels().get(0).setNumberStyle(NumberStyle.ARABIC);
list.getListLevels().get(0).setNumberPosition(0);

list.getListLevels().get(1).setNumberFormat("%1.%2.");
list.getListLevels().get(1).setNumberStyle(NumberStyle.ARABIC);
list.getListLevels().get(1).setNumberPosition(20);

builder.getListFormat().setList(list);
builder.writeln("一级项目1");
builder.getListFormat().listIndent();
builder.writeln("二级项目1");
builder.getListFormat().listOutdent();
builder.writeln("一级项目2");

and the output is

however , I thought I should the Multilevel List that I use, but its still Numbering, why ?
black box again. :>>>

@Madecho You can use the following code to apply the list via styles:

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Create the list
com.aspose.words.List list = doc.getLists().add(ListTemplate.OUTLINE_NUMBERS);
// Configure levels.
list.getListLevels().get(0).setNumberFormat("%1.");
list.getListLevels().get(0).setNumberStyle(NumberStyle.ARABIC);
list.getListLevels().get(0).setNumberPosition(0);

list.getListLevels().get(1).setNumberFormat("%1.%2.");
list.getListLevels().get(1).setNumberStyle(NumberStyle.ARABIC);
list.getListLevels().get(1).setNumberPosition(20);

// Assign the list to the appropriate styles.
Style h1 = doc.getStyles().getByStyleIdentifier(StyleIdentifier.HEADING_1);
Style h2 = doc.getStyles().getByStyleIdentifier(StyleIdentifier.HEADING_2);
h1.getListFormat().setList(list);
h1.getListFormat().setListLevelNumber(0);
h2.getListFormat().setList(list);
h2.getListFormat().setListLevelNumber(1);

builder.getParagraphFormat().setStyleIdentifier(StyleIdentifier.HEADING_1);
builder.writeln("First level");
builder.writeln("First level");
builder.getParagraphFormat().setStyleIdentifier(StyleIdentifier.HEADING_2);
builder.writeln("Second level");
builder.getParagraphFormat().clearFormatting();

doc.save("C:\\Temp\\out.docx");

It looks like a specific of MS Word UI. You will see the same even if apply multilevel list using MS Word.

1 Like

@alexey.noskov ah alexey, that a question that strikes me , I thought Aspose words is designed dealing with MS Word , but your reply suggests that it aims at some other ‘Word’ ? What is it ?

@Madecho Aspose.Words is designed to work with MS Word document, not with MS Word itself. Aspose.Words works only with document files and how the files are interpreted by the consumer applications is out of Aspose.Words control.