Monthly Archives: April 2013

Document Generation with OOXML Part 3

In a previous post I explained how the Document Generator library can be used with Word templates and how to use the styles defined in those templates. Besides using the styles in the template it should also be possible to insert specific content in the document. For example placing the title in the header of a document or the author of the document. Before Word 2007 and OOXML you could use fields or create a WordML file with your own tags in it to insert content programmaticly. With OOXML you can use Content Controls for this.
The Document Generator uses content controls to insert a information at specified places in the documents. The content control types that are supported are:

  1. Text
  2. Rich Text
  3. Picture

The content control must be defined in the OOXML document or template that is used with the library. This can be done from the Developer Tab in Word 2007 or higher.

//Create text document from a template
var document = TextDocument.Create("MyDocument.docx", "MyTemplate.dotx", true);

//Insert the author of document, where Author is the name of the content
//control and Shakespeare is the text that will be inserted.
document.ReplaceCustomTag("Author", "W. Shakespeare");

//Insert an image of the author, where AuthorImage is the name of the content
//control and Shakespeare.png is the image.
document.ReplaceCustomTag("AuthorImage", "Shakespeare.png");

The method name is the same for images and text. The library will determine the type of content control to find depending on the type that is given. When a string is given the library will look for a Picture content control and when a string is provided it will look for a Text control.
When it cannot find a match on both name and type it will ignore the replacement.
When a content control is defined more then once in the document it will insert the content for all the controls.

In this post I showed you how you can insert content such as text and images at specific places in documents by using Content Controls.
In the next part I will show how you can create list and tables.

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.