Insert Html List into word Document with custom Styling

Is there a way to insert Html into a word template that has a specific Style( Font, NumberList, Colour…)
My code looks like this:

DocumentBuilder builder = new DocumentBuilder( doc );
builder.moveToMergeField( mergeField.getFieldName() );
builder.getParagraphFormat().setStyleIdentifier(StyleIdentifier.HEADING_1);
builder.insertHtml( probablyMergeFields.get( keyWithoutPrefix ) );

The Problem is that it creates a Paragraph with the correct Styling before the inserted List, but the list is not styled correctly.

@heydet

To insert HTML content into a Word document with specific styling using Aspose.Words, you can utilize the DocumentBuilder class. However, when inserting HTML that includes lists, the styling may not always carry over as expected.

Here’s a general approach to ensure that your HTML list is styled correctly:

  1. Set the Desired Style Before Inserting HTML: Make sure to set the paragraph format and styles before you insert the HTML content. This includes setting the font, color, and any other formatting you want to apply.

  2. Use CSS for Styling: If your HTML includes lists, you can use inline CSS styles to define the appearance of the list items. This can help ensure that the styles are applied correctly when the HTML is inserted.

  3. Example Code: Below is an example of how you might structure your code to insert an HTML list with custom styling:

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

// Set the desired style for the paragraph
builder.getParagraphFormat().setStyleIdentifier(StyleIdentifier.NORMAL);
builder.getFont().setName("Arial");
builder.getFont().setSize(12);
builder.getFont().setColor(Color.BLACK);

// Insert HTML with a styled list
String htmlContent = "<ul style='list-style-type: square; color: blue;'>" +
                     "<li>Item 1</li>" +
                     "<li>Item 2</li>" +
                     "<li>Item 3</li>" +
                     "</ul>";
builder.insertHtml(htmlContent);

// Save the document
doc.save("StyledListDocument.docx");

In this example, the list is styled with a square bullet and blue color. Adjust the CSS styles as needed to fit your requirements.

If you find that the list is still not styled as expected, consider manually formatting the list items after insertion, as the HTML rendering may not always perfectly match the desired Word styles.

Feel free to adjust the styles and HTML content to meet your specific needs!

the problem is that the styling is defined in an word template document and cant be hard coded into the method

@heydet You can set usebuiltFormating flag in insertHtml method:

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

builder.getParagraphFormat().setStyleIdentifier(StyleIdentifier.HEADING_1);
String htmlContent = "<ul>" +
        "<li>Item 1</li>" +
        "<li>Item 2</li>" +
        "<li>Item 3</li>" +
        "</ul>";
builder.insertHtml(htmlContent, true);

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

out.docx (8.1 KB)

then i get a diffrent styling then what is defined.

the list should also start with “§” and then the numbers (e.g. §1,§2…)

@heydet Could you please attach your input document and HTML string that you are inserting? We will check the issue and provide you more information.

Thanks, thats the input document:
0_TB_DefaultOL - StyleStart.docx (24,9 KB)
and thats the string:

DocProp.VERTRAGSTEXT = <ol>
	<li>huhu</li>
	<li>haha</li>
	<li>huhu</li>
	<li>haha</li>
	<li>huhu</li>
	<li>haha</li>
	<li>huhu</li>
	<li>haha</li
</ol>

@heydet To get the expected output you should use simple paragraphs in your HTML instead of list items:

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

String htmlContent = "<p>Item 1</p>" +
        "<p>Item 2</p>" +
        "<p>Item 3</p>";

builder.insertHtml(htmlContent, HtmlInsertOptions.USE_BUILDER_FORMATTING | HtmlInsertOptions.REMOVE_LAST_EMPTY_PARAGRAPH);

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

out.docx (17.0 KB)