DataTplCollState.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.Globalization;
  10. using System.Linq;
  11. using GrapeCity.Documents.Word;
  12.  
  13. namespace DsWordWeb.Demos
  14. {
  15. // This example demonstrates the use of collection state functions
  16. // Index(), IsFirst() and IsLast() that can be used with the report templates
  17. // 'calc' feature.
  18. public class DataTplCollState
  19. {
  20. public GcWordDocument CreateDocx()
  21. {
  22. // The original data source (ocean and sea data from Wikipedia):
  23. var oceans = new[]
  24. {
  25. new { name = "Pacific", areaOfWorldOcean = 0.466, volumeOfWorldOcean = 0.501, seas = new[]
  26. { new {name = "Australasian Mediterranean Sea" }, new { name = "Philippine Sea" }, new { name = "Coral Sea" }, new { name = "South China Sea"} }
  27. },
  28. new { name = "Atlantic", areaOfWorldOcean = 0.235, volumeOfWorldOcean = 0.233, seas = new[]
  29. { new {name = "Sargasso Sea" }, new { name = "Caribbean Sea" }, new { name = "Mediterranean Sea" }, new { name = "Gulf of Guinea" } }
  30. },
  31. new { name = "Indian", areaOfWorldOcean = 0.195, volumeOfWorldOcean = 0.198, seas = new[]
  32. { new {name = "Arabian Sea" }, new { name = "Bay of Bengal" }, new { name = "Andaman Sea" }, new { name = "Laccadive Sea" } }
  33. },
  34. new { name = "Southern", areaOfWorldOcean = 0.061, volumeOfWorldOcean = 0.054, seas = new[]
  35. { new {name = "Weddell Sea" }, new { name = "Somov Sea" }, new { name = "Riiser-Larsen Sea" }, new { name = "Lazarev Sea" } }
  36. },
  37. new { name = "Arctic", areaOfWorldOcean = 0.043, volumeOfWorldOcean = 0.014, seas = new[]
  38. { new {name = "Barents Sea" }, new { name = "Greenland Sea" }, new { name = "East Siberian Sea" }, new { name = "Kara Sea" } }
  39. },
  40. };
  41. // Flatten the data source to get a flat collection of seas:
  42. var seas = oceans.SelectMany(o => o.seas);
  43.  
  44. // Create the document and add the sea list as the data source:
  45. var doc = new GcWordDocument();
  46. doc.DataTemplate.DataSources.Add("ds", seas);
  47.  
  48. // Template that for each record (sea) prints:
  49. // - The 1-based number of the collection element (sea) and the total number of records;
  50. // - The sea name;
  51. // - The string '(first)' after the first element (sea);
  52. // - The string '(last)' after the last element (sea):
  53. var p = doc.Body.Paragraphs.Add(
  54. "Record {{calc Index(ds)+1}} of {{calc Count(ds)}}: {{ds.name}}{{if IsFirst(ds)}} (first){{else}}{{if IsLast(ds)}} (last){{endif}}{{endif}}");
  55.  
  56. // Expand the template:
  57. doc.DataTemplate.Process(CultureInfo.GetCultureInfo("en-US"));
  58.  
  59. // Done:
  60. return doc;
  61. }
  62. }
  63. }
  64.