DataTplSiblingAccess.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 demonstrates how to access sibling data members
    // in two ranges with a common parent.
    public class DataTplSiblingAccess
    {
        public GcWordDocument CreateDocx()
        {
            // Simple JSON data source with two ranges:
            var test = "{" +
                @"""letters"": [ { ""id"": ""a"" }, { ""id"": ""b"" }, { ""id"": ""c"" }, { ""id"": ""d"" } ]," +
                @"""numbers"": [ { ""id"": 1 }, { ""id"": 2 }, { ""id"": 3 }, { ""id"": 4 }, { ""id"": 5 }, { ""id"": 6 }, { ""id"": 7 } ]" +
            "}";

            var doc = new GcWordDocument();

            // Add the data source to the data template data sources:
            doc.DataTemplate.DataSources.Add("ds", test);

            // Add a template that will iterate over both ranges and print all possible combinations of values
            // (this did not work in releases prior to v4.2):
            var p = doc.Body.Paragraphs.Add("{{#ds}}{{ds.letters.id}:toupper()}{{#ds.numbers}}{{ds.numbers.id}}{{/ds.numbers}}{{/ds}}");

            // Format this as a bullet list:
            p.ListFormat.Template = doc.ListTemplates.Add(BuiltInListTemplateId.BulletDefault, "myListTemplate");

            // This call expands all data templates in the document,
            // replacing template tags with data (iterating over all data items):
            doc.DataTemplate.Process(CultureInfo.GetCultureInfo("en-US"));

            // Done:
            return doc;
        }
    }
}