- //
- // 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 demonstrates how to use 'sequence' ('seq') and 'follow' formatters
- // to synchronize two ranges from two different JSON data sources.
- public class DataTplSequence
- {
- public GcWordDocument CreateDocx()
- {
- // The first JSON data source - the sequence master.
- // In this sample we use it to spell sequence numbers.
- // Note that the resulting number of iterations will be the MINIMUM
- // of the lengths of the sequence and follower, so print all oceans
- // we need to provide at least 5 numbers, but more won't hurt:
- var sequence = @"[
- { ""num"": ""First"" },
- { ""num"": ""Second"" },
- { ""num"": ""Third"" },
- { ""num"": ""Fourth"" },
- { ""num"": ""Fifth"" },
- { ""num"": ""Sixth"" },
- { ""num"": ""Seventh"" },
- { ""num"": ""Eights"" },
- { ""num"": ""Ninth"" },
- { ""num"": ""Tenth"" }
- ]";
- var doc = new GcWordDocument();
- // Add the data sources to the data template data sources:
- doc.DataTemplate.DataSources.Add("nums", sequence);
- // The second 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 only paragraph added to the document will print the sequence number
- // fetched from the first data source (the sequence master), and the ocean name
- // fetched from the second data source (the sequence follower):
- var p = doc.Body.Paragraphs.Add("{{#nums}:seq(seq1)}{{nums.num}} -- {{#ds}:follow(seq1)}{{ds.name}}{{/ds}}{{/nums}}", 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;
- }
- }
- }