DataTplArithmeticOps.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 arithmetic operators
  16. // that can be used with the 'calc' report templates feature
  17. // to perform arithmetic operations on data.
  18. // For example, to insert the sum of two data fields,
  19. // the template '{{calc ds.a + ds.b)}}' can be used.
  20. // The data source used in this demo is a list of pairs of random decimal values.
  21. public class DataTplArithmeticOps
  22. {
  23. public GcWordDocument CreateDocx()
  24. {
  25. var rnd = Util.NewRandom();
  26. decimal next()
  27. {
  28. return new decimal(Math.Round((double)rnd.Next(1, 1000000) / rnd.Next(1, 100), 2));
  29. }
  30.  
  31. // A simple data source with pairs of random numeric data:
  32. var data = new[]
  33. {
  34. new { a = next(), b = next() },
  35. new { a = next(), b = next() },
  36. new { a = next(), b = next() },
  37. new { a = next(), b = next() },
  38. new { a = next(), b = next() },
  39. new { a = next(), b = next() },
  40. };
  41.  
  42. var doc = new GcWordDocument();
  43.  
  44. // Add the data source:
  45. doc.DataTemplate.DataSources.Add("ds", data);
  46. var paras = doc.Body.Paragraphs;
  47.  
  48. // Styles and templates to show results:
  49. var myListTemplate = doc.ListTemplates.Add(BuiltInListTemplateId.NumberDefault, "myListTemplate");
  50. var resStyle = doc.Styles[BuiltInStyleId.Strong];
  51.  
  52. // Print data and result for each record (\x200B is a zero-width space to prevent template expansion):
  53. paras.Add("Pairs of data values and results of arithmetic operations on them:", doc.Styles[BuiltInStyleId.Heading2]);
  54.  
  55. var p = paras.Add("{{#ds}}a = ", doc.Styles[BuiltInStyleId.ListParagraph]);
  56. p.GetRange().Runs.Add("{{ds.a}}", resStyle);
  57. p.GetRange().Runs.Add(", b = ");
  58. p.GetRange().Runs.Add("{{ds.b}}\n", resStyle);
  59. p.GetRange().Runs.Add("{\x200B{ calc ds.a + ds.b }} : ");
  60. p.GetRange().Runs.Add("{{ calc ds.a + ds.b }}\n", resStyle);
  61.  
  62. p.GetRange().Runs.Add("{\x200B{ calc ds.a - ds.b }} : ");
  63. p.GetRange().Runs.Add("{{ calc ds.a - ds.b }}\n", resStyle);
  64. p.GetRange().Runs.Add("{\x200B{ calc ds.a * ds.b }} : ");
  65. p.GetRange().Runs.Add("{{ calc ds.a * ds.b }}\n", resStyle);
  66. p.GetRange().Runs.Add("{\x200B{ calc ds.a / ds.b }} : ");
  67. p.GetRange().Runs.Add("{{ calc ds.a / ds.b }}\n", resStyle);
  68. p.GetRange().Runs.Add("{{/ds}}");
  69. p.ListFormat.Template = myListTemplate;
  70.  
  71. // Process the templates:
  72. doc.DataTemplate.Process(CultureInfo.GetCultureInfo("en-US"));
  73.  
  74. // Add a short note describing the demo at the top of the document:
  75. paras.Insert(
  76. "This example demonstrates the available arithmetic operators " +
  77. "that can be used with the 'calc' report templates feature " +
  78. "to perform arithmetic operations on data." +
  79. "For example, to insert the sum of two data fields, " +
  80. "the template '{{calc ds.a + ds.b)}}' can be used. " +
  81. "The data source used in this demo is a list of pairs of random decimal values. " +
  82. "Please see this sample source code for full details.",
  83. InsertLocation.Start);
  84. paras.Insert("Report templates: calc arithmetic operators", doc.Styles[BuiltInStyleId.Heading1], InsertLocation.Start);
  85.  
  86. // Done:
  87. return doc;
  88. }
  89. }
  90. }
  91.