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

Free Support Forum - aspose.com

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("

Test
");

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:

http://www.aspose.com/documentation/.net-components/aspose.words-for-.net/howto-extract-content-based-on-styles.html


Moreover, please follow up the example code snippet:

string divhtml = “<style
type=“text/css”> div.highlight
{color:
red;}

Div
Text
”;

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, “.MyStyle{color:red;}<p class=“MyStyle”>Text

”);<o:p></o:p>

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

{<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>

{<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>

}<o:p></o:p>

builder.MoveTo(currentNode);<o:p></o:p>

}


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)