Monthly Archives: March 2013

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.