Nesting lists

Hi,

I have a use case to nest lists.

This is what I wish to achieve:

  1. Some content here
    Some more content here
    • List here
    • List here also
  2. Other content here

What is the best way to do this with the .NET library?

@aspose.shrayasr,

Thanks for your inquiry. Please use following code example get the desired output. Hope this helps you.

DocumentBuilder builder = new DocumentBuilder();

builder.Writeln("Aspose.Words allows:");
builder.Writeln();

// Start a numbered list with default formatting.
builder.ListFormat.ApplyNumberDefault();
builder.Writeln("Opening documents from different formats:");

// Go to second list level, add more text.
builder.ListFormat.ListIndent();
builder.Writeln("DOC");
builder.Writeln("PDF");
builder.Writeln("HTML");

// Outdent to the first list level.
builder.ListFormat.ListOutdent();
builder.Writeln("Processing documents");
builder.Writeln("Saving documents in different formats:");

// Indent the list level again.
builder.ListFormat.ListIndent();
builder.Writeln("DOC");
builder.Writeln("PDF");
builder.Writeln("HTML");
builder.Writeln("MHTML");
builder.Writeln("Plain text");

// Outdent the list level again.
builder.ListFormat.ListOutdent();
builder.Writeln("Doing many other things!");

// End the numbered list.
builder.ListFormat.RemoveNumbers();
builder.Writeln();

builder.Writeln("Aspose.Words main advantages are:");
builder.Writeln();

// Start a bulleted list with default formatting.
builder.ListFormat.ApplyBulletDefault();
builder.Writeln("Great performance");
builder.Writeln("High reliability");
builder.Writeln("Quality code and working");
builder.Writeln("Wide variety of features");
builder.Writeln("Easy to understand API");

// End the bulleted list.
builder.ListFormat.RemoveNumbers();

builder.Document.Save(MyDir + "Lists.ApplyDefaultBulletsAndNumbers Out.doc");

Hi Tahir,

This helps, but what if I want to have bullets under a numbered list like I’ve shown in the original question.

Example:

  1. Aspose.words supports the following output format
    • DOC
    • PNG
    • PDF

Additionally what do I do if I want to add a paragraph in between my numbers? So Something like:


Heading 1

  1. Some content
    Some other content
    • Hello
    • World

Heading 2

  1. Continued content
    More details here
    • Foo
    • Bar

What do I do in this case?

@aspose.shrayasr,

Thanks for your inquiry. Please use the following code example to get the desired output.

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);

builder.ParagraphFormat.StyleIdentifier = StyleIdentifier.Heading1;
builder.Writeln("Aspose.Words allows:");
builder.ParagraphFormat.ClearFormatting();
builder.Writeln();

// Start a numbered list with default formatting.
builder.ListFormat.ApplyNumberDefault();
builder.Write("Opening documents from different formats:");
builder.InsertBreak(BreakType.LineBreak);
builder.Writeln("Some more content here:");

// Go to second list level, add more text.
builder.ListFormat.ApplyBulletDefault();
builder.ListFormat.ListIndent();
builder.Writeln("DOC");
builder.Writeln("PDF");
builder.Writeln("HTML");

// End the numbered list.
builder.ListFormat.RemoveNumbers();
builder.Writeln();
builder.ParagraphFormat.StyleIdentifier = StyleIdentifier.Heading1;
builder.Writeln("Aspose.Words main advantages are:");
builder.ParagraphFormat.ClearFormatting();
builder.Writeln();

// Start a bulleted list with default formatting.
builder.ListFormat.ApplyNumberDefault();
builder.Writeln("Great performance");
builder.ListFormat.ApplyBulletDefault();
builder.ListFormat.ListIndent();
builder.Writeln("High reliability");
builder.Writeln("Quality code and working");
builder.Writeln("Wide variety of features");
builder.Writeln("Easy to understand API");

// End the bulleted list.
builder.ListFormat.RemoveNumbers();

doc.Save(MyDir + "output.docx");

Hi Tahir,

This just almost solves it :slight_smile: And is as far as I was able to get actually. I failed because the number doesn’t continue. Since you restart the numbering, it starts again from 1 instead of continuing from 2

Output of your snippet is this:

image.png (4.3 KB)

@aspose.shrayasr,

Thanks for your inquiry. Please try following modified code. Hope this helps you.

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);

builder.ParagraphFormat.StyleIdentifier = StyleIdentifier.Heading1;
builder.Writeln("Aspose.Words allows:");

builder.ParagraphFormat.ClearFormatting();
builder.Writeln();

// Start a numbered list with default formatting.
builder.ListFormat.ApplyNumberDefault();
List list = builder.ListFormat.List;

builder.Write("Opening documents from different formats:");
builder.InsertBreak(BreakType.LineBreak);
builder.Writeln("Some more content here:");

// Go to second list level, add more text.
builder.ListFormat.ApplyBulletDefault();
builder.ListFormat.ListIndent();
builder.Writeln("DOC");
builder.Writeln("PDF");
builder.Writeln("HTML");

// End the numbered list.
builder.ListFormat.RemoveNumbers();
builder.Writeln();
builder.ParagraphFormat.StyleIdentifier = StyleIdentifier.Heading1;
builder.Writeln("Aspose.Words main advantages are:");
builder.ParagraphFormat.ClearFormatting();
builder.Writeln();

// Start a bulleted list with default formatting.
builder.ListFormat.List = list;
builder.Writeln("Great performance");
builder.ListFormat.ApplyBulletDefault();
builder.ListFormat.ListIndent();
builder.Writeln("High reliability");
builder.Writeln("Quality code and working");
builder.Writeln("Wide variety of features");
builder.Writeln("Easy to understand API");

// End the bulleted list.
builder.ListFormat.RemoveNumbers();

doc.Save(MyDir + "output.docx");

Totally what I was looking for.

Thanks so much for your assistance as usual Tahir!

@aspose.shrayasr,

Please feel free to ask if you have any question about Aspose.Words, we will be happy to help you.