DataTplSiblingAccess.cs
  1. //
  2. // This code is part of Document Solutions for Word demos.
  3. // Copyright (c) MESCIUS inc. All rights reserved.
  4. //
  5. using System;
  6. using System.IO;
  7. using System.Drawing;
  8. using System.Collections.Generic;
  9. using System.Linq;
  10. using System.Globalization;
  11. using GrapeCity.Documents.Word;
  12.  
  13. namespace DsWordWeb.Demos
  14. {
  15. // This sample demonstrates how to access sibling data members
  16. // in two ranges with a common parent.
  17. public class DataTplSiblingAccess
  18. {
  19. public GcWordDocument CreateDocx()
  20. {
  21. // Simple JSON data source with two ranges:
  22. var test = "{" +
  23. @"""letters"": [ { ""id"": ""a"" }, { ""id"": ""b"" }, { ""id"": ""c"" }, { ""id"": ""d"" } ]," +
  24. @"""numbers"": [ { ""id"": 1 }, { ""id"": 2 }, { ""id"": 3 }, { ""id"": 4 }, { ""id"": 5 }, { ""id"": 6 }, { ""id"": 7 } ]" +
  25. "}";
  26.  
  27. var doc = new GcWordDocument();
  28.  
  29. // Add the data source to the data template data sources:
  30. doc.DataTemplate.DataSources.Add("ds", test);
  31.  
  32. // Add a template that will iterate over both ranges and print all possible combinations of values
  33. // (this did not work in releases prior to v4.2):
  34. var p = doc.Body.Paragraphs.Add("{{#ds}}{{ds.letters.id}:toupper()}{{#ds.numbers}}{{ds.numbers.id}}{{/ds.numbers}}{{/ds}}");
  35.  
  36. // Format this as a bullet list:
  37. p.ListFormat.Template = doc.ListTemplates.Add(BuiltInListTemplateId.BulletDefault, "myListTemplate");
  38.  
  39. // This call expands all data templates in the document,
  40. // replacing template tags with data (iterating over all data items):
  41. doc.DataTemplate.Process(CultureInfo.GetCultureInfo("en-US"));
  42.  
  43. // Done:
  44. return doc;
  45. }
  46. }
  47. }
  48.