Skip to content
Olivier Nizet edited this page Jun 30, 2024 · 3 revisions

Stylesheet and style tag

External stylesheets or style tag are ignored. The recommended solution is to "inline the css". You can use a library such as PreMailer.Net to move CSS to inline style attributes.

string html = ResourceHelper.GetString("Resources.CompleteRunTest.html");
string css = ResourceHelper.GetString("Resources.style.css");
var result = PreMailer.Net.PreMailer.MoveCssInline(html, css: css);
html = result.Html;

HtmlConverter converter = new HtmlConverter(mainPart);
await converter.ParseHtml(html);

OpenXml style mapping

You can assign an OpenXml style for all the paragraphs met during the conversion. Style attributes on the tag are still applied over the specified default style.

Predefined Styles

When the converter encounters some tags (as a and h1) and the styles are missing from the Word document, the text is displayed in raw format, which doesn't give a nice visual feedback. To circumvent this behavior, the converter will automatically insert the missing styles if needed.

  • Caption
  • Heading 1 to 6
  • HyperLink
  • TableGrid
  • Footnote and Endnote
  • Quote

You can override the predefined styles by specifying yourself the style name in converter.HtmlStyles.DefaultStyles. In this example, the Intense Quote style is used. Predefined IntenseQuote sample

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.

CSS Class

If you wish to apply a specific style on an Html tag, you can use the native html class attribute. Each time the converter encounters the class attribute, it looks for a Style in the Word document with the same name (case insensitive). If it is not found, you can provide your own OpenXml style definition.

<table class="Standard_Table TableWhite" cellspacing="0" cellpadding="0">
<thead>
    <tr>
    <td>Column 1</td>
    <td>Column 2</td>
</tr>
</table>

In this sample, the converter will try to find a style with the name Standard_Table and if not found, TableWhite.

Provision your 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 deal with every styles. You can subscribe to the StyleMissing event to be warned and add them yourself.

converter.HtmlStyles.StyleMissing += delegate(object sender, StyleEventArgs args)
{
    if (args.Name == "custom-style")
    {
        converter.HtmlStyles.AddStyle(new Style() {
            StyleId = "custom-style",
            Type = args.Type,
            BasedOn = new BasedOn { Val = "Normal" },
            StyleRunProperties = new() {
                Color = new() { Val = HtmlColorTranslator.FromHtml("red").ToHexString() }
            }
        });
    }
};

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.