Creating Number list for Word Report

Hi
I have to create the number list for the given scenario. I am using Headings 2 and ListParagraph as the Style Identifier
Heading2 should start from 2.1 (continue for every heading
List paragraph should use a b c as number (start new for every heading2)

eg,
2.1 Section1
a. summary text
b. criteria text
2.2 Section 2
a. summary text
b. criteria text

I am generating all the text with writeln and using empty writeln to give open line space
Attached is the code used to generate the word.
Please help to identify and set the numbering for each heading and list paragraphGenerate Headings and Paragraphs.zip (1.5 KB)

@mcvmega To get the desired output, you can apply the list directly to the style. For example see the following code:

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

// Configure list.
com.aspose.words.List list = doc.getLists().add(ListTemplate.NUMBER_ARABIC_DOT);
list.getListLevels().get(1).setNumberStyle(NumberStyle.ARABIC);
list.getListLevels().get(1).setNumberFormat("\u0000.\u0001");
list.getListLevels().get(0).setStartAt(2);
// Configure list formatting for Heading 2 style.
Style heading2 = doc.getStyles().getByStyleIdentifier(StyleIdentifier.HEADING_2);
heading2.getListFormat().setList(list);
heading2.getListFormat().setListLevelNumber(1);

// Create heading paragraph.
builder.getParagraphFormat().setStyleIdentifier(StyleIdentifier.HEADING_2);
builder.writeln("This is heading paragraph");
builder.getParagraphFormat().clearFormatting();
// Start numbering
builder.getListFormat().setList(doc.getLists().add(ListTemplate.NUMBER_LOWERCASE_LETTER_DOT));
builder.writeln("summary text");
builder.writeln("criteria text");
builder.getParagraphFormat().clearFormatting();
// Create another heading paragraph.
builder.getParagraphFormat().setStyleIdentifier(StyleIdentifier.HEADING_2);
builder.writeln("This is heading paragraph");
builder.getParagraphFormat().clearFormatting();
// Start numbering
builder.getListFormat().setList(doc.getLists().add(ListTemplate.NUMBER_LOWERCASE_LETTER_DOT));
builder.writeln("summary text");
builder.writeln("criteria text");
builder.getParagraphFormat().clearFormatting();

doc.save("C:\\Temp\\out.docx");

Here is the produced output: out.docx (8.2 KB)

Thanks for the reply @alexey.noskov

It is working partially. Can you please reply for the below clarifications?

  1. how to reduce the space tab between numbering and text and

  2. After Heading2 style, how to set the a. b. c. bullets for ListParagraph style and to reset for every new Heading2.
    From your reply to apply list a b c for subsection is not consecutive in my case. So I want to apply the style and reset every Heading 2

@mcvmega

  1. You can use ListLevels.TabPosition to configure the tab.
  2. I think in your case you can assig the same list to ListParagraph style as used by Heading2 style, but specify the next level. Then numbering will be restarted after each heading. For example see the following modified code:
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);

// Configure list.
com.aspose.words.List list = doc.getLists().add(ListTemplate.NUMBER_ARABIC_DOT);
// Configure the first level
list.getListLevels().get(0).setStartAt(2);
// Configure the second level.
list.getListLevels().get(1).setNumberStyle(NumberStyle.ARABIC);
list.getListLevels().get(1).setNumberFormat("\u0000.\u0001");
list.getListLevels().get(1).setNumberPosition(0); // configure number position
list.getListLevels().get(1).setTabPosition(50); // configure tab position
list.getListLevels().get(1).setTextPosition(50); // configure text position
// Configure the third level
list.getListLevels().get(2).setNumberFormat("\u0002.");
list.getListLevels().get(2).setNumberStyle(NumberStyle.LOWERCASE_LETTER);
list.getListLevels().get(2).setNumberPosition(20); // configure number position
list.getListLevels().get(2).setTabPosition(30); // configure tab position
list.getListLevels().get(2).setTextPosition(30); // configure text position

