The Second Iteration of the Template Document

After great feedback by Svetlin, also after some more contemplation about tables, this post presents the second iteration on a template document to be used for a document generation process.

This post is the third in a series of blog posts. Here is the complete list: Generating Open XML WordprocessingML Documents Blog Post Series

One additional goal that I have for these document templates is that if necessary, the template designer can specify formatting for a field or for a cell in a table. To facilitate this, I’m going add the capability to specify the style in a separate nested content control.

In the following template, there are five content controls. The first is a value with a style. The second is a value that uses the style of the containing paragraph. The third generates a table from the query. The table is formatted with the table style of the sample table. The fourth shows conditional content. The last specifies that the user should be asked a question, the answer to which must be shorter than 256 characters.

I am certain that the design for this document template will be refined over the next couple of weeks.

!!!

5 Comments »

  1. Svetlin said,

    January 31, 2011 @ 7:49 am

    Hi Eric,

    Thanks for another interesting post !

    I just want to comment on the styles implementation. I know it’s a bit early still, but I’m trying to guess how you would process this template later.
    In your first example and any other for that matter – Why is there a need to set the style explicitly ?

    The way I addressed the styling (and I’m not saying it’s the right way) was to use the MS Word formatting that was applied at the time of template creation. And that is where the manner in which the placeholders are processed matters. A MS Word content control is defined by std that contains stdPr and stdContent elements. At processing time one can get a copy of the stdContent element first child(this is what you see on screen), replace the t element content with the relevant value and put that back into the document replacing the entire std element. In doing this one will never have to know what the actual formatting is, it just becomes a process of replacing content and swapping with the original. Same will apply for a table cell – it can be pre-formatted and at the time of processing just replace or insert a t element value if one doesn’t exist.

    I hope my reasoning is explained clearly enough and makes sense.

  2. Eric White said,

    February 1, 2011 @ 10:20 am

    Hi Svetlin,

    Yes, your reasoning makes sense. I agree, in many circumstances, it is fine to use the MS Word formatting that was applied at the time of template creation. However, I also envision the scenario where the template writer wants to apply different styling based on some criteria. Upon reflection, I think that this might be best served using the Conditional content control, so you are right, being able to specify the Word style may not be required.

    I am working on the next post, which will give more detail about how I envision the first version of this proof-of-concept will work.

    -Eric

  3. Nandy said,

    February 9, 2012 @ 6:06 am

    Hi We want to insert value into word document header plain text control. how we can do that ?

    we are replacing document controls using this code.

    using (WordprocessingDocument myDoc = WordprocessingDocument.Open(mem, true))
    {
    MainDocumentPart mainPart = myDoc.MainDocumentPart;

    foreach (Word.SdtElement sdt in mainPart.Document.Descendants().ToList())
    {
    if (sdt.Descendants().FirstOrDefault().Text == “projectname”)
    {
    sdt.Descendants().FirstOrDefault().Text = “This is T1 test project name”;
    }
    else if (sdt.Descendants().FirstOrDefault().Text == “productname”)
    {
    sdt.Descendants().FirstOrDefault().Text = “This is T2 test product”;
    }
    }
    }
    }

    but we want replace header controls how we can do that?

  4. Eric White said,

    March 30, 2016 @ 5:48 am

    @Nandy,

    I recommend that you take a look at DocumentAssembler, which will enable you to do this with little or no programming.

    Please watch the screen-casts at this link:

    http://www.ericwhite.com/blog/blog/documentassembler-developer-center/

    Cheers, Eric

  5. PREVENTING LIFESTYLE DISEASES Living Longer And Maintaining Great Health said,

    August 18, 2019 @ 9:10 am

    I like the valuable information you supply in your articles.
    I’ll bookmark your blog and test once more right here regularly.
    I’m somewhat certain I will learn many new stuff right here!
    Best of luck for the next!

RSS feed for comments on this post · TrackBack URI

Leave a Comment