DataTplNestedList.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 the list of sea names
  16. // from a simple hierarchical data source.
  17. // See DataTplRestartList for how to implement a numbered nested list
  18. // that is restarted for each ocean.
  19. public class DataTplNestedList
  20. {
  21. public GcWordDocument CreateDocx()
  22. {
  23. // The data source (ocean and sea data from Wikipedia):
  24. var oceans = new[]
  25. {
  26. new { name = "Pacific", areaOfWorldOcean = 0.466, volumeOfWorldOcean = 0.501, seas = new[]
  27. { new {name = "Australasian Mediterranean Sea" }, new { name = "Philippine Sea" }, new { name = "Coral Sea" }, new { name = "South China Sea"} }
  28. },
  29. new { name = "Atlantic", areaOfWorldOcean = 0.235, volumeOfWorldOcean = 0.233, seas = new[]
  30. { new {name = "Sargasso Sea" }, new { name = "Caribbean Sea" }, new { name = "Mediterranean Sea" }, new { name = "Gulf of Guinea" } }
  31. },
  32. new { name = "Indian", areaOfWorldOcean = 0.195, volumeOfWorldOcean = 0.198, seas = new[]
  33. { new {name = "Arabian Sea" }, new { name = "Bay of Bengal" }, new { name = "Andaman Sea" }, new { name = "Laccadive Sea" } }
  34. },
  35. new { name = "Southern", areaOfWorldOcean = 0.061, volumeOfWorldOcean = 0.054, seas = new[]
  36. { new {name = "Weddell Sea" }, new { name = "Somov Sea" }, new { name = "Riiser-Larsen Sea" }, new { name = "Lazarev Sea" } }
  37. },
  38. new { name = "Arctic", areaOfWorldOcean = 0.043, volumeOfWorldOcean = 0.014, seas = new[]
  39. { new {name = "Barents Sea" }, new { name = "Greenland Sea" }, new { name = "East Siberian Sea" }, new { name = "Kara Sea" } }
  40. },
  41. };
  42.  
  43. var doc = new GcWordDocument();
  44.  
  45. // Add the data source to the data template data sources:
  46. doc.DataTemplate.DataSources.Add("ds", oceans);
  47.  
  48. // Add list templates for the two-level list:
  49. var listTplTop = doc.ListTemplates.Add(BuiltInListTemplateId.NumberArabicParenthesis, "listTplTop");
  50. var listTplNested = doc.ListTemplates.Add(BuiltInListTemplateId.BulletDefault, "listTplNested");
  51.  
  52. // Print 'ocean - area % of world ocean' at the top list level:
  53. var p1 = doc.Body.Paragraphs.Add("{{#ds}}{{name}} - {{areaOfWorldOcean}:format(0.#%)}");
  54. p1.ListFormat.Template = listTplTop;
  55. // Print the ocean's largest sea names as the nested list:
  56. var p2 = doc.Body.Paragraphs.Add("{{seas.name}}{{/ds}}");
  57. p2.ListFormat.Template = listTplNested;
  58. p2.ListFormat.LevelNumber = 1;
  59.  
  60. // This call expands all data templates in the document,
  61. // replacing template tags with data (iterating over all data items):
  62. doc.DataTemplate.Process(CultureInfo.GetCultureInfo("en-US"));
  63.  
  64. // Done:
  65. return doc;
  66. }
  67. }
  68. }
  69.