Link HTML Class to Custom Style in Word Document

Hi,
I have a DOTX template that contain custom styles and using Aspose.Words I load the template into a DocumentBuilder, insert HTML into the template and save it as a DOC file.
In my HTML I have a class called “highlight” and in my template I have a custom style called “highlight” as well. Is it possible to link the HTML class to the custom style in the template?
Attached I have an ASPX file and a DOTX file that shows what I want to do. In the aspx file you will see the highlight class in the html and in the template you will see the custom style called highlight, but they don’t get linked when the html is inserted into the template.
Thanks!

Hi Nathan,

Thanks for your inquiry.

Please note that, a CSS class defined in HTML is imported into the Aspose.Words Document Object Model as aStyle. Any content inserted from HTML with a CSS class set has the style automatically applied to it. Unfortunately, currently Aspose.Words does not support importing HTML table styles. Your request has been linked to the appropriate feature and you will be notified as soon as it is available. Sorry for inconvenience.

So if I have the following code:

builder.InsertHtml("<div class='highlight'>Test</div>");

Will the class highlight be imported into the Aspose Words Document Object Model as a style?
If so, will I be able to use that style to find each element that uses it?

Thanks!
Nathan

Hi Nathan,
Thank you for inquiry. Yes, the said class will be imported into the Aspose Words Document Object Model as a style but with different naming convention. In your case, div tag is converted to a paragraph with Normal style.
You can use this to identify it:https://docs.aspose.com/words/net/how-to-extract-selected-content-between-nodes-in-a-document/
Moreover, please follow up the example code snippet:

string divhtml = "<html><head><style type=\"text/css\"> div.highlight{color:red;}</style></head><body><div class='highlight'>Div Text</div></body></html>";

Aspose.Words.LoadOptions options = new Aspose.Words.LoadOptions();

options.LoadFormat = LoadFormat.Dotx;

// load dotx file in document object Document
doc = new Document(new MemoryStream(File.ReadAllBytes("c:/temp/test031/template1.dotx")), options);

DocumentBuilder builder = new DocumentBuilder(doc);

// insert html
builder.InsertHtml(divhtml);
doc.Save("c:/temp/test031/Out.doc", SaveFormat.Doc);

Here you can observe a paragraph with Div_highlight style. Please let me know if I can be of any further assistance.

Hi there,
Thanks for your inquiry.
Just in case it is useful, you can use the following work around code to achieve what you are looking for. This should work for most cases, although you may need to make sure how you structure your CSS names (so they are not imported into a document as P_MyStyle). This would not work with the work around code.
In the code below if the document associated with the builder already had a paragraph style called “MyStyle” then the inserted HTML content will use that existing style and no extra styles will be included in the document.

InsertHtmlUsingDocumentStyles(builder, "<html><head><style>.MyStyle{color:red;}</style></head><body>Text</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-318) have been fixed in this .NET update and this Java update.

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

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