3
votes

TL;DR Can I generate Word documents with .NET like XAML ItemTemplates?

I'm finding it pretty hard to come up with a solution that meets all my requirements, so I thought I'll throw this out to stackoverflow in the hope someone can guide me. Much thanks in advance.

Simply put, I need to generate Word document based on data from a database.

My ideal solution: I want it to work like how DataTemplates work in xaml. I've found heaps of examples and solutions where the Template represents a static document, with bits of (single) dynamic content which needs to be replaced.

e.g. WordDocGenerator

The thing is, I need a solution where I can specify a template for each level of the hierarchy, and the document generator will use these item level templates to build a final document based on a Document level template.

My specific requirements are:

  • Preferably .NET solution
  • No need for office to be installed on the server
  • A template exists that encapsulates purely the 'View' of the document (doesn't necessarily have to be a Word template) which a user can modify at will (within boundaries of course). This is very important, as the user needs to control the presentation by just modifying the Word template directly.
  • Preferably at each level of data hierarchy there will be an accompanying template.
  • Headers and footers
  • Table of Contents

Let's say the data hierarchy is like this

class Country
{
  public string Name { get; set; }
  public IList<City> { get; set; }
}

class City
{
  public string Name { get; set; }
  public IList<Suburb> { get; set;}
}

class Suburb
{
  public string Name { get; set; }
  public int Postcode { get; set; }
}

In my mind the solution will be a function call, which accepts a list of countries.

// returns path to generated document
public static string GenerateDocument(IList<Country> countries);

This function will accept the list of countries, and for each country

  • use a prepared template for Countries to present the country data.
  • for each City in country, use prepared template for Cities to present the City data inside the Country template
  • for each Suburb in city, use prepared template for Suburbs to present the Suburb data inside the City template.

Finally, these generated document 'pieces' will be accumulated into one final Word Document using a Document level template, which will specify the Title page, headers/footers, TOC.

2
docx documents are ZIP archives of plain old XML. The sky is the limit, you can manipulate the XML however you want without even needing to have Office installed, and no need for a third-party library; just set up bookmarks in the Word template around the sections that you want to act as Country-, City-, Suburb-subtemplates, open up the docx manually to see what that looks like in XML, then programatically open the template XML, pull the bookmarked sections, and replicate them for as many countries/cities/etc. you have.vladr
Ok, so I can theoretically have these Item level templates? So in my mind I think what you're saying is use OpenXMl to load the nodes, like any other XML, and programmatically find and replace the 'bookmarked' stubs with content? After that insert the nodes into the final Document template?syclee
Exactly. If you look inside the docx as a zip archive you'll see word\document.xml, and in there you'll see e.g. <w:bookmarkStart w:id="0" w:name="country_template"/><w:p...><...>This is country #Country</...></w:p><w:bookmarkStart w:id="1" w:name="city_template"/><w:p...><...>This is city #City</...></w:p><w:bookmarkStart w:id="2" w:name="suburb_template"/><w:p...><...>This is suburb #Suburb</...></w:p><w:bookmarkEnd w:id="2"/><w:bookmarkEnd w:id="1"/><w:bookmarkEnd w:id="0"/>vladr
yeah, just as @vladr says. you may want to spend some time reading through Eric White's stuff at ericwhite.com/blog and blogs.msdn.com/b/ericwhite. also, openxmldeveloper.org has a bunch of articles as well.Todd Main

2 Answers

3
votes

Templater was designed with that use case in mind.

You can define regions of documents (like tables or lists) which will be duplicated based on current object that is being processed.

Disclamer: I'm the author.

1
votes

I eventually got what I wanted. I did everything manually, with a lot of help from Eric White's articles.

So a taste of the source is this. Have a template ensuring the first three paragraphs are the 3 levels of hierarchy you want. Loop through your collections, clone the node, replace the text, repeat.

private const string COUNTRY_TITLE = "[[CountryTitle]]"
private const string CITY_TITLE = "[[CityTitle]]"
private const string SUBURB_TITLE = "[[SuburbTitle]]"

using (WordprocessingDocument myDoc = WordprocessingDocument.Open(outputPath, true))
{
    var mainPart = myDoc.MainDocumentPart;
    var body = mainPart.Document.Body;

    var originalCountryPara = body.ElementAt(0);
    var originalCityPara = body.ElementAt(1);
    var originalSuburbPara = body.ElementAt(2); 

    foreach (var country in Countries)
    {
        if (!String.IsNullOrEmpty(country.Title))
        {
            // clone Country level node on template
            var clonedCountry = originalCountryPara.CloneNode(true);

            // replace Country title
            Helpers.CompletelyReplaceText(clonedCountry as Paragraph, COUNTRY_TITLE,  country.Title);
            body.AppendChild(clonedCountry);
        }    

        foreach (var city in country.Cities)
        {
            if (!String.IsNullOrEmpty(city.Title))
            {
                // clone City level node on template
                var clonedCity = originalCityPara.CloneNode(true);

                // replace City title
                Helpers.CompletelyReplaceText(clonedCity as Paragraph, CITY_TITLE, city.Title);
                body.AppendChild(clonedCity);
            }

            foreach (var suburb in city.Suburbs)
            {
                // clone Suburb level node on template
                var clonedSuburb = originalSuburbPara.CloneNode(true);

                // replace Suburb title
                Helpers.CompletelyReplaceText(clonedSuburb as Paragraph, SUBURB_TITLE, suburb.Title);
                body.AppendChild(clonedSuburb);         
            }
        }
    }

    body.RemoveChild(originalCountryPara);
    body.RemoveChild(originalCityPara);
    body.RemoveChild(originalSuburbPara);

    mainPart.Document.Save();
}

/// <summary>
/// 'Completely' refers to the fact this method replaces the whole paragraph with newText if it finds
/// existingText in this paragraph. The only thing spared is the pPr (paragraph properties)
/// </summary>
/// <param name="paragraph"></param>
/// <param name="existingText"></param>
/// <param name="newText"></param>
public static void CompletelyReplaceText(Paragraph paragraph, string existingText, string newText)
{
    StringBuilder stringBuilder = new StringBuilder();            
    foreach (var text in paragraph.Elements<Run>().SelectMany(run => run.Elements<Text>()))
    {                
        stringBuilder.Append(text.Text);
    }

    string paraText = stringBuilder.ToString();
    if (!paraText.Contains(existingText)) return;

    // remove everything here except properties node                
    foreach (var element in paragraph.Elements().ToList().Where(element => !(element is ParagraphProperties)))
    {
        paragraph.RemoveChild(element);
    }

    // insert new run with text
    var newRun = new Run();
    var newTextNode = new Text(newText);
    newRun.AppendChild(newTextNode);
    paragraph.AppendChild(newRun);
}