Free Support Forum - aspose.com

Continue numbering for lists

I’m trying to create a numbering list that contains a text paragraph inside it:

  1. Main Section1
    1.1 Sub Section1.1
    1.2 Sub Section 1.2
    Some paragraph text 1.
    1.3 Sub Section 1.3
  2. Main Section2
    2.1 Sub Section2.1
    Some paragraph text 2.

My code is:

builder = new DocumentBuilder(doc);
builder.getListFormat().applyNumberDefault();
builder.writeln("Main Section1");

builder.getListFormat().setListLevelNumber(1);
builder.getListFormat().getListLevel().setNumberStyle(NumberStyle.ARABIC);
builder.getListFormat().getListLevel().setNumberFormat("\u0000.\u0001");

builder.writeln("Sub Section1.1 ");

builder.getListFormat().setListLevelNumber(1);
builder.writeln("Sub Section 1.2 ");

builder.getListFormat().removeNumbers();

builder.writeln("Some paragraph text 1.");

builder.getListFormat().applyNumberDefault();

builder.getListFormat().setListLevelNumber(1);
builder.getListFormat().getListLevel().setNumberStyle(NumberStyle.ARABIC);
builder.getListFormat().getListLevel().setNumberFormat("\u0000.\u0001");
//builder.getListFormat().getListLevel().setRestartAfterLevel(-1);
builder.writeln("Sub Section 1.3 ");

builder.getListFormat().setListLevelNumber(0);
builder.writeln("Main Section2");

builder.getListFormat().setListLevelNumber(1);
builder.writeln("Sub Section2.1");

builder.getListFormat().removeNumbers();
        
builder.writeln("Some paragraph text 2.");

doc.save("test.docx");

But I get the result (I get ‘1.1 Sub Section 1.3’ instead of ‘1.3 Sub Section 1.3’):

  1. Main Section1
    1.1 Sub Section1.1
    1.2 Sub Section 1.2
    Some paragraph text 1.
    1.1 Sub Section 1.3
  2. Main Section2
    2.1 Sub Section2.1
    Some paragraph text 2.

Question: how to continue the numbering for ‘Sub Section 1.3’?
I use Aspose.Words for Java 18.1.

@dyuzhev,

Thanks for your inquiry. To ensure a timely and accurate response, please ZIP and attach the following resources here for testing:

  • Your input Word document
  • Aspose.Words generated output document (DOCX file) which shows the undesired behavior
  • Your expected Word document showing the correct output. We will investigate the structure of your expected document as to how you want your final output be generated like. You can create expected document by using Microsoft Word.

As soon as you get these pieces of information ready, we will start further investigation into your issue and provide you more information. Thanks for your cooperation.

@awais.hafeez

thank you for your answer.
In the attached examples.zip (16.5 KB) you can find two files:
sample.docx - excepted document
test.docx - Aspose.Words generated output document.

I have an additional information about my question. Sure, I can call the method setStartAt, but I would like to use an automatic numbering method. I found the method ‘setRestartAfterLevel’, but how to use it? May be there is an example?

@dyuzhev,

We are working over your query and will get back to you soon.

@dyuzhev,

There are two lists in your source document. To get the desired output, Paragraph 0 and 5 both should belong to same List. And Paragraph 1, 2 and 4 should also belong to same List. And if you need to reset numbering of 6th Paragraph, you should simply reset it to the new list. Please see the following code:

Document doc = new Document("D:\\temp\\Examples\\test.docx");

Paragraph para0 = doc.getFirstSection().getBody().getParagraphs().get(0);
Paragraph para5 = doc.getFirstSection().getBody().getParagraphs().get(0);

para0.getListFormat().setList(doc.getLists().get(1));
para5.getListFormat().setList(doc.getLists().get(1));

Paragraph para1 = doc.getFirstSection().getBody().getParagraphs().get(1);
Paragraph para2 = doc.getFirstSection().getBody().getParagraphs().get(2);
Paragraph para4 = doc.getFirstSection().getBody().getParagraphs().get(4);

para1.getListFormat().setList(doc.getLists().get(0));
para2.getListFormat().setList(doc.getLists().get(0));
para4.getListFormat().setList(doc.getLists().get(0));

List listCopy = doc.getLists().addCopy(doc.getLists().get(1));
listCopy.getListLevels().get(0).setStartAt(2);

Paragraph para6 = doc.getFirstSection().getBody().getParagraphs().get(6);
para6.getListFormat().setList(listCopy);

doc.save("D:\\Temp\\Examples\\awjava-18.5.docx");

@awais.hafeez
thank you for the answer. But I don’t have a source document. I generate a document on-fly from external text file with some markup and I would like to use a feature to continue a numbering of list (sure, if there is a such feature)…

@dyuzhev,

You can manipulate lists by using the related List, ListLevel, ListFormat and ListLabel classes.

@awais.hafeez
I’ve made a lot of attempts with list numbering, but I can’t understand how set number ‘2.1’ to the first sub-item of list item 2. I.e. I get the result:

1	Main Section1
  1.1	Sub Section 1.1 
  1.2	Sub Section 1.2 
