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

Free Support Forum - aspose.com

Reset Numbering on Styled Numbered List

I am using the following code and attached template to generate a document containing numbered list paragraphs which are numbered using a style. Referencing attached output.docx, I would like to restart the numbering (at 1) at paragraph “D”. That is I would like:

  1. D
  2. E
    etc.

I tried adding a copy of the “List Number” list to do the document and then setting that copy into paragraph D’s ListFormat, but then every subsequent paragraph was numbered including those styled as “Normal” which should not be numbered.

Attachment: numbered-list.zip

		ClassLoader classLoader = getClass().getClassLoader();
		String fileName = classLoader.getResource("template.docx").toString().substring(5);
		com.aspose.words.Document document = new com.aspose.words.Document(fileName);
		document.removeAllChildren();
		DocumentBuilder builder = new DocumentBuilder(document);
		String listStyle = "List Number";
		builder.getCurrentParagraph().getParagraphFormat().setStyleName(listStyle);
		builder.write("A");
		builder.insertParagraph();
		builder.write("B");
		builder.insertParagraph();
		builder.write("C");
		builder.insertParagraph();
		builder.getCurrentParagraph().getParagraphFormat().setStyleName("Normal");
		builder.write("Not Numbered");
		builder.insertParagraph();
		builder.getCurrentParagraph().getParagraphFormat().setStyleName(listStyle);
		builder.write("D");
		builder.insertParagraph();
		builder.write("E");
		builder.insertParagraph();
		builder.write("F");
		builder.insertParagraph();
		builder.getCurrentParagraph().getParagraphFormat().setStyleName("Normal");
		builder.write("Not Numbered");
		document.save("numbered-list.docx");

@neallester,

You can fix the output.docx by using the following code:

Document doc = new Document("E:\\numbered-list\\output.docx");

Paragraph para = (Paragraph)doc.getChild(NodeType.PARAGRAPH, 4, true);
if (para.isListItem())
{
    com.aspose.words.List newlist = doc.getLists().addCopy(para.getListFormat().getList());
    //get the fifth paragaph
    Paragraph paragraph = (Paragraph)doc.getChild(NodeType.PARAGRAPH, 4, true);
    while (paragraph != null && paragraph.isListItem())
    {
        paragraph.getListFormat().setList(newlist);
        paragraph = (Paragraph)paragraph.getNextSibling();
    }
}

doc.save("E:\\numbered-list\\awjava-19.2.docx"); 

See output: awjava-19.2.zip (11.3 KB)

Thank you for the sample code. Unfortunately it does not demonstrate how to solve the problem I am having. My problem has nothing to do with manipulating a document using the document model. I need to understand how to control the way DocumentBuilder copies formatting information from the current paragraph to subsequent paragraphs. I am using DocumentBuilder because I also need to add field codes to the document and in a separate thread you recommended using DocumentBuilder for this purpose. If DocumentBuilder is not the correct tool for this task, how should I build this document from the template.

Assuming that I should use Documentbuilder, for a more complete example of what I am trying to do, how would I use DocumentBuilder to build this document from this template.

@neallester,

We are checking this scenario and will get back to you soon.

@neallester,

Considering this template.docx (template.zip (11.9 KB)), please try using the following code to get the desired list numbering:

Document doc = new Document("E:\\numbered-list\\template.docx");
doc.removeAllChildren();
DocumentBuilder builder = new DocumentBuilder(doc);

String listStyle = "List Number";
builder.getCurrentParagraph().getParagraphFormat().setStyleName(listStyle);
Paragraph para = builder.getCurrentParagraph();

builder.write("A");
builder.insertParagraph();
builder.write("B");
builder.insertParagraph();
builder.write("C");
builder.insertParagraph();
builder.getCurrentParagraph().getParagraphFormat().setStyleName("Normal");
builder.write("Not Numbered");
builder.insertParagraph();

com.aspose.words.List newlist = doc.getLists().addCopy(para.getListFormat().getList());
builder.getCurrentParagraph().getListFormat().setList(newlist);

builder.write("D");
builder.insertParagraph();
builder.write("E");
builder.insertParagraph();
builder.write("F");
builder.insertParagraph();

builder.getCurrentParagraph().getParagraphFormat().clearFormatting();
builder.getCurrentParagraph().getParagraphFormat().setStyleName("Normal");
builder.write("Not Numbered");

doc.save("E:\\temp\\numbered-list\\awjava-19.2.docx"); 

Hope, this helps.