// Configure list formatting for Heading 2 style.
Style heading2 = doc.getStyles().getByStyleIdentifier(StyleIdentifier.HEADING_2);
heading2.getListFormat().setList(list);
heading2.getListFormat().setListLevelNumber(1);
// Configure formatting for ListParagraph style.
Style listParagraph = doc.getStyles().getByStyleIdentifier(StyleIdentifier.LIST_PARAGRAPH);
listParagraph.getListFormat().setList(list); // Use the same list as for heading paragraph, but with different level.
listParagraph.getListFormat().setListLevelNumber(2);

// Create heading paragraph.
builder.getParagraphFormat().setStyleIdentifier(StyleIdentifier.HEADING_2);
builder.writeln("This is heading paragraph");
builder.getParagraphFormat().clearFormatting();
// Start numbering
builder.getParagraphFormat().setStyleIdentifier(StyleIdentifier.LIST_PARAGRAPH);
builder.writeln("summary text");
builder.writeln("criteria text");
builder.getParagraphFormat().clearFormatting();
// Create another heading paragraph.
builder.getParagraphFormat().setStyleIdentifier(StyleIdentifier.HEADING_2);
builder.writeln("This is heading paragraph");
builder.getParagraphFormat().clearFormatting();
// Start numbering
builder.getParagraphFormat().setStyleIdentifier(StyleIdentifier.LIST_PARAGRAPH);
builder.writeln("summary text");
builder.writeln("criteria text");
builder.getParagraphFormat().clearFormatting();

doc.save("C:\\Temp\\out.docx");

And here is the output document: out.docx (8.2 KB)

1 Like

