// This code is part of GrapeCity Documents for Word samples.
// Copyright (c) GrapeCity, Inc. All rights reserved.
using System;
using System.IO;
using System.Drawing;
using System.Collections.Generic;
using System.Linq;
using GrapeCity.Documents.Word;

namespace GcWordWeb.Samples
    // This example shows how to use the 'pbb' ('paragraph-block-behavior') formatter
    // to print all data items of a list as separate paragraphs inside a single table cell
    // rather than generating a new row for each item.
    public class DataTplParaBlockBehavior
        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 nested table for seas:
            // doc.Body.Paragraphs.Add("{{#ds}}{{}}", doc.Styles[BuiltInStyleId.ListParagraph]);

            // Add a table with one column and two rows, note that each cell has one default paragraph
            // when the table is created, so we add template tags to those already existing paragraphs:
            var table = doc.Body.Tables.Add(1, 2, doc.Styles[BuiltInStyleId.ColorfulShadingAccent1]);
            // The first row is used to print the ocean's name:
            table[0, 0].GetRange().Paragraphs.First.GetRange().Runs.Add("{{#ds}}{{}}");
            // The second row prints the ocean's seas range. Using the 'pbb' (paragraph-block-b) formatter
            // allows us to print all items in the range (seas) inside the same single cell rather than
            // generating a row per each item:
            table[1, 0].GetRange().Paragraphs.First.GetRange().Runs.Add("{{#ds.seas}:pbb()}{{}}{{/ds.seas}}");

            // Close the parent range '#ds':

            // Process the template:

            return doc;