DataTplCalcConvert.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 conversion functions
  16. // that can be used with the 'calc' report templates feature.
  17. public class DataTplCalcConvert
  18. {
  19. public GcWordDocument CreateDocx()
  20. {
  21. // A simple data source to be used by the logical functions:
  22. var data = new bool[] { true };
  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.ListParagraph];
  32. var resStyle = doc.Styles[BuiltInStyleId.Strong];
  33.  
  34. var paras = doc.Body.Paragraphs;
  35. add("{{ calc CBool(\"True\") }}");
  36. add("{{ calc CBool(\"False\") }}");
  37. add("{{ calc CByte(\"127\") }}");
  38. add("{{ calc CChar(\"\u4249\") }}");
  39. add("{{ calc CDate(\"1-jan-2001\") }}");
  40. add("{{ calc CDbl(\"123.456\") }}");
  41. add("{{ calc CDec(\"123.456\") }}");
  42. add("{{ calc CInt(\"123\") }}");
  43. add("{{ calc CLng(\"123456789\") }}");
  44. add("{{ calc CObj(\"Nothing\") }}");
  45. add("{{ calc CByte(\"123\") }}");
  46. add("{{ calc CShort(\"123\") }}");
  47. add("{{ calc CSng(\"123\") }}");
  48. add("{{ calc CStr(123.456) }}");
  49. add("{{ calc CType(123, \"System.Int64\") }}");
  50. add("{{ calc CUInt(\"123\") }}");
  51. add("{{ calc CULong(\"123\") }}");
  52. add("{{ calc CUShort(\"123\") }}");
  53.  
  54. // Process the templates:
  55. doc.DataTemplate.Process(CultureInfo.GetCultureInfo("en-US"));
  56.  
  57. // Add a short note describing the demo at the top of the document:
  58. paras.Insert(
  59. "This example demonstrates the available conversion functions " +
  60. "that can be used with the 'calc' report templates feature. " +
  61. "Please see this example's source code for full details.",
  62. InsertLocation.Start);
  63. paras.Insert("Report templates: available calc conversion functions", doc.Styles[BuiltInStyleId.Heading1], InsertLocation.Start);
  64.  
  65. // Done:
  66. return doc;
  67.  
  68. void add(string expr)
  69. {
  70. // \x200B is a zero-width space used to prevent template expansion:
  71. paras.Add(expr.Insert(1, "​​​\x200B") + " : ", exStyle).ListFormat.Template = bulletListTemplate;
  72. paras.Last.GetRange().Runs.Add(expr, resStyle);
  73. }
  74. }
  75. }
  76. }
  77.