We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com


Received : 2007/10/31 00:06:23
Message : I need to control the styles that are generated during insertHtml(). For example, when a


  • is encountered, I need it to use a specific Word Style vs. the current behavior where it applys adhoc formatting to the current paragraph. Is there a way to do this?
    This message was posted using Aspose.Live 2 Forum
  • Hi

    Thanks for your request. Unfortunately you can’t change style during inserting HTML. But you can try to change style after. See the following code example.

    Document doc = new Document();

    DocumentBuilder builder = new DocumentBuilder(doc);

    string html = "


    • test
    • test
    • test
    • test


    foreach (Paragraph paragraph in doc.FirstSection.Body.Paragraphs)


    if (paragraph.IsListItem)


    //change style here

    paragraph.ParagraphFormat.Style.Font.Bold = true;




    //change style here

    paragraph.ParagraphFormat.Style.Font.Italic = true;




    I hope that this will help you.

    Best regards.


    Thank you for your help. I really think that this is a common pattern, which would be easily supported by the API. More specifically, I would recommend (as an enhancement) to provide an alternate method:

    insertHtml( String html, Map styles )

    where styles is a key-value Map with the key being the HTML tag name (even optionally tagName.className combination) and the value is a Word style name. Accordingly, one can preconfigure a Word document with a set of styles to use for the inserted HTML. I think this would be really easy to add, since your HTML renderer must be using some sort of a formatting lookup for each encountered HTML tag. If a styles map is provided, it will look it up there first. If no match, then it will default to the current behavior.

    Hope you take this into consideration...would love to test it out for you:-)



    Thanks for your suggestion. We will consider this suggestion and provide you more information.

    Best regards.


    Thanks again for your suggestion. I created an issue #3991 in our defects database and we will investigate whether and when it can be done. Please expect a reply before the next hotfix (within 2-3 weeks).

    Best regards.

    Just wanted to check if this enhancement made it into the hotfix. Please advice.


    Unfortunately this issue is unresolved yet. We will notify you as soon as it is done. Thanks for your patience.

    Best regards.

    Hi there,

    I’m not sure if you are still looking for this functionality. I have taken a brief look at your issue and you can use some work around code below to achieve very similar functionality.

    InsertHtmlUsingDocumentStyles(builder, “.MyStyle{color:red;}<p class=“MyStyle”>Text


    public static void InsertHtmlUsingDocumentStyles(DocumentBuilder builder, string html)<o:p></o:p>


    Document tempDoc = new Document(new MemoryStream(Encoding.UTF8.GetBytes(html)));<o:p></o:p>

    NodeImporter importer = new NodeImporter(tempDoc, builder.Document,ImportFormatMode.UseDestinationStyles);<o:p></o:p>

    Node currentNode = builder.CurrentParagraph;<o:p></o:p>

    foreach (Node node in tempDoc.FirstSection.Body.ChildNodes)<o:p></o:p>


    Node importNode = importer.ImportNode(node, true);<o:p></o:p>

    currentNode.ParentNode.InsertAfter(importNode, currentNode);<o:p></o:p>

    currentNode = importNode;<o:p></o:p>





    The issues you have found earlier (filed as WORDSNET-1432) have been fixed in this .NET update and this Java update.

    This message was posted using Notification2Forum from Downloads module by aspose.notifier.