DataTplCalcText.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 demonstrates the available text functions " +
  16. // that can be used with the 'calc' report templates feature " +
  17. // to perform various operations on strings." +
  18. // For example, to insert the concatenation of three text data fields, " +
  19. // the template '{{calc Concat(ds.a, ds.b, ds.c)}}' can be used. " +
  20. // The data source used in this demo contains a single record " +
  21. // with random string values. " +
  22. public class DataTplCalcText
  23. {
  24. public GcWordDocument CreateDocx()
  25. {
  26. const int MinLen = 5;
  27. var rnd = Util.NewRandom();
  28. int next()
  29. {
  30. return rnd.Next(1, 5);
  31. }
  32.  
  33. string nextWord()
  34. {
  35. for (int i = 0; i < 50; ++i)
  36. {
  37. var t = Util.LoremIpsumWord();
  38. if (t.Length >= MinLen)
  39. return t;
  40. }
  41. return "lorem";
  42. }
  43.  
  44. // Generate a simple data source with pairs of random numeric data:
  45. var data = new[]
  46. {
  47. new { a = nextWord(), b = nextWord(), c = nextWord(), n = next(), },
  48. };
  49.  
  50. var doc = new GcWordDocument();
  51. doc.Body.Sections.First.PageSetup.Margin.Top = 36;
  52. doc.Body.Sections.First.PageSetup.Margin.Bottom = 36;
  53. doc.Body.Sections.First.PageSetup.Margin.Left = 36;
  54. doc.Body.Sections.First.PageSetup.Margin.Right = 36;
  55. var paras = doc.Body.Paragraphs;
  56.  
  57. // Add the data source:
  58. doc.DataTemplate.DataSources.Add("ds", data);
  59.  
  60. // Styles and templates to show results:
  61. var bulletListTemplate = doc.ListTemplates.Add(BuiltInListTemplateId.BulletDefault, "bulletListTemplate");
  62. var exStyle = doc.Styles[BuiltInStyleId.Heading3];
  63. exStyle.ParagraphFormat.TabStops.Add(110);
  64. var resStyle = doc.Styles[BuiltInStyleId.Strong];
  65.  
  66. add("Data", "{{ds.a}}");
  67. add("Data", "{{ds.b}}");
  68. add("Data", "{{ds.c}}");
  69. add("Data", "{{ds.n}}");
  70.  
  71. add("Asc", "{{calc Asc(ds.a)}}");
  72. add("Concat", "{{calc Concat(ds.a, ds.b, ds.c)}}");
  73. add("Contains", "{{calc Contains(ds.a, \"s\")}}");
  74. add("EndsWith", "{{calc EndsWith(ds.a, \"s\")}}");
  75. add("Insert", "{{calc Insert(ds.a, ds.n, ds.b)}}");
  76. add("InStr", "{{calc InStr(ds.a, \"s\")}}");
  77. add("InStr", "{{calc InStr(ds.a, \"s\", ds.n)}}");
  78. add("LCase", "{{calc LCase(\"Lorem Ipsum!\")}}");
  79. add("Len", "{{calc Len(ds.a)}}");
  80. add("LSet", "{{calc LSet(ds.a, 12)}}");
  81. add("LSet", "{{calc LSet(ds.a, 12, \"!\")}}");
  82. add("Mid", "{{calc Mid(ds.a, 2)}}");
  83. add("Mid", "{{calc Mid(ds.a, 2, 1)}}");
  84. add("Remove", "{{calc Remove(ds.a, 2)}}");
  85. add("Remove", "{{calc Remove(ds.a, 2, 1)}}");
  86. add("Replace", "{{calc Replace(\"loremipsumlobortis\", \"lorem\",\"ipsum\")}}");
  87. add("RSet", "{{calc RSet(ds.a, 12)}}");
  88. add("RSet", "{{calc RSet(ds.a, 12, \"!\")}}");
  89. add("StartsWith", "{{calc StartsWith(ds.a, \"s\")}}");
  90. add("StrReverse", "{{calc StrReverse(ds.a)}}");
  91. add("Trim", "{{calc Trim(\" Lorem Ipsum \")}}");
  92. add("UCase", "{{calc UCase(\"Lorem Ipsum!\")}}");
  93.  
  94. // Process the templates:
  95. doc.DataTemplate.Process(CultureInfo.GetCultureInfo("en-US"));
  96.  
  97. // Add a short note describing the demo at the top of the document:
  98. paras.Insert(
  99. "This example demonstrates the available text functions " +
  100. "that can be used with the 'calc' report templates feature " +
  101. "to perform various operations on strings." +
  102. "For example, to insert the concatenation of three text data fields, " +
  103. "the template '{{calc Concat(ds.a, ds.b, ds.c)}}' can be used. " +
  104. "The data source used in this demo contains a single record " +
  105. "with random string values. " +
  106. "Please see this sample source code for full details.",
  107. InsertLocation.Start);
  108. paras.Insert("Report templates: available calc operators", doc.Styles[BuiltInStyleId.Heading1], InsertLocation.Start);
  109.  
  110. // Done:
  111. return doc;
  112.  
  113. void add(string caption, string expr)
  114. {
  115. // \x200B is a zero-width space used to prevent template expansion:
  116. paras.Add(caption + "\t" + expr.Insert(1, "​​​\x200B") + " : ", exStyle).ListFormat.Template = bulletListTemplate;
  117. paras.Last.GetRange().Runs.Add(expr, resStyle);
  118. }
  119. }
  120. }
  121. }
  122.