Insert HTML (containing Bulleted Numbered Multi-Level List Paragraphs) in Word Document using Java

Hi,

I am using aspose word APIs.I am having a html where paragraghs containing the list styles like list id,list level and list numbering format(bullet or numbering etc) as their attributes.Using this attributes, i have to create a list in MSWord using aspose API. please help me.

My input looks like

<html>
<head>
    <body>
        <p><span>Normal Paragraph</span></p>
        <p listid="1" level="1" type="bullet">
            <span>level1</span>
        </p>
        <p listid="1" level="2" type="bullet">
            <span> level2</span>
        </p>
        <p listid="1" level="3" type="bullet">
            <span>level3</span>
        </p>
        <p listid="1" level="4" type="bullet">
            <span>level4</span>
        </p>
        <p><span>Normal paragraph</span></p>
    </body>
</head>
</html>

I have taken all data needed for list.Can anyone guide me which API i have to use.

Hi

Thanks for your request. Please try using the following code to create list:

// Open document.
Document doc = new Document();
 
DocumentBuilder builder = new DocumentBuilder(doc);
 
builder.InsertHtml("<ol>
<li>Items One</li>
<ol type='a'>
    <li>Item One</li>
    <li>Item Two</li>
    <li>Item Three</li>
</ol>
</li>
<li>Items Two</li>
<ol type='a'>
    <li>Item One</li>
    <li>Item Two</li>
    <li>Item Three</li>
</ol>
</li>
</ol>");
 
doc.save("C:\\Temp\\out.doc");

Maybe in your case, you can build lists programmatically. Please see the following code for example:

Document doc = new Document();
 
// Create a list based on one of the Microsoft Word list templates.
List list = doc.getLists().add(ListTemplate.NUMBER_DEFAULT);
 
// Completely customize one list level.
ListLevel level1 = list.getListLevels().get(0);
level1.setNumberStyle(NumberStyle.ARABIC);
level1.setNumberFormat("\u0000)");
 
// Completely customize yet another list level.
ListLevel level2 = list.getListLevels().get(1);
level2.setNumberStyle(NumberStyle.LOWERCASE_LETTER);
level2.setNumberFormat("\u0001)");
level2.setRestartAfterLevel(0);
 
// Now add some text that uses the list that we created.
// It does not matter when to customize the list - before or after adding the paragraphs.
DocumentBuilder builder = new DocumentBuilder(doc);
 
builder.getListFormat().setList(list);
builder.writeln("Items One");
 
builder.getListFormat().listIndent();
builder.writeln("Item One");
builder.writeln("Item Two");
builder.writeln("Item Three");
 
builder.getListFormat().listOutdent();
builder.writeln("Items Two");
 
builder.getListFormat().listIndent();
builder.writeln("Item One");
builder.writeln("Item Two");
builder.writeln("Item Three");
 
builder.getListFormat().removeNumbers();
 
// Save ouput document.
doc.save("C:\\Temp\\out.doc");

Hope this helps.

Best regards,

Thanks for your reply. May i have sample java code for this task.Here we are not using list id, then how to differentiate between lists when creating. If i have number of lists in my html, how to start and stop creating the list.

Hi

Thanks for your request. The code provided by Andrey shows how to create a simple list in MS Word document. If you need to create few lists, you can use the similar technique. Just create a new instance of List object for each separate list. You can find an example here:

  • List Class - e.g. you can use it to specify list level number when building a list using DocumentBuilder

Also, if you create a document that shows us the expected output we will help you to create a code.

Best regards,