DataTplCalcMath.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 math functions
  16. // that can be used with the 'calc' report templates feature.
  17. public class DataTplCalcMath
  18. {
  19. public GcWordDocument CreateDocx()
  20. {
  21. // A simple data source to be used by the math functions:
  22. var data = new double[] { Math.PI };
  23.  
  24. var doc = new GcWordDocument();
  25.  
  26. // Add the data source:
  27. doc.DataTemplate.DataSources.Add("ds", data);
  28.  
  29. // Styles and templates to show results:
  30. var bulletListTemplate = doc.ListTemplates.Add(BuiltInListTemplateId.BulletDefault, "bulletListTemplate");
  31. var exStyle = doc.Styles[BuiltInStyleId.Heading3];
  32. var resStyle = doc.Styles[BuiltInStyleId.Strong];
  33.  
  34. var paras = doc.Body.Paragraphs;
  35. add("{{ ds.value }}");
  36. add("{{ calc Abs(-ds.value) }}");
  37. add("{{ calc Round(ds.value, 2) }}");
  38. add("{{ calc Acos(ds.value) }}");
  39. add("{{ calc Asin(ds.value) }}");
  40. add("{{ calc Atan(ds.value) }}");
  41. add("{{ calc Atan2(ds.value, 1) }}");
  42. add("{{ calc Ceiling(ds.value) }}");
  43. add("{{ calc Cos(ds.value) }}");
  44. add("{{ calc Cosh(ds.value) }}");
  45. add("{{ calc Exp(ds.value) }}");
  46. add("{{ calc Floor(ds.value) }}");
  47. add("{{ calc Log10(ds.value) }}");
  48. add("{{ calc Log(ds.value) }}");
  49. add("{{ calc Log(ds.value, 10) }}");
  50. add("{{ calc Pow(ds.value, 2) }}");
  51. add("{{ calc Rand() }}");
  52. add("{{ calc RandBetween(0, 100) }}");
  53. add("{{ calc Sign(ds.value) }}");
  54. add("{{ calc Sin(ds.value) }}");
  55. add("{{ calc Sinh(ds.value) }}");
  56. add("{{ calc Sqrt(ds.value) }}");
  57. add("{{ calc Tan(ds.value) }}");
  58. add("{{ calc Tanh(ds.value) }}");
  59.  
  60. // Process the templates:
  61. doc.DataTemplate.Process(CultureInfo.GetCultureInfo("en-US"));
  62.  
  63. // Add a short note describing the demo at the top of the document:
  64. paras.Insert(
  65. "This example demonstrates the available math functions " +
  66. "that can be used with the 'calc' report templates feature. " +
  67. "Please see this sample source code for full details.",
  68. InsertLocation.Start);
  69. paras.Insert("Report templates: available calc math functions", doc.Styles[BuiltInStyleId.Heading1], InsertLocation.Start);
  70.  
  71. // Done:
  72. return doc;
  73.  
  74. void add(string expr)
  75. {
  76. // \x200B is a zero-width space used to prevent template expansion:
  77. paras.Add(expr.Insert(1, "​​​\x200B") + " : ", exStyle).ListFormat.Template = bulletListTemplate;
  78. paras.Last.GetRange().Runs.Add(expr, resStyle);
  79. }
  80. }
  81. }
  82. }
  83.