DataTplCalcLogical.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 logical functions
  16. // that can be used with the 'calc' report templates feature.
  17. public class DataTplCalcLogical
  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.Heading3];
  32. var resStyle = doc.Styles[BuiltInStyleId.Strong];
  33.  
  34. var paras = doc.Body.Paragraphs;
  35. add("{{ calc Iif(ds.value, \"Iif result true\", \"Iif result false\") }}");
  36. add("{{ calc IsNull(\"Some data\") }}");
  37. add("{{ calc Not IsNull(\"Some data\") }}");
  38.  
  39. // Process the templates:
  40. doc.DataTemplate.Process(CultureInfo.GetCultureInfo("en-US"));
  41.  
  42. // Add a short note describing the demo at the top of the document:
  43. paras.Insert(
  44. "This example demonstrates the available logical functions " +
  45. "that can be used with the 'calc' report templates feature. " +
  46. "Please see this example's source code for full details.",
  47. InsertLocation.Start);
  48. paras.Insert("Report templates: available calc logical functions", doc.Styles[BuiltInStyleId.Heading1], InsertLocation.Start);
  49.  
  50. // Done:
  51. return doc;
  52.  
  53. void add(string expr)
  54. {
  55. // \x200B is a zero-width space used to prevent template expansion:
  56. paras.Add(expr.Insert(1, "​​​\x200B") + " : ", exStyle).ListFormat.Template = bulletListTemplate;
  57. paras.Last.GetRange().Runs.Add(expr, resStyle);
  58. }
  59. }
  60. }
  61. }
  62.