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

Free Support Forum - aspose.com

How to continue a previously ended list?

Hi There, I’m wondering how to continue a previous list.

I have a document which I wish to start a list, then write a line, then possible insert a table, then write a second list item, write a line, optionally include some other document sections etc.

I’m using a simple helper function to write my first list item (like this):

builder.ListFormat.ApplyNumberDefault();
builder.ParagraphFormat.Style.Font.Bold = true;
builder.Writeln(headerText);
builder.ListFormat.RemoveNumbers();
builder.InsertBreak(BreakType.LineBreak);

Then as this point I may want to write some other elements, tables, other paragraph text etc but after this conditional output I want to write item 2 of my original list (using the above helper again). Unfortunately at this point I get an entirely new list rather than a continuation, and I can’t seem to find a way to get the numbering to continue (a new list would be ok if the numbering would continue).

So my question is about how I can either continue a previous list or alternatively start a new list and continue the numbering. I’ve attached two simple docs to show the actual and expected document formats.

Hi Gavin,

Thanks for your inquiry. In your case, you need to set the Aspose.Words.Lists.List to DocumentBuilder.ListFormat.List after creating table or some other contents. Please check the following code example for your kind reference.

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
Aspose.Words.Lists.List list = doc.Lists.Add(ListTemplate.NumberDefault);
// Level 1 labels will be "Appendix A", continuous and linked to the Heading 1 paragraph style.
// list.ListLevels[0].NumberFormat = "Appendix \x0000";
// list.ListLevels[0].NumberStyle = NumberStyle.UppercaseLetter;
// list.ListLevels[0].LinkedStyle = doc.Styles["Heading 1"];
// Apply list formatting to the current paragraph.
builder.ListFormat.List = list;
builder.ListFormat.ListLevelNumber = 0;
builder.Writeln("List Item ");
builder.ListFormat.RemoveNumbers();
// We call this method to start building the table.
builder.StartTable();
builder.InsertCell();
builder.Write("Row 1, Cell 1 Content.");
// Build the second cell
builder.InsertCell();
builder.Write("Row 1, Cell 2 Content.");
// Call the following method to end the row and start a new row.
builder.EndRow();
// Build the first cell of the second row.
builder.InsertCell();
builder.Write("Row 2, Cell 1 Content");
// Build the second cell.
builder.InsertCell();
builder.Write("Row 2, Cell 2 Content.");
builder.EndRow();
// Signal that we have finished building the table.
builder.EndTable();
builder.ListFormat.List = list;
builder.Writeln("List item after table ");
builder.ListFormat.RemoveNumbers();
builder.Document.Save(MyDir + "Lists.Out.doc");

Moreover, I suggest you please check the ListLevel.RestartAfterLevel property. The property sets or returns the list level that must appear before the specified list level restarts numbering. Please check following code example for your kind reference.

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
Aspose.Words.Lists.List list = doc.Lists.Add(ListTemplate.NumberDefault);
// Level 1 labels will be "Appendix A", continuous and linked to the Heading 1 paragraph style.
list.ListLevels[0].NumberFormat = "Appendix \x0000";
list.ListLevels[0].NumberStyle = NumberStyle.UppercaseLetter;
list.ListLevels[0].LinkedStyle = doc.Styles["Heading 1"];
// Level 2 labels will be "Section (1.01)" and restarting after Level 2 item appears.
list.ListLevels[1].NumberFormat = "Section (\x0000.\x0001)";
list.ListLevels[1].NumberStyle = NumberStyle.LeadingZero;
// Notice the higher level uses UppercaseLetter numbering, but we want arabic number
// of the higher levels to appear in this level, therefore set this property.
list.ListLevels[1].IsLegal = true;
list.ListLevels[1].RestartAfterLevel = 0;
// Level 3 labels will be "-I-" and restarting after Level 2 item appears.
list.ListLevels[2].NumberFormat = "-\x0002-";
list.ListLevels[2].NumberStyle = NumberStyle.UppercaseRoman;
list.ListLevels[2].RestartAfterLevel = 1;
// Make labels of all list levels bold.
foreach (ListLevel level in list.ListLevels)
    level.Font.Bold = true;
