DataTplMultipleDataSrc.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 the use of multiple data sources with data templates.
    // It uses two simple JSON data sources, one contains a list of some popular car makes,
    // the other a list of various car body styles. The DOCX includes two templates that are
    // used to print first the list of body styles, followed by the list of car brands.
    // The two lists are not related to each other, demonstrating how unrelated data sources
    // can be used to include various data in a single data bound document.
    public class DataTplMultipleDataSrc
    {
        public GcWordDocument CreateDocx()
        {
            var makes = "[" +
                "{ \"make\": \"Toyota\" }," +
                "{ \"make\": \"General Motors\" }," +
                "{ \"make\": \"Volkswagen\" }," +
                "{ \"make\": \"Ford\" }," +
                "{ \"make\": \"BMW\" }," +
                "{ \"make\": \"Nissan\" }," +
                "{ \"make\": \"Hyundai\" }," +
                "{ \"make\": \"Honda\" }," +
                "{ \"make\": \"Mazda\" }," +
                "{ \"make\": \"Jaguar\" }," +
                "]";
            var bodyStyles = "[" +
                "{ \"style\": \"Sedan\" }," +
                "{ \"style\": \"Coupe\" }," +
                "{ \"style\": \"Hatchback\" }," +
                "{ \"style\": \"SUV\" }," +
                "{ \"style\": \"Crossover\" }," +
                "{ \"style\": \"Minivan\" }," +
                "{ \"style\": \"Pickup\" }," +
                "{ \"style\": \"Wagon\" }," +
                "]";

            var doc = new GcWordDocument();

            // Add the data sources:
            doc.DataTemplate.DataSources.Add("makes", makes);
            doc.DataTemplate.DataSources.Add("styles", bodyStyles);

            // Print the list of some car makes:
            doc.Body.Paragraphs.Add("Popular Car Makers", doc.Styles[BuiltInStyleId.Heading2]);
            doc.Body.Paragraphs.Add("{{#makes}}{{makes.make}}{{/makes}}", doc.Styles[BuiltInStyleId.ListBullet]);

            // Print the list of car body styles:
            doc.Body.Paragraphs.Add("Car Body Styles", doc.Styles[BuiltInStyleId.Heading2]);
            doc.Body.Paragraphs.Add("{{#styles}}{{styles.style}}{{/styles}}", doc.Styles[BuiltInStyleId.ListBullet]);

            doc.DataTemplate.Process(CultureInfo.GetCultureInfo("en-US"));

            // Done:
            return doc;
        }
    }
}