Some paragraph text 1.
  1.3	Sub Section 1.3 
2	Main Section2
  **1.1**	Sub Section 2.1
Some paragraph text 2.

but I need:

1	Main Section1
  1.1	Sub Section 1.1 
  1.2	Sub Section 1.2 
Some paragraph text 1.
  1.3	Sub Section 1.3 
2	Main Section2
  **1.2**	Sub Section 2.1
Some paragraph text 2.

My code is:

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

int counter_level0 = 1;
int counter_level1 = 1;
builder.getListFormat().setListLevelNumber(0);
builder.getListFormat().applyNumberDefault();
builder.getListFormat().getListLevel().setNumberStyle(NumberStyle.ARABIC);
builder.getListFormat().getListLevel().setNumberFormat("\u0000");
builder.getListFormat().getListLevel().setStartAt(counter_level0);
builder.writeln("Main Section1");
builder.getListFormat().removeNumbers();

builder.getListFormat().applyNumberDefault();
builder.getListFormat().setListLevelNumber(1);
builder.getListFormat().getListLevel().setNumberStyle(NumberStyle.ARABIC);
builder.getListFormat().getListLevel().setNumberFormat("\u0000.\u0001");
builder.getListFormat().getListLevel().setStartAt(counter_level1);
builder.writeln("Sub Section 1.1 ");
builder.getListFormat().removeNumbers();

counter_level1++;
builder.getListFormat().applyNumberDefault();
builder.getListFormat().setListLevelNumber(1);
builder.getListFormat().getListLevel().setNumberStyle(NumberStyle.ARABIC);
builder.getListFormat().getListLevel().setNumberFormat("\u0000.\u0001");
builder.getListFormat().getListLevel().setStartAt(counter_level1);
builder.writeln("Sub Section 1.2 ");
builder.getListFormat().removeNumbers();

builder.writeln("Some paragraph text 1.");

counter_level1++;
builder.getListFormat().applyNumberDefault();
builder.getListFormat().setListLevelNumber(1);
builder.getListFormat().getListLevel().setNumberStyle(NumberStyle.ARABIC);
builder.getListFormat().getListLevel().setNumberFormat("\u0000.\u0001");
builder.getListFormat().getListLevel().setStartAt(counter_level1);
builder.writeln("Sub Section 1.3 ");
builder.getListFormat().removeNumbers();

counter_level0++;
builder.getListFormat().applyNumberDefault();
builder.getListFormat().setListLevelNumber(0);
builder.getListFormat().getListLevel().setNumberStyle(NumberStyle.ARABIC);
builder.getListFormat().getListLevel().setNumberFormat("\u0000");
builder.getListFormat().getListLevel().setStartAt(counter_level0);
builder.writeln("Main Section2");
builder.getListFormat().removeNumbers();
        
counter_level1 = 1;
builder.getListFormat().applyNumberDefault();
builder.getListFormat().getListLevel().setRestartAfterLevel(0);
builder.getListFormat().setListLevelNumber(1);
builder.getListFormat().getListLevel().setNumberStyle(NumberStyle.ARABIC);
builder.getListFormat().getListLevel().setNumberFormat("\u0000.\u0001");
builder.getListFormat().getListLevel().setStartAt(counter_level1);
builder.writeln("Sub Section 2.1");
builder.getListFormat().removeNumbers();

builder.writeln("Some paragraph text 2.");

doc.save("test.docx");

Resulted docx is test.zip (6.0 KB)

What do I do wrong?

Thank you.

@dyuzhev,

The following code produces expected output (see awjava-18.6.zip (6.0 KB)):

Document doc = new Document("D:\\temp\\test\\test.docx");

Paragraph para0 = doc.getFirstSection().getBody().getParagraphs().get(0);
Paragraph para5 = doc.getFirstSection().getBody().getParagraphs().get(5);

para0.getListFormat().setList(doc.getLists().get(0));
para5.getListFormat().setList(doc.getLists().get(0));

Paragraph para1 = doc.getFirstSection().getBody().getParagraphs().get(1);
Paragraph para2 = doc.getFirstSection().getBody().getParagraphs().get(2);
Paragraph para4 = doc.getFirstSection().getBody().getParagraphs().get(4);

para1.getListFormat().setList(doc.getLists().get(1));
para2.getListFormat().setList(doc.getLists().get(1));
para4.getListFormat().setList(doc.getLists().get(1));

List listCopy = doc.getLists().addCopy(doc.getLists().get(1));
listCopy.getListLevels().get(0).setStartAt(2);

Paragraph para6 = doc.getFirstSection().getBody().getParagraphs().get(6);
para6.getListFormat().setList(listCopy);

doc.save("D:\\Temp\\test\\awjava-18.6.docx");

@awais.hafeez
your code processes concrete structure of document, but I would like to do this on-fly when I generate my document. It is possible? Or I have to run the post-processing like your code?

@dyuzhev,

Can you please use MS Word 2016 to generate desired output from the test.docx that you attached in previous post? Please also list the steps that you performed in MS Word to get the desired output. Please also attach the MS Word generated DOCX document here for our reference. Thanks for your cooperation.