This project has moved. For the latest updates, please go here.

Example code of applying styles

Aug 27, 2011 at 6:04 PM

Hello,

Maybe I am missing something obvious, but I haven't found any example of defining styles or using styles from a ready made document.

On the documentation page, you write the following:

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. 

Could you provide an example of applying styles?

Aug 27, 2011 at 6:14 PM

Let me clarify:

I mean a full example. The line provided in the documentation

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

doesn't make it obvious where is should be used. No context is given. Where does "Intense Quote" come from? When should someone set DefaultStyle?

A full example of setting a font/size/color on a "Heading 1" style would be very helpful.

Thanks!

Aug 27, 2011 at 6:24 PM

One last bit: I wanted to use

HtmlDocumentStyle.AddStyle(String name, Style style)

but noticed that it is internal.

Aug 30, 2011 at 11:08 PM

Finally, I have found the solution to my previous problem. This is a simple example of loading the styles from an existing document, no error handling. You will need a template.docx in the directory of the exe.

using System;
using System.IO;
using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Wordprocessing;
using NotesFor.HtmlToOpenXml;
using System.IO.Packaging;

class Program
{

static void Main(string[] args)
{

    string html =
        @"<h1>Heading 1 style</h1>
         Lorem ipsum <font face=""Courier"">dolor sit</font>amet,
         <font color='red'>consectetur</font> adipiscing elit.
         Duis dictum <font size=""x-lagre"">leo quis </font>ipsum
         <b>tempor nec <i>ultrices</i> sapien </b>elementum.";
  

    string targetfile = "target.docx";

    if (File.Exists(targetfile))
        File.Delete(targetfile);

    File.Copy("template.docx", targetfile);

    using (WordprocessingDocument wpdoc =
            WordprocessingDocument.Open(targetfile, true))
    {
        MainDocumentPart mainPart = wpdoc.MainDocumentPart;
        if (mainPart == null)
        {
            mainPart = wpdoc.AddMainDocumentPart();
            new Document(new Body()).Save(mainPart);
        }

        HtmlConverter converter = new HtmlConverter(mainPart);

        converter.HtmlStyles.StyleMissing +=
            delegate(object sender, StyleEventArgs e)
            {
                //when using templates, styles sholud not be missing
                //if they do this alerts
                Console.WriteLine("Missing style: " + e.Name);
            };

        Body body = mainPart.Document.Body;



        var paragraphs = converter.Parse(html);
        for (int i = 0; i < paragraphs.Count; i++)
        {
            body.Append(paragraphs[i]);
        }

        mainPart.Document.Save();
    }


}

 

Coordinator
Sep 5, 2011 at 12:05 PM
Edited Sep 27, 2011 at 1:27 PM

Hello,

I'm coming too late but I want to clarify some of your questions.

Intense Quote is the name of a predefined style injected by Word. But if you create a document from scratch, no styles exists!
I did not use an enum for al the predefined style because their name depends on the language of your MS Office! 


In fact, you did well in reusing a home-made template.

 

The DefaultStyle is applied as a fallback for any free text.

This is not easy to create a new font style with OpenXml because a lot of classes and parameters are required.
This is why I did like you : define everything with Word than append the converted Html in that document.

Jan 24, 2012 at 5:38 AM
onizet wrote:

Hello,

I'm coming too late but I want to clarify some of your questions.

Intense Quote is the name of a predefined style injected by Word. But if you create a document from scratch, no styles exists!
I did not use an enum for al the predefined style because their name depends on the language of your MS Office! 


In fact, you did well in reusing a home-made template.

 

The DefaultStyle is applied as a fallback for any free text.

This is not easy to create a new font style with OpenXml because a lot of classes and parameters are required.
This is why I did like you : define everything with Word than append the converted Html in that document.

Hello,

How to apply font name and font size?I am using beta version of htmltoOpenXml.dll.but still font style and fontsize not applied