DataTplCalcOps.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 binary and unary operators
  16. // that can be used with the 'calc' report templates feature
  17. // to perform various operations on data.
  18. // For example, to insert the remainder of the division of two data fields,
  19. // the template '{{calc ds.a Mod ds.b)}}' can be used.
  20. // The data source used in this demo contains a single record
  21. // with random decimal and string values.
  22. public class DataTplCalcOps
  23. {
  24. public GcWordDocument CreateDocx()
  25. {
  26. var rnd = Util.NewRandom();
  27. decimal next()
  28. {
  29. return new decimal(Math.Round((double)rnd.Next(1, 1000000) / rnd.Next(1, 100), 2));
  30. }
  31.  
  32. // Generate a sample data source with a single record of random data:
  33. var data = new[]
  34. {
  35. new { a = next(), b = next(), ta = Util.LoremIpsumWord(), tb = Util.LoremIpsumWord() },
  36. };
  37.  
  38. var doc = new GcWordDocument();
  39.  
  40. // Add the data source:
  41. doc.DataTemplate.DataSources.Add("ds", data);
  42. var paras = doc.Body.Paragraphs;
  43.  
  44. // Styles and templates to show results:
  45. var bulletListTemplate = doc.ListTemplates.Add(BuiltInListTemplateId.BulletDefault, "bulletListTemplate");
  46. var exStyle = doc.Styles[BuiltInStyleId.Heading3];
  47. exStyle.ParagraphFormat.TabStops.Add(130);
  48. exStyle.ParagraphFormat.TabStops.Add(260);
  49. var resStyle = doc.Styles[BuiltInStyleId.Strong];
  50.  
  51. add("Data", "{{ds.a}}");
  52. add("Data", "{{ds.b}}");
  53. add("Data", "{{ds.ta}}");
  54. add("Data", "{{ds.tb}}");
  55.  
  56. add("Add", "{{calc ds.a + ds.b}}");
  57. add("Subtract", "{{calc ds.a - ds.b}}");
  58. add("Multiply", "{{calc ds.a * ds.b}}");
  59. add("Divide", "{{calc ds.a / ds.b}}");
  60. add("Modulus", "{{calc ds.a Mod ds.b}}");
  61. add("Concatenate", "{{calc ds.ta & ds.tb}}");
  62. add("Equal", "{{calc ds.a = ds.b}}");
  63. add("Not equal", "{{calc ds.a <> ds.b}}");
  64. add("Greater than", "{{calc ds.a > ds.b}}");
  65. add("Greater or equal", "{{calc ds.a >= ds.b}}");
  66. add("Less than", "{{calc ds.a < ds.b}}");
  67. add("Less or equal", "{{calc ds.a <= ds.b}}");
  68. add("Logical And", "{{calc ds.a And ds.b}}");
  69. add("Logical Or", "{{calc ds.a Or ds.b}}");
  70. add("Logical Not", "{{calc Not ds.a}}");
  71.  
  72. // Process the templates:
  73. doc.DataTemplate.Process(CultureInfo.GetCultureInfo("en-US"));
  74.  
  75. // Add a short note describing the demo at the top of the document:
  76. paras.Insert(
  77. "This example demonstrates the available binary and unary operators " +
  78. "that can be used with the 'calc' report templates feature " +
  79. "to perform various operations on data." +
  80. "For example, to insert the remainder of the division of two data fields, " +
  81. "the template '{{calc ds.a Mod ds.b)}}' can be used. " +
  82. "The data source used in this demo contains a single record " +
  83. "with random decimal and string values. " +
  84. "Please see this example's source code for full details.",
  85. InsertLocation.Start);
  86. paras.Insert("Report templates: calc operators", doc.Styles[BuiltInStyleId.Heading1], InsertLocation.Start);
  87.  
  88. // Done:
  89. return doc;
  90.  
  91. void add(string caption, string expr)
  92. {
  93. // \x200B is a zero-width space used to prevent template expansion:
  94. paras.Add(caption + "\t" + expr.Insert(1, "​​​\x200B") + ":\t", exStyle).ListFormat.Template = bulletListTemplate;
  95. paras.Last.GetRange().Runs.Add(expr, resStyle);
  96. }
  97. }
  98. }
  99. }
  100.