DataTplRunBlockBehavior.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 example shows how to use the 'rbb' ('run-block-behavior') formatter
    // to print the data items of a list within a single paragraph rather than
    // as a list of paragraphs.
    public class DataTplRunBlockBehavior
    {
        public GcWordDocument CreateDocx()
        {
            using var oceans = File.OpenRead(Path.Combine("Resources", "data", "oceans.json"));
            var doc = new GcWordDocument();
            doc.DataTemplate.DataSources.Add("ds", oceans);

            // Add a list for oceans, and a run for seas:
            var p = doc.Body.Paragraphs.Add("{{#ds}}{{ds.name}} ocean: ", doc.Styles[BuiltInStyleId.ListParagraph]);
            var myListTemplate = doc.ListTemplates.Add(BuiltInListTemplateId.BulletDefault, "myListTemplate");
            p.ListFormat.Template = myListTemplate;

            // The 'rbb' ('run-block-behavior') formatter forces the range to be expanded
            // within the current paragraph, each data item generating a run rather than a paragraph:
            doc.Body.Paragraphs.First.GetRange().Runs.Add("{{#ds.seas}:rbb()}{{ds.seas.name}}; {{/ds.seas}}");

            // Close the parent range '#ds':
            doc.Body.Paragraphs.First.GetRange().Runs.Add("{{/ds}}");

            // Process the template:
            doc.DataTemplate.Process(CultureInfo.GetCultureInfo("en-US"));

            return doc;
        }
    }
}