DataTplElemColl.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 shows how arrays of primitive value types (such as int[] or List<string>)
  16. // can be used as data sources for templates, injecting the array elements into
  17. // the document with the "value" template tag.
  18. // Types that allow using the "value" tag are those for which Type.IsPrimitive gets true
  19. // (except pointer types), and strings.
  20. public class DataTplElemColl
  21. {
  22. public GcWordDocument CreateDocx()
  23. {
  24. var doc = new GcWordDocument();
  25.  
  26. // Add a few simple arrays as data sources:
  27. doc.DataTemplate.DataSources.Add("dsInts", new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 });
  28. doc.DataTemplate.DataSources.Add("dsConsts", new double[]
  29. {
  30. Math.PI,
  31. Math.E,
  32. });
  33. doc.DataTemplate.DataSources.Add("dsStrs", new List<string>()
  34. {
  35. "Pacific",
  36. "Atlantic",
  37. "Indian",
  38. "Southern",
  39. "Arctic",
  40. });
  41.  
  42. // Add a list template so that the data is formatted as a list:
  43. var myListTemplate = doc.ListTemplates.Add(BuiltInListTemplateId.BulletDefault, "myListTemplate");
  44.  
  45. // Integers:
  46. doc.Body.Paragraphs.Add("Integers from 1 to 9:", doc.Styles[BuiltInStyleId.Heading1]);
  47. var p = doc.Body.Paragraphs.Add("{{#dsInts}}{{dsInts.value}}{{/dsInts}}", doc.Styles[BuiltInStyleId.ListParagraph]);
  48. p.ListFormat.Template = myListTemplate;
  49.  
  50. // Math constants:
  51. doc.Body.Paragraphs.Add("Constants from the Math class:", doc.Styles[BuiltInStyleId.Heading1]);
  52. p = doc.Body.Paragraphs.Add("{{#dsConsts}}{{dsConsts.value}}{{/dsConsts}}", doc.Styles[BuiltInStyleId.ListParagraph]);
  53. p.ListFormat.Template = myListTemplate;
  54.  
  55. // Strings (oceans):
  56. doc.Body.Paragraphs.Add("Strings (oceans):", doc.Styles[BuiltInStyleId.Heading1]);
  57. p = doc.Body.Paragraphs.Add("{{#dsStrs}}{{dsStrs.value}:toupper()}{{/dsStrs}}", doc.Styles[BuiltInStyleId.ListParagraph]);
  58. p.ListFormat.Template = myListTemplate;
  59.  
  60. // Expand all data templates in the document:
  61. doc.DataTemplate.Process(CultureInfo.GetCultureInfo("en-US"));
  62.  
  63. // Done:
  64. return doc;
  65. }
  66. }
  67. }
  68.