DataTplSimpleList.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 uses data templates to print a simple list of items
  16. // from a hierarchical data source, uppercasing each item.
  17. public class DataTplSimpleList
  18. {
  19. public GcWordDocument CreateDocx()
  20. {
  21. // The data source (ocean and sea data from Wikipedia):
  22. var oceans = new[]
  23. {
  24. new { name = "Pacific", areaOfWorldOcean = 0.466, volumeOfWorldOcean = 0.501, seas = new[]
  25. { new {name = "Australasian Mediterranean Sea" }, new { name = "Philippine Sea" }, new { name = "Coral Sea" }, new { name = "South China Sea"} }
  26. },
  27. new { name = "Atlantic", areaOfWorldOcean = 0.235, volumeOfWorldOcean = 0.233, seas = new[]
  28. { new {name = "Sargasso Sea" }, new { name = "Caribbean Sea" }, new { name = "Mediterranean Sea" }, new { name = "Gulf of Guinea" } }
  29. },
  30. new { name = "Indian", areaOfWorldOcean = 0.195, volumeOfWorldOcean = 0.198, seas = new[]
  31. { new {name = "Arabian Sea" }, new { name = "Bay of Bengal" }, new { name = "Andaman Sea" }, new { name = "Laccadive Sea" } }
  32. },
  33. new { name = "Southern", areaOfWorldOcean = 0.061, volumeOfWorldOcean = 0.054, seas = new[]
  34. { new {name = "Weddell Sea" }, new { name = "Somov Sea" }, new { name = "Riiser-Larsen Sea" }, new { name = "Lazarev Sea" } }
  35. },
  36. new { name = "Arctic", areaOfWorldOcean = 0.043, volumeOfWorldOcean = 0.014, seas = new[]
  37. { new {name = "Barents Sea" }, new { name = "Greenland Sea" }, new { name = "East Siberian Sea" }, new { name = "Kara Sea" } }
  38. },
  39. };
  40.  
  41. var doc = new GcWordDocument();
  42.  
  43. // Add the data source to the data template data sources:
  44. doc.DataTemplate.DataSources.Add("ds", oceans);
  45.  
  46. // Add a list template so that the data is formatted as a list:
  47. var myListTemplate = doc.ListTemplates.Add(BuiltInListTemplateId.BulletDefault, "myListTemplate");
  48.  
  49. // The single paragraph added to the document contains template tags:
  50. // - {{#ds}}..{{/ds}} -- root template, 'ds' is the name of the data source;
  51. // - {ds.seas.name} -- fetches the sea name;
  52. // - :toupper() -- uppercases the data:
  53. var p = doc.Body.Paragraphs.Add("{{#ds}}{{ds.seas.name}:toupper()}{{/ds}}", doc.Styles[BuiltInStyleId.ListParagraph]);
  54. p.ListFormat.Template = myListTemplate;
  55.  
  56. // This call expands all data templates in the document,
  57. // replacing template tags with data (iterating over all data items):
  58. doc.DataTemplate.Process(CultureInfo.GetCultureInfo("en-US"));
  59.  
  60. // Done:
  61. return doc;
  62. }
  63. }
  64. }
  65.