Tag Archives: C#

Document Generation with OOXML Part 2

When creating a document from Word templates are used when creating documents. These can be default templates or templates created by users. It is their for essential that these templates can be used when generating OOXML documents. With the Document Generator library templates can be loaded when a new document is created.

//Load a template from file. document is the name of the new document.
//template is the name and full path of the template file
//true indicates that the document can override documents with the same name.
var document = TextDocument.Create(document, template, true);

//Load a template from a stream
using (var stream = new FileStream(templateFile, FileMode.Open, FileAccess.Read))
{
    using (var reader = new StreamReader(stream))
    {
        //stream is the stream of the template.
        var document = TextDocument.Create(stream);
    }
}

Once the template is loaded the styles defined in the template can be used. As long as the styles are the default for the header and the paragraphs only the level needs to be defined.

//This adds a new paragraph with the paragraph text and heading text.
//The styles used are Heading2 and Normal2. When the styles cannot be found then
//Heading1 and normal will be used as styles.
var paragraph = document.AddParagraph("Paragraph Text", "Heading Text", 2);

Templates can also define custom styles. When the name of the style in the template is known the style can be used with the Document Generator library.
The style can be applied to paragraphs and headers

//Creates a paragraph and assigns it the MyParagraphStyle.
var paragraph = document.AddParagraph("This is the paragraph", "MyParagraphStyle");

//Creates a paragraph and assigns it the MyParagraphStyle.
//and gives it a header with the style MyHeaderStyle
var paragraph = document.AddParagraph("This is the paragraph", "Header for Paragraph", "MyParagraphStyle", "MyHeaderStyle");

In this post I showed you how you can use a template when creating documents and how to use custom styles in the template.
In the next part I will show how you can replace content in documents using Content Controls.

Enum.HasFlag() versus Bitwise comparison

Call me a bit slow, but I recently discovered that Microsoft has implemented an HasFlag method on the Enum class since .Net 4. The method is meant as replacement for the bitwise comparison between values of an enumeration that has the FlagAttribute. For more information about the the FlagAttribute and bitwise comparison you can have a look at this blog.

For a project I am working on I am using the bitwise comparison extensively, so using the HasFlag method would make the code much more readable. Before using it I wanted to get some more information about how the HasFlag method works and I found this article. It shows the inner workings of the method, but also mentions that there is a performance cost to pay. However it does not mention what the cost is.

I therefor did my own test to see what the performance overhead would be and if it was worth to change. Continue reading