Utility to generate Word documents from templates using Visual Studio 2010 and Open Xml 2.0 SDK – Part 4

[Update: The source code has been updated to Visual Studio 2017 and DocumentFormat.OpenXml 2.8.1You can download the code from GitHub.]

This is the fourth part of this series. The code is available for download at http://worddocgenerator.codeplex.com/.

In Part 1 I discussed about

  • Document generation using Content Controls and Open XML 2.0 SDK
  • Creating Word templates
  • Implementation and Samples

In Part 2 I discussed about

  • List of functionalities that can be achieved using the utility/source code
  • Description regarding Samples provided with utility
  • New samples added in this update

In Part 3 I discussed about

  • Document-level customizations for Word 2007 and Word 2010
  • One of the ways to “Refresh the document from within the Word(e.g. right click on document and click Refresh) using document-level customizations for Word 2007 and Word 2010“

This post will focus on Charts. I’ve added the samples to generate/refresh charts using OpenXML. The screenshot below displays the template having Scatter chart and Line chart and the document generated out of this template using this utility.

Word 2010 Template having Charts –> Generated documents with Charts refreshed:

image

image

Code Changes:

Class diagram for Chart specific classes is displayed below

image

“SampleDocumentWithTableAndChartsGenerator” is the sample that shows how to refresh template having Scatter and Line charts. The code snippets are displayed below

  • “RefreshCharts” method is added to DocumentGenerator class
/// <summary>

/// Refreshes the charts.

/// </summary>

/// <param name="mainDocumentPart">The main document part.</param>

protected virtual void RefreshCharts(MainDocumentPart mainDocumentPart)

{


}

  • “SampleDocumentWithTableAndChartsGenerator” class overrides “RefreshCharts” method
/// <summary>

/// Refreshes the charts.

/// </summary>

/// <param name="mainDocumentPart">The main document part.</param>

protected override void RefreshCharts(MainDocumentPart mainDocumentPart)

{

    if (mainDocumentPart != null)

    {

        foreach (ChartPart chartPart in mainDocumentPart.ChartParts)

        {

            Chart chart = chartPart.ChartSpace.Elements<Chart>().FirstOrDefault();


            if (chart != null)

            {

                DocumentFormat.OpenXml.Drawing.Charts.ScatterChart scatterChart = chart.Descendants<DocumentFormat.OpenXml.Drawing.Charts.ScatterChart>().FirstOrDefault();

                DocumentFormat.OpenXml.Drawing.Charts.Line3DChart lineChart = chart.Descendants<DocumentFormat.OpenXml.Drawing.Charts.Line3DChart>().FirstOrDefault();


                if (scatterChart != null)

                {

                    ScatterChartEx chartEx = new ScatterChartEx(chartPart, this.scatterChartData);

                    chartEx.Refresh();

                }


                if (lineChart != null)

                {

                    Line3DChartEx chartEx = new Line3DChartEx(chartPart, this.lineChartData);

                    chartEx.Refresh();

                }

            }


            chartPart.ChartSpace.Save();

        }

    }

}

  • Refresh method is defined in ChartEx<T>
/// <summary>

/// Refreshes this instance.

/// </summary>

public void Refresh()

{

    ChartData chartData = this.GetChartData();


    if (chartData != null && chartData.IsValid())

    {

        string sheetName = this.UpdateEmbeddedObject();


        Chart chart = chartPart.ChartSpace.Elements<Chart>().FirstOrDefault();


        if (chart != null)

        {

            this.UpdateChart(chart.Descendants<T>().FirstOrDefault(), sheetName);

        }

    }

}

For complete code download the source code.

Summary:

Please provide feedback/comments and I’ll try to incorporate most of them in new releases.

3 Replies to “Utility to generate Word documents from templates using Visual Studio 2010 and Open Xml 2.0 SDK – Part 4”

  1. +5

    Atul it would be interesting to have an example covering the following two scenarios,

    1. How to add hyperlinks with in the document.

    2. How to add images with in the document.

    Great work!

  2. I will answer the second scenario first

    I have added images using msdn code "How to: Insert a Picture into a Word Processing Document" at msdn.microsoft.com/…/bb497430.aspx

    Regarding first scenario – I once copied hyperlinks from one doc to another using code displayed below where

    paragraphs – copy hyperlink for these paragraphs

    oldMainDocPart – mainDocumentPart of document from which hyperlinks are to be copied

    newMainDocPart – mainDocumentPart of documnet to which hyperlinks are to be copied

    foreach (Paragraph para in paragraphs)

               {

                   foreach (Hyperlink hyperlink in para.Descendants<Hyperlink>())

                       if (hyperlink.Id != null)

                       {

                           string relId = hyperlink.Id.Value;

                           HyperlinkRelationship relLink = oldMainDocPart.HyperlinkRelationships.Where(rel => (rel.Id == relId)).FirstOrDefault();

                           if (relLink != null)

                           {

                               HyperlinkRelationship newRelLink = newMainDocPart.AddHyperlinkRelationship(relLink.Uri, relLink.IsExternal);

                               hyperlink.Id.Value = newRelLink.Id;

                           }

                       }

               }

    I'm extremely occupied with Kinect for Windows, CRM online & SharePoint online thus lot of action items piled up for next release of WordDocGenerator

Leave a Reply

Your email address will not be published. Required fields are marked *