//
// 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 '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}}{{ds.name}}", 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}}{{ds.name}}");
// 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.name}}{{/ds.seas}}");
// Close the parent range '#ds':
doc.Body.Paragraphs.Add("{{/ds}}");
// Process the template:
doc.DataTemplate.Process(CultureInfo.GetCultureInfo("en-US"));
return doc;
}
}
}