DocumentBuilder.insertHtml()

Received : 2007/10/31 00:06:23

Message : I need to control the styles that are generated during insertHtml(). For example, when a <p> or <li> 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?

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 = "<p>test</p><ul><li>test</li><li>test</li><li>test</li><li>test</li></ul>";

builder.InsertHtml(html);

foreach (Paragraph paragraph in doc.FirstSection.Body.Paragraphs)
{
    if(paragraph.IsListItem)
    {
        //change style here
        paragraph.ParagraphFormat.Style.Font.Bold = true;
    }
    else
    {
        //change style here
        paragraph.ParagraphFormat.Style.Font.Italic = true;
    }
}

doc.Save(@"320_100709_ContentPilot\out.doc");

I hope that this will help you.

Best regards.

Alexey,
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:-)
Iyad

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

Hi,
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.

Hi
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, "<html><head><style>.MyStyle{color:red;}</style></head><body><p>Text</p></body></html>");

public static void InsertHtmlUsingDocumentStyles(DocumentBuilder builder, string html)
{
      Document tempDoc = new Document(new MemoryStream(Encoding.UTF8.GetBytes(html)));
      NodeImporter importer = new NodeImporter(tempDoc, builder.Document,ImportFormatMode.UseDestinationStyles);
      Node currentNode = builder.CurrentParagraph;
      foreach (Node node in tempDoc.FirstSection.Body.ChildNodes)
      {
          Node importNode = importer.ImportNode(node, true);
          currentNode.ParentNode.InsertAfter(importNode, currentNode);
          currentNode = importNode;
      }
      builder.MoveTo(currentNode);
}

Thanks,

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.
(2)