Html classes to custom styles in word

Hi,
I have a DOTX template that contains custom styles and load the template into a DocumentBuilder, insert HTML into the template and save it as a DOCX file.
In my HTML I have classes which has same name as in my template. Is it possible to link the HTML class to the custom style in the template in order to keep formats defined in the template? OR Can we have any workaround?

Hi Vuong,

Thanks for your inquiry. First of all, please note that, a CSS class defined in HTML is imported into the Aspose.Words Document Object Model as a Style. Any content inserted from HTML with a CSS class set has the style automatically applied to it.

Secondly, could you please attach your input template Word document along with HTML file/string here for testing? I will investigate the issue further and provide you more information.

Best Regards,

Hi awais.hafeez,

I understand that "Any content inserted from HTML with a CSS class set has the style automatically applied to it". In my case, html has format like this:

<p class="resite_heading">Content of P</p>

And in my template file, I also has a custom style, named “resite_heading”. I wanna link html class to custom style in order to keep format defined in the template when I use the insertHTML method of DocumentBuilder.

I’m really sorry when I can not attach any file here because of my company policy.

Hi there,

Thanks for your inquiry.

I’m afraid all formatting on HTML snippets inserted into a document must be applied to the HTML itself. We will consider improving this functionality in the future so HTML inserted can use existing document styles. I have linked your request to the appropriate issue. You will be informed of any developments.

In the mean time you can try using the work around code to try achieve the same behaviour. 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><p class=\"MyStyle\">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.
(15)