Tag Archives: .Net

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.

Document Generation with OOXML Part 1

For a while I have been working on a private project called Document Generator. Document Generator is a .Net library for creating text documents in OOXML and in ODF. With the library you can create an outline for a document with paragraphs, image, etc and generate an OOXML or ODF document. I recently thought it was mature enough to make it a final release. You can find the project at Codeplex .

To explain the library I decided to write a serie of articles and this is the first part. It will explain how to get started with the library.

Pre-requisites

The library is using OOXML SDK 2.5 for creating OOXML documents and this requires .Net Framework 4.0.

Installation

Document generator does not require any installation. Download the zip file with the latest version and extract the contents to a directory of choice. The zip file contains:

  • DocumentGenerator.dll
  • DocumentFormat.OpenXml.dll (This is the dll for the OOXML SDK 2.5)
  • ICSharpCode.SharpZipLib.dll (This dll is required for creating ODF files)

Setup the project

  1. Create a new console project in Visual Studio
  2. Add a reference to the dll’s for the document generation:
    • DocumentGenerator.dll
    • DocumentFormat.OpenXml.dll
    • ICSharpCode.SharpZipLib.dll

 Generate Documents

  1. The first step in generating a document is to create a Textdocument object. This is the container of the document to generate and is the same for OOXML and ODF documents. For OOXML documents it is also possible to start from a template (.dotx) or an existing document (.docx).
  2. //Create a new text document
    var document = TextDocument.Create();

    //Create a new text document with a file name (including the file location)
    //The Boolean indicates whether the file can be overridden if a file
    //with the same name is found.
    var document = TextDocument.Create("MyFilename", true);

    //Create a new text document based on a template
    var document = TextDocument.Create("MyFilename","myTemplate", true);

    //Open an existing ooxml document.
    var document = TextDocument.Open("MyFile");
  3. When the text document is created headers and paragraphs can be added.
  4. //Add a paragraph to the document. Level is an integer used for the style.
    //of the paragraph, e.g. level "0" set style "Normal"
    //and level "1" style "Normal1"
    document.AddParagraph("The paragraph text", level);

    //Add a paragraph and a header. Level is an integer is used for the style.
    //E.g. level "1" applies "Header1" level "2" applies "Header2"
    document.AddParagraph("The header","The paragraph text", level);

    //Add only a header. Leaving the text for the paragraph empty
    //will add only a header.
    document.AddParagraph("The header",string.Empty, level);
  5. Finally the document can be save in OOXML or ODF format.
  6. //First set a filename (including the file location), if not already set
    document.Filename = "MyFile";

    //Save the document in OOXML format.
    document.Save(DocumentType.OOXMLTextDocument);

    //Save the document in ODF format.
    document.Save(DocumentType.ODFTextDocument);

In this post I showed you how you can create a document, add contents to the document and save this document in either OOXML or ODF format.
I also showed how you can use a dotx template or an existing ooxml document to create new documents.

In the next post I will go through some more advanced features such as creating tables and lists and replace content in OOXML documents.
If you cannot wait for these posts you can have a look a the DocumentGeneratorTest project in Codeplex to see the use of these features.

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

Just another blog

This is the first blog post on my new blog DotNet Bits and Pieces. Like the name says I will blog here about the things that I will run into while working with the .Net Framework.

Because of my work most post will probably be about Visual Studio and C#, but we will see when time goes by.