DataTplSimpleListJson.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 is similar to DataTplSimpleList
- // but uses a JSON data source to generate
- // the list of ocean seas.
- public class DataTplSimpleListJson
- {
- public GcWordDocument CreateDocx()
- {
- var doc = new GcWordDocument();
-
- // The JSON data source is loaded from oceans.json:
- using (var oceans = File.OpenRead(Path.Combine("Resources", "data", "oceans.json")))
- doc.DataTemplate.DataSources.Add("ds", oceans);
-
- // Add a list template so that the data is formatted as a list:
- var myListTemplate = doc.ListTemplates.Add(BuiltInListTemplateId.BulletDefault, "myListTemplate");
-
- // The single paragraph added to the document contains template tags:
- // - {{#ds}}..{{/ds}} -- root template, 'ds' is the name of the data source;
- // - {ds.seas.name} -- fetches the sea name;
- // - :toupper() -- uppercases the data:
- var p = doc.Body.Paragraphs.Add("{{#ds}}{{ds.seas.name}:toupper()}{{/ds}}", doc.Styles[BuiltInStyleId.ListParagraph]);
- p.ListFormat.Template = myListTemplate;
-
- // 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;
- }
- }
- }
-