Default style

You can assign a style for all the paragraphs meet during the conversion. Of course, the style attributes on the tag are still applied over the specified default style.

In this example, the Intense Quote style is used.
defaultstyle.png

C#
converter.HtmlStyles.DefaultStyle = converter.HtmlStyles.GetStyle("Intense Quote");

Html
<h1>A title</h1>
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Duis dictum leo quis ipsum tempor nec ultrices sapien elementum.

Some texts does not apply the expected style

If you add this line of code in the startup example and run it again, you will not see the "Heading 1" style applied.
 <h1>First steps</h1>
In fact, the "Heading 1" style is well applied but this style does not exists in the document ; when you create a new document, no styles are defined : it's up to you. HtmlToOpenXml handles automatically the hyperlink style for you but it does not deals with any others.
You can subscribe to the StyleMissing event to be warned and add them yourself.

converter.HtmlStyles.StyleMissing += delegate(object sender, StyleEventArgs e)
{
    Console.WriteLine(e.Name);
};

Generally, you will load in memory an existing document template and append the Html conversion inside it. MS Word 2007 embeds a lot of informations (like styles, themes, document properties, ...) that you don't really want to bother with.

Last edited Apr 20, 2011 at 8:56 PM by onizet, version 4

Comments

DmitryPavlov Jan 23, 2012 at 2:19 PM 
Hi guys!

I see no StyleMissing event raised fr unknown styles. There are the steps I tried:

Initially I tried my HTML text that contains the

<del>deleted text</del>
and
<ins>inserted text</ins>

tags which are converted into

<w:r><w:rPr><w:strike /></w:rPr><w:t xml:space="preserve">deleted text</w:t></w:r>
and
<w:r><w:rPr><w:u w:val="single" /></w:rPr><w:t xml:space="preserve">inserted text</w:t></w:r>
correspondingly.

That is just nice. But then I decided to adjust the source HTML and replaced it with
<del><span style='NotSupportedDeleteStyle'> deleted text</span></del>
and
<ins><span style='NotSupportedInsertStyle'> inserted text</span></ins>

and subscribed the converter to StyleMissing event. I expected to handle missing NotSupportedDeleteStyle and NotSupportedInsertStyle styles - and add some style into document on the fly. But the even is never happens.

Could yo please point me - what is wrong?
Thank you in advance!