// Apply list formatting to the current paragraph.
builder.ListFormat.List = list;
// Exercise the 3 levels we created two times.
for (int n = 0; n < 2; n++)
{
    for (int i = 0; i < 3; i++)
    {
        builder.ListFormat.ListLevelNumber = i;
        builder.Writeln("Level " + i);
    }
}
builder.ListFormat.RemoveNumbers();
builder.Document.Save(MyDir + "Lists.CreateListRestartAfterHigher Out.doc");

Hope this helps you. Please let us know if you have any more queries.

Hi Tahir,

That’s great, thanks for the reply, I now have the numbers appearing 1,2,3 as expected.

The one slight outstanding issue I have is that every row after the first one now has an additional line break after the number (shown in the attached doc).

Does anything look out of place in the following two methods I’m using to populate the header (numbered bit RequirementsHeader()) and then the non numbered bit (BlankText()).

public static void RequirementsHeader(string headerText, DocumentBuilder builder, List list)
{
    builder.ParagraphFormat.Style.Font.Size = 10;
    builder.ParagraphFormat.Style.Font.Name = GeneralText.FontName;
    builder.ParagraphFormat.Style.Font.Bold = true;
    builder.ListFormat.List = list;
    builder.Writeln(headerText);
    builder.ListFormat.RemoveNumbers();
    builder.InsertBreak(BreakType.LineBreak);
}

public static void BlankText(string text, DocumentBuilder builder)
{
    builder.ParagraphFormat.LeftIndent = 35;
    builder.Writeln(text);
}

Hi Gavin,

Thanks for your inquiry. In your case, I suggest you please use the DocumentBuilder.Write method in RequirementsHeader method as highlighted below.

DocumentBuilder.Write Method : Inserts a string into the document at the current insert position.

DocumentBuilder.Writeln Method (String) : Inserts a string and a paragraph break into the document.

public static void RequirementsHeader(string headerText, DocumentBuilder builder, List list)
{
    builder.ParagraphFormat.Style.Font.Size = 10;
    builder.ParagraphFormat.Style.Font.Name = GeneralText.FontName;
    builder.ParagraphFormat.Style.Font.Bold = true;
    builder.ListFormat.List = list;
    builder.Write(headerText);
    builder.ListFormat.RemoveNumbers();
    builder.InsertBreak(BreakType.LineBreak);
}

Please check the following code snippet for your kind reference.

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
Aspose.Words.Lists.List list = doc.Lists.Add(ListTemplate.NumberDefault);
// Apply list formatting to the current paragraph.
builder.ListFormat.List = list;
builder.ListFormat.ListLevelNumber = 0;
builder.Write("List item 1");
builder.InsertBreak(BreakType.LineBreak);
builder.Writeln("Blank text");
builder.Write("List item 1");
builder.InsertBreak(BreakType.LineBreak);
builder.Writeln("Blank text");
builder.Write("List item 1");
builder.InsertBreak(BreakType.LineBreak);
builder.Writeln("Blank text");
builder.ListFormat.RemoveNumbers();
builder.Document.Save(MyDir + "Out.docx");

Moreover, please check the ParagraphFormat.FirstLineIndent Property. This property gets/sets the value (in points) for a first line or hanging indent. Use a positive value to set a first-line indent, and use a negative value to set a hanging indent.

Hope this helps you. Please let us know if you have any more queries.

Thanks Tahir,

I will bear that in mind, for the time being I found a separate workaround which was to use the SpaceBefore and SpaceAfter paragraph formatting on the BlankText() function.

Cheers for the help

Hi Gavin,

Thanks for your feedback. It is nice to hear from you that you have found the solution of your issue. Please feel free to ask if you have any question about Aspose.Words, we will be happy to help you.