Apply Custom Paragraph (Numbering) Style to Generate Numbered List Items in Word Document using Java

Hi there,

i’m trying to use a custom list format (“ListFormat1”) which is defined in my word file. This format references to a style format (“Format1”) in the first layer.

When i create the list in word the style looks correct. But when i insert the list with aspose the listtext has standard format.

Am i doing something wrong here?

My code looks like this:

Document document = new Document("C:/TMP/ListTest.docx");
DocumentBuilder builder = new DocumentBuilder(document);
        
builder.moveToBookmark("Bookmark", true, true);
        
Style listStyle = document.getStyles().get("ListFormat1");
ListFormat listFormat = builder.getListFormat();
        
List list = document.getLists().add(listStyle);
listFormat.setList(list);
        
builder.write("Hello");
        
document.save("C:/TMP/ListTestOut.docx", SaveFormat.DOCX);

@benjamin.schueller,

Please compress the following resources into ZIP format and attach the .zip file here for testing:

  • Your simplified source Word document
  • Aspose.Words for Java 21.5 generated output DOCX file showing the undesired behavior
  • Your expected DOCX file showing the desired output. You can create this expected document manually by using MS Word.

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

Here are the wanted files.

WordFiles.zip (31.5 KB)

@benjamin.schueller,

Please use the following simple code to get the desired output:

Document document = new Document("C:\\Temp\\WordFiles\\ListTest.docx");
DocumentBuilder builder = new DocumentBuilder(document);

builder.moveToBookmark("Bookmark", true, true);

builder.getParagraphFormat().setStyleName("Format1");

builder.writeln("Hello");
builder.write("World");

document.save("C:\\Temp\\ordFiles\\awjava-21.5.docx");

I’m not so happy with this solution because my internal handling is very dynamic. And i thought that if i start a new list with defined format, this format will be used as default.

But for now i have a workaround which looks like this:

ListFormat currentListFormat = documentBuilder.getListFormat();
Style linkedListLevelStyle = currentListFormat.getListLevel().getLinkedStyle();
if (Objects.nonNull(linkedListLevelStyle)) {
  documentBuilder.getCurrentParagraph().getParagraphFormat().setStyle(linkedListLevelStyle);
}

So thanks for your help and maybe my improvement suggestion will be noticed.

@benjamin.schueller,

To investigate this scenario further, we have logged a ticket in our issue tracking system with ID WORDSNET-22283. We will further look into the details of this problem and will keep you updated on the status.