DataTplToDouble.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 example shows how to use the todouble() template formatter
  16. // to enable using numeric format specifiers (percent in this sample)
  17. // on numeric values stored as strings in the data source.
  18. public class DataTplToDouble
  19. {
  20. public GcWordDocument CreateDocx()
  21. {
  22. // The data source (ocean and sea data from Wikipedia),
  23. // note that in order to demonstrate the todouble() formatter,
  24. // the 'areaOfWorldOcean' in this data source is a string rather
  25. // than a number:
  26. var oceans = new[]
  27. {
  28. new { name = "Pacific", areaOfWorldOcean = "0.466", volumeOfWorldOcean = 0.501, seas = new[]
  29. { new {name = "Australasian Mediterranean Sea" }, new { name = "Philippine Sea" }, new { name = "Coral Sea" }, new { name = "South China Sea"} }
  30. },
  31. new { name = "Atlantic", areaOfWorldOcean = "0.235", volumeOfWorldOcean = 0.233, seas = new[]
  32. { new {name = "Sargasso Sea" }, new { name = "Caribbean Sea" }, new { name = "Mediterranean Sea" }, new { name = "Gulf of Guinea" } }
  33. },
  34. new { name = "Indian", areaOfWorldOcean = "0.195", volumeOfWorldOcean = 0.198, seas = new[]
  35. { new {name = "Arabian Sea" }, new { name = "Bay of Bengal" }, new { name = "Andaman Sea" }, new { name = "Laccadive Sea" } }
  36. },
  37. new { name = "Southern", areaOfWorldOcean = "0.061", volumeOfWorldOcean = 0.054, seas = new[]
  38. { new {name = "Weddell Sea" }, new { name = "Somov Sea" }, new { name = "Riiser-Larsen Sea" }, new { name = "Lazarev Sea" } }
  39. },
  40. new { name = "Arctic", areaOfWorldOcean = "0.043", volumeOfWorldOcean = 0.014, seas = new[]
  41. { new {name = "Barents Sea" }, new { name = "Greenland Sea" }, new { name = "East Siberian Sea" }, new { name = "Kara Sea" } }
  42. },
  43. };
  44.  
  45. var doc = new GcWordDocument();
  46.  
  47. // Add the data source to the data template data sources:
  48. doc.DataTemplate.DataSources.Add("ds", oceans);
  49.  
  50. // Add list templates for the two-level list:
  51. var listTplTop = doc.ListTemplates.Add(BuiltInListTemplateId.NumberArabicParenthesis, "listTplTop");
  52. var listTplNested = doc.ListTemplates.Add(BuiltInListTemplateId.BulletDefault, "listTplNested");
  53.  
  54. // 'areaOfWorldOcean' in the data source is a string, so in order to be able
  55. // to use percent formatting on it we convert it to a number using the
  56. // todouble() formatter, then pass the result to format():
  57. var p1 = doc.Body.Paragraphs.Add("{{#ds}}{{name}} - {{areaOfWorldOcean}:todouble():format(0.#%)} of World Ocean area:");
  58. p1.ListFormat.Template = listTplTop;
  59. // Print the ocean's largest sea names as the nested list:
  60. var p2 = doc.Body.Paragraphs.Add("{{seas.name}}{{/ds}}");
  61. p2.ListFormat.Template = listTplNested;
  62. p2.ListFormat.LevelNumber = 1;
  63.  
  64. // Note that we explicitly specify the culture to make sure that
  65. // the dot in 'areaOfWorldOcean' strings is recognized as decimal separator:
  66. doc.DataTemplate.Process(CultureInfo.GetCultureInfo("en-US"));
  67.  
  68. // Done:
  69. return doc;
  70. }
  71. }
  72. }
  73.