DataTplRestartList.cs
C# VB
//// This code is part of Document Solutions for Word demos.// Copyright (c) MESCIUS inc. All rights reserved.//using System.IO;using System.Globalization;using GrapeCity.Documents.Word;
namespace DsWordWeb.Demos{ // This sample demonstrates the use of the restart list formatter // to print the list of oceans and each ocean's list of seas, // restarting the inner seas list for each ocean. // This sample is similar to DataTplNestedList but uses a JSON // data source, and restarts the nested list for each ocean. public class DataTplRestartList { public GcWordDocument CreateDocx() { var doc = new GcWordDocument();
// Add JSON data source to the data template data sources: using (var oceans = File.OpenRead(Path.Combine("Resources", "data", "oceans.json"))) doc.DataTemplate.DataSources.Add("ds", oceans);
// Our document consists of a top list with ocean names, // and a nested list with each ocean's sea names. // The ':restart()' formatter associated with a range restarts // the nested lists on each iteration over the range, // but does not restart the outer list - which is what we // want in this and most other cases (to restart the topmost list too, // use the optional 'all' parameter ':restart(all)'): var pTop = doc.Body.Paragraphs.Add("{{#ds}:restart()}{{ds.name}} Ocean:", doc.Styles[BuiltInStyleId.ListNumber]); var pNested = doc.Body.Paragraphs.Add("{{#ds.seas}}{{ds.seas.name}}{{/ds.seas}}{{/ds}}", doc.Styles[BuiltInStyleId.ListNumber2]);
// Add list templates and set up list formatting: var topListTemplate = doc.ListTemplates.Add(BuiltInListTemplateId.NumberDefault, "topListTemplate"); var nestedListTemplate = doc.ListTemplates.Add(BuiltInListTemplateId.NumberDefault, "nestedListTemplate"); pTop.ListFormat.Template = topListTemplate; pNested.ListFormat.Template = nestedListTemplate; pNested.ListFormat.LevelNumber = 1;
// 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; } }}