DataTplElemColl.cs
//
// This code is part of Document Solutions for Word demos.
// Copyright (c) MESCIUS inc. All rights reserved.
//
using System;
using System.IO;
using System.Drawing;
using System.Collections.Generic;
using System.Linq;
using System.Globalization;
using GrapeCity.Documents.Word;

namespace DsWordWeb.Demos
{
    // This sample shows how arrays of primitive value types (such as int[] or List<string>)
    // can be used as data sources for templates, injecting the array elements into
    // the document with the "value" template tag.
    // Types that allow using the "value" tag are those for which Type.IsPrimitive gets true
    // (except pointer types), and strings.
    public class DataTplElemColl
    {
        public GcWordDocument CreateDocx()
        {
            var doc = new GcWordDocument();

            // Add a few simple arrays as data sources:
            doc.DataTemplate.DataSources.Add("dsInts", new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 });
            doc.DataTemplate.DataSources.Add("dsConsts", new double[]
            {
                Math.PI,
                Math.E,
            });
            doc.DataTemplate.DataSources.Add("dsStrs", new List<string>()
            {
                "Pacific",
                "Atlantic",
                "Indian",
                "Southern",
                "Arctic",
            });

            // Add a list template so that the data is formatted as a list:
            var myListTemplate = doc.ListTemplates.Add(BuiltInListTemplateId.BulletDefault, "myListTemplate");

            // Integers:
            doc.Body.Paragraphs.Add("Integers from 1 to 9:", doc.Styles[BuiltInStyleId.Heading1]);
            var p = doc.Body.Paragraphs.Add("{{#dsInts}}{{dsInts.value}}{{/dsInts}}", doc.Styles[BuiltInStyleId.ListParagraph]);
            p.ListFormat.Template = myListTemplate;

            // Math constants:
            doc.Body.Paragraphs.Add("Constants from the Math class:", doc.Styles[BuiltInStyleId.Heading1]);
            p = doc.Body.Paragraphs.Add("{{#dsConsts}}{{dsConsts.value}}{{/dsConsts}}", doc.Styles[BuiltInStyleId.ListParagraph]);
            p.ListFormat.Template = myListTemplate;

            // Strings (oceans):
            doc.Body.Paragraphs.Add("Strings (oceans):", doc.Styles[BuiltInStyleId.Heading1]);
            p = doc.Body.Paragraphs.Add("{{#dsStrs}}{{dsStrs.value}:toupper()}{{/dsStrs}}", doc.Styles[BuiltInStyleId.ListParagraph]);
            p.ListFormat.Template = myListTemplate;

            // Expand all data templates in the document:
            doc.DataTemplate.Process(CultureInfo.GetCultureInfo("en-US"));

            // Done:
            return doc;
        }
    }
}