@alexey.noskov I have tried your code. Somehow it is not generating for the `code below. “Summary Text has the List Paragraph” but numbering not generated

// Configure list.
com.aspose.words.List list = this.finaldoc.getLists().add(ListTemplate.NUMBER_ARABIC_DOT);
list.getListLevels().get(1).setNumberStyle(NumberStyle.ARABIC);
list.getListLevels().get(1).setNumberFormat("\u0000.\u0001");
list.getListLevels().get(0).setStartAt(2);
// Configure list formatting for Heading 2 style.
Style heading2 = this.finaldoc.getStyles().getByStyleIdentifier(StyleIdentifier.HEADING_2);
heading2.getListFormat().setList(list);
heading2.getListFormat().setListLevelNumber(1);

list.getListLevels().get(2).setNumberFormat("\u0002.");
list.getListLevels().get(2).setNumberStyle(NumberStyle.LOWERCASE_LETTER);
// Configure formatting for ListParagraph style.
Style listParagraph = this.finaldoc.getStyles().getByStyleIdentifier(StyleIdentifier.LIST_PARAGRAPH);
listParagraph.getListFormat().setList(list); // Use the same list as for heading paragraph, but with different level.
listParagraph.getListFormat().setListLevelNumber(2);

//Name of the Findings
this.builder.getParagraphFormat().setStyleIdentifier(StyleIdentifier.NORMAL);
this.builder.writeln();
this.builder.getParagraphFormat().setFirstLineIndent(0);
this.builder.getParagraphFormat().setLeftIndent(0.5);
this.builder.getParagraphFormat().setStyleIdentifier(StyleIdentifier.HEADING_2);
this.builder.getFont().setUnderline(0);
this.builder.getFont().setSize(14);
this.builder.getFont().setName("Times New Roman");
this.builder.getFont().setBold(true);
this.builder.getParagraphFormat().setAlignment(ParagraphAlignment.LEFT);
this.builder.writeln((String)find.getProp(Constants.MA_SHORTNAME, "Display"));
//this.builder.getParagraphFormat().clearFormatting();
this.builder.getParagraphFormat().setStyleIdentifier(StyleIdentifier.NORMAL);
this.builder.writeln();


//Summary
this.builder.getParagraphFormat().setStyleIdentifier(StyleIdentifier.HEADING_3);
this.builder.getFont().setSize(13);
this.builder.getFont().setName("Times New Roman");
this.builder.getFont().setBold(true);
this.builder.getFont().setUnderline(1);
this.builder.writeln("Summary: ");
this.builder.getParagraphFormat().setSpaceBefore(0);
this.builder.getParagraphFormat().setSpaceAfter(0);
this.builder.getParagraphFormat().setStyleIdentifier(StyleIdentifier.NORMAL);
this.builder.writeln();

//Summary text
this.builder.getParagraphFormat().setStyleIdentifier(StyleIdentifier.LIST_PARAGRAPH);
this.builder.getParagraphFormat().setAlignment(ParagraphAlignment.JUSTIFY);
this.builder.getParagraphFormat().setSpaceBefore(0);
this.builder.getParagraphFormat().setSpaceAfter(0);
this.builder.getFont().setSize(13);
this.builder.getFont().setBold(false);
this.builder.getFont().setUnderline(0);
//this.builder.getListFormat().setList(this.finaldoc.getLists().add(ListTemplate.NUMBER_LOWERCASE_LETTER_DOT));
this.builder.write((String)find.getProp("~)tQL(uByE1sO[Detailed Description]", "Display"));
//this.builder.getParagraphFormat().clearFormatting();
this.builder.getParagraphFormat().setStyleIdentifier(StyleIdentifier.NORMAL);
this.builder.writeln();

@mcvmega Please use the following line:

this.builder.writeln((String)find.getProp("~)tQL(uByE1sO[Detailed Description]", "Display"));

instead of

this.builder.write((String)find.getProp("~)tQL(uByE1sO[Detailed Description]", "Display"));

In your code you use the following:

this.builder.write((String)find.getProp("~)tQL(uByE1sO[Detailed Description]", "Display"));
builder.getParagraphFormat().setStyleIdentifier(StyleIdentifier.NORMAL);

i.e. style is reset to Normal for the paragraph.

1 Like

@alexey.noskov Thanks Numbering List are working fine
from the below example I have two paragraphs, when it is generated, empty line also generated with numbering (f - higlighted). Is it possible to avoid empty line numbering?

@mcvmega Please use code like the following:

// Has numbering
builder.getParagraphFormat().setStyleIdentifier(StyleIdentifier.LIST_PARAGRAPH);
builder.writeln("test");
// Does not have numbering
builder.getParagraphFormat().setStyleIdentifier(StyleIdentifier.NORMAL);
builder.writeln();
// Has numbering
builder.getParagraphFormat().setStyleIdentifier(StyleIdentifier.LIST_PARAGRAPH);
builder.writeln("test");
// Doe not have numbering
builder.getParagraphFormat().setStyleIdentifier(StyleIdentifier.NORMAL);
builder.writeln();

@alexey.noskov

In my case, I am reading a string formatted to RTF, I have two paragraphs in a string where it produced a numbering between the first paragraph and the second paragraph with the empty line.

can we avoid numbering for the empty line in this case?

@mcvmega Unfortunately, the described problem is not quite clear to me. Could you please attach your input RTF string and provide simple code that will allow us to reproduce the problem? We will check the scenario on our side and provide you more information.
If your requirement is to remove numbering from the empty paragraphs in your document, you can use code like this:

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

Iterable<Paragraph> paragraphs = doc.getChildNodes(NodeType.PARAGRAPH, true);
for (Paragraph p : paragraphs)
{
    if (p.isListItem() && !p.hasChildNodes())
        p.getListFormat().removeNumbers();
}

doc.save("C:\\Temp\\out.docx");

in.docx (13.4 KB)
out.docx (11.0 KB)

@alexey.noskov I have solved the issue. I will explain my case here

I have the RTF string coming as follows
String txt = "Line 1 \n Line2"; builder.writeln(txt);

the above code produces the output paragraph with numbering as follows
a. Line1
b.
c. Line2
In this case, I don’t want “b.” to be generated numbering for empty line.

What I did as follows
String txt = "Line 1 \n Line2"; String[] splitm = txt.split("\n"); for(String para : splitm) { para = para.trim(); if(para.isEmpty()) { continue; } this.builder.writeln(para); }

Now the output produced as
a. Line 1
b. Line 2

@mcvmega It is perfect that you managed to resolve the problem. Please let us know in case of any further issues. We will be glad to assist you.

1 Like