DocumentBuilder.ListFormat.List not working as expected

I have the following code:

var builder = new DocumentBuilder();

builder.ListFormat.List = builder.Document.Lists.Add(default(ListTemplate));
builder.ListFormat.List.ListLevels[0].NumberStyle = NumberStyle.UppercaseLetter;
builder.ListFormat.List.ListLevels[1].NumberStyle = NumberStyle.Arabic;

builder.ListFormat.ListLevelNumber = 0;
builder.Writeln("This should be A");

builder.ListFormat.ListLevelNumber = 1;
builder.Writeln("This should be 1");

// end list
builder.ListFormat.List = null;

I’m expecting the output to be

 A. This should be A
     1. This should be 1

But the actual is just a bullet list. I think it’s because I used default(ListTemplate). But what if none of the built-in template matches my expected output? How should I customize each list level?

@gojanpaolo,

Please try using the following code to get the desired output:

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

builder.ListFormat.List = doc.Lists.Add(ListTemplate.NumberDefault);

builder.ListFormat.List.ListLevels[0].NumberStyle = NumberStyle.UppercaseLetter;
builder.ListFormat.List.ListLevels[1].NumberStyle = NumberStyle.Arabic;

builder.ListFormat.ListLevelNumber = 0;
builder.Writeln("This should be A");

builder.ListFormat.ListLevelNumber = 1;
builder.Writeln("This should be 1");

// end list
builder.ListFormat.List = null;

doc.Save("E:\\Temp\\19.5.docx");

Thanks for the response @awais.hafeez. Will any combination of NumberStyle (e.g. bullet, uppercase, lowercase, roman numeral, etc) work when using ListTemplate.NumberDefault?

@gojanpaolo,

Yes, there should not be a problem. You can also try different combinations on your end. If we can help you with anything else, please feel free to ask.

Got it! thanks!

@awais.hafeez It looks like ListTemplate.NumberDefault does not work with NumberStyle.Bullet

var builder = new DocumentBuilder();
builder.ListFormat.List = builder.Document.Lists.Add(ListTemplate.NumberDefault);
builder.ListFormat.ListLevel.NumberStyle = NumberStyle.Bullet;
builder.Writeln("This should be a bullet");

expected:

  • This should be a bullet

actual (bullet is not drawn, font style does not matter.):

    This should be a bullet

To give a little bit of context, we’re trying to create a reusable function for creating lists.

// usage
builder.BuildList(
    new ListConfiguration
    {
        NumberStyle = NumberStyle.UppercaseLetter,
        Items =
        {
            "This should be A",
            new ListConfiguration
            {
                NumberStyle = NumberStyle.Bullet,
                Items =
                {
                    "This should be bullet"
                }
            }
        }
    });
    

internal static class DocumentBuilderExtensions
{
    public static void BuildList(this DocumentBuilder builder, ListConfiguration listConfiguration)
    {
        // NumberDefault should work with any combination of NumberStyle
        // https://forum.aspose.com/t/documentbuilder-listformat-list-not-working-as-expected/196821
        builder.ListFormat.List = builder.Document.Lists.Add(ListTemplate.NumberDefault);

        var listLevel = 0;
        BuildList(listConfiguration);

        // end list
        builder.ListFormat.List = null;

        void BuildList(ListConfiguration current)
        {
            listLevel++;
            builder.ListFormat.ListLevelNumber = listLevel - 1;
            builder.ListFormat.ListLevel.NumberStyle = current.NumberStyle;
            foreach (var item in current.Items)
            {
                if (item is ListConfiguration subList)
                {
                    BuildList(subList);
                    listLevel--;
                    builder.ListFormat.ListLevelNumber = listLevel - 1;
                }
                else
                {
                    builder.Writeln(item.ToString());
                }
            }
        }
    }
}

internal sealed class ListConfiguration
{
    public NumberStyle NumberStyle { get; set; }

    public List<object> Items { get; } = new List<object>();
}

@awais.hafeez
Also, how can we build a list with different NumberStyle on the same ListLevel, or maybe have the same indent for multiple ListLevel?

builder.ListFormat.List = builder.Document.Lists.Add(ListTemplate.NumberDefault);
builder.ListFormat.ListLevel.NumberStyle = NumberStyle.UppercaseLetter;
builder.Writeln("This should be A");
builder.ListFormat.ListLevelNumber = 1;
builder.ListFormat.ListLevel.NumberStyle = NumberStyle.Arabic;
builder.Writeln("This should be 1");
builder.ListFormat.ListLevelNumber = 0;
builder.Writeln("This should be B");
builder.ListFormat.ListLevelNumber = 1;
builder.ListFormat.ListLevel.NumberStyle = NumberStyle.LowercaseLetter;
builder.Writeln("This should be a");

expected:

A. This should be A
    1. This should be 1
B. This should be B
    a. This should be a

@gojanpaolo,

You can define a separate List for Bullets for example using ListTemplate.BulletDiamonds. A document can have multiple Lists and you need to apply correct List to a particular Paragraph. Please also check the examples mentioned here:

@awais.hafeez Below code seems to work with both letter and bullet in the same list. Is there anything we should be aware with such implementation?

builder.ListFormat.List.ListLevels[0].NumberStyle = NumberStyle.UppercaseLetter;
builder.ListFormat.List.ListLevels[0].NumberFormat = "\u0000.";
builder.ListFormat.List.ListLevels[0].Font.Name = "Times New Roman";

builder.ListFormat.List.ListLevels[1].NumberStyle = NumberStyle.Bullet;
builder.ListFormat.List.ListLevels[1].NumberFormat = "·";
builder.ListFormat.List.ListLevels[1].Font.Name = "Symbol";

builder.ListFormat.ListLevelNumber = 0;
builder.Writeln("This should be A");

builder.ListFormat.ListLevelNumber = 1;
builder.Writeln("This should be bullet");

@gojanpaolo,

You can experiment with different combinations of ListTemplate/NumberStyle etc to see what suites your needs better. It is great that the above combination helps to achieve what you are looking for. If we can help you with anything else, please feel free to ask.