Free Support Forum - aspose.com

Restart paragraph numbering

Hi support,

Please specify if there is any solution to reset paragraph numbering within the following code. I’m pointing to the same functionality as the “Restart numbering at 1” option in Ms Word.

Document doc = new Document(“C:\Temp\MyStylesheet.doc”);
DocumentBuilder docBuilder = new DocumentBuilder(doc);
Paragraph para = doc.getLastSection().getBody().getLastParagraph();
docBuilder.moveTo(para);

Style styleH1 = doc.getStyles().get(“Heading 1”);
Style styleH2 = doc.getStyles().get(“Heading 2”);

docBuilder.writeln();
docBuilder.getParagraphFormat().setStyle(styleH1);
docBuilder.write(“A”);

docBuilder.writeln();
docBuilder.getParagraphFormat().setStyle(styleH2);
docBuilder.write(“B”);

docBuilder.writeln();
docBuilder.getParagraphFormat().setStyle(styleH2);
docBuilder.write(“C”);

docBuilder.writeln();
//here the numbering must reset to 1
docBuilder.getParagraphFormat().setStyle(styleH1);
docBuilder.write(“D”);

docBuilder.writeln();
docBuilder.getParagraphFormat().setStyle(styleH2);
docBuilder.write(“E”);

docBuilder.writeln();
//here the numbering must reset to 1
docBuilder.getParagraphFormat().setStyle(styleH2);
docBuilder.write(“F”);

doc.save(“C:\Temp\docParagraphStyleNumberingReset.doc”);

Thank you for looking into this,
Milan

Hi Milan,<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thanks for your inquiry. You can try using code like the following:

Document doc = new Document("C:\\Temp\\MyStylesheet.doc");

DocumentBuilder docBuilder = new DocumentBuilder(doc);

Paragraph para = doc.getLastSection().getBody().getLastParagraph();

docBuilder.moveTo(para);

Style styleH1 = doc.getStyles().get("Heading 1");

Style styleH2 = doc.getStyles().get("Heading 2");

List HeadingLst = doc.getStyles().get("Heading 1").getListFormat().getList();

docBuilder.writeln();

docBuilder.getParagraphFormat().setStyle(styleH1);

docBuilder.getListFormat().setList(HeadingLst);

docBuilder.write("A");

docBuilder.writeln();

docBuilder.getParagraphFormat().setStyle(styleH2);

docBuilder.getListFormat().setList(HeadingLst);

docBuilder.write("B");

docBuilder.writeln();

docBuilder.getParagraphFormat().setStyle(styleH2);

docBuilder.getListFormat().setList(HeadingLst);

docBuilder.write("C");

docBuilder.writeln();

//here the numbering must reset to 1

//Create clone of H1 and H2 lists

List HeadingLst_copy = doc.getLists().addCopy(HeadingLst);

docBuilder.getParagraphFormat().setStyle(styleH1);

docBuilder.getListFormat().setList(HeadingLst_copy);

docBuilder.write("D");

docBuilder.writeln();

docBuilder.getParagraphFormat().setStyle(styleH2);

docBuilder.getListFormat().setList(HeadingLst_copy);

docBuilder.write("E");

docBuilder.writeln();

List HeadingLst_copy1 = doc.getLists().addCopy(HeadingLst);

//here the numbering must reset to 1

docBuilder.getParagraphFormat().setStyle(styleH2);

docBuilder.getListFormat().setList(HeadingLst_copy1);

docBuilder.write("F");

Hope this helps.

Best regards.

Hi Alexey,

It is possible to reset only the last number in the level ? Please note that if you add another heading 1 object after “D”, the “F” object has the composed number 1.1, instead of 2.1.

Thanks,
Milan

Hi Milan,

<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thanks for your inquiry. Actually, you know, lists in MS Word are very complex. Could you please create simple Word document, which will demonstrate your requirements. I will take a look and tell you how you can do the same in Aspose.Words.

Best regards.

Hi Alexey,

This is the code:

Document doc = new Document(“C:\Temp\headings.dot”);
DocumentBuilder docBuilder = new DocumentBuilder(doc);

Style styleH1 = doc.getStyles().get(“Heading 1”);
Style styleH2 = doc.getStyles().get(“Heading 2”);
Style styleH3 = doc.getStyles().get(“Heading 3”);

List HeadingLst = doc.getStyles().get(“Heading 1”).getListFormat().getList();

docBuilder.writeln();
docBuilder.getParagraphFormat().setStyle(styleH1);
docBuilder.getListFormat().setList(HeadingLst);
docBuilder.write(“A”);

docBuilder.writeln();
docBuilder.getParagraphFormat().setStyle(styleH2);
docBuilder.getListFormat().setList(HeadingLst);
docBuilder.write(“B”);

docBuilder.writeln();
docBuilder.getParagraphFormat().setStyle(styleH2);
docBuilder.getListFormat().setList(HeadingLst);
docBuilder.write(“C”);

docBuilder.writeln();
//here the numbering must reset to 1
List HeadingLst_copy = doc.getLists().addCopy(HeadingLst);
docBuilder.getParagraphFormat().setStyle(styleH1);
docBuilder.getListFormat().setList(HeadingLst_copy);
docBuilder.write(“D (reset)”);

docBuilder.writeln();
docBuilder.getParagraphFormat().setStyle(styleH1);
docBuilder.getListFormat().setList(HeadingLst_copy);
docBuilder.write(“D1”);

docBuilder.writeln();
docBuilder.getParagraphFormat().setStyle(styleH2);
docBuilder.getListFormat().setList(HeadingLst_copy);
docBuilder.write(“E”);

docBuilder.writeln();
List HeadingLst_copy1 = doc.getLists().addCopy(HeadingLst);
//here the numbering must reset to 1
docBuilder.getParagraphFormat().setStyle(styleH2);
docBuilder.getListFormat().setList(HeadingLst_copy1);
docBuilder.write(“F (reset)”);

doc.save(“C:\Temp\docParagraphStyleNumberingResetAspose.doc”);

I attach my result, and the desired result.

Thank you for looking into this,
Milan

Hi

<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thank you for additional information. In your expected document, you just set start numbed of the first level to 2. You can do the same using Aspose.Words:

docBuilder.writeln();

List HeadingLst_copy1 = doc.getLists().addCopy(HeadingLst);

HeadingLst_copy1.getListLevels().get(0).setStartAt(2);

//here the numbering must reset to 1

docBuilder.getParagraphFormat().setStyle(styleH2);

docBuilder.getListFormat().setList(HeadingLst_copy1);

docBuilder.write("F (reset)");

I think, you should calculate current numbers of each level while building the list in the document. In this case, you will be able to properly set start number, when you restart numbering.

Best regards.