DataTplMultipleDataSrc.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 sample demonstrates the use of multiple data sources with data templates.
  16. // It uses two simple JSON data sources, one contains a list of some popular car makes,
  17. // the other a list of various car body styles. The DOCX includes two templates that are
  18. // used to print first the list of body styles, followed by the list of car brands.
  19. // The two lists are not related to each other, demonstrating how unrelated data sources
  20. // can be used to include various data in a single data bound document.
  21. public class DataTplMultipleDataSrc
  22. {
  23. public GcWordDocument CreateDocx()
  24. {
  25. var makes = "[" +
  26. "{ \"make\": \"Toyota\" }," +
  27. "{ \"make\": \"General Motors\" }," +
  28. "{ \"make\": \"Volkswagen\" }," +
  29. "{ \"make\": \"Ford\" }," +
  30. "{ \"make\": \"BMW\" }," +
  31. "{ \"make\": \"Nissan\" }," +
  32. "{ \"make\": \"Hyundai\" }," +
  33. "{ \"make\": \"Honda\" }," +
  34. "{ \"make\": \"Mazda\" }," +
  35. "{ \"make\": \"Jaguar\" }," +
  36. "]";
  37. var bodyStyles = "[" +
  38. "{ \"style\": \"Sedan\" }," +
  39. "{ \"style\": \"Coupe\" }," +
  40. "{ \"style\": \"Hatchback\" }," +
  41. "{ \"style\": \"SUV\" }," +
  42. "{ \"style\": \"Crossover\" }," +
  43. "{ \"style\": \"Minivan\" }," +
  44. "{ \"style\": \"Pickup\" }," +
  45. "{ \"style\": \"Wagon\" }," +
  46. "]";
  47.  
  48. var doc = new GcWordDocument();
  49.  
  50. // Add the data sources:
  51. doc.DataTemplate.DataSources.Add("makes", makes);
  52. doc.DataTemplate.DataSources.Add("styles", bodyStyles);
  53.  
  54. // Print the list of some car makes:
  55. doc.Body.Paragraphs.Add("Popular Car Makers", doc.Styles[BuiltInStyleId.Heading2]);
  56. doc.Body.Paragraphs.Add("{{#makes}}{{makes.make}}{{/makes}}", doc.Styles[BuiltInStyleId.ListBullet]);
  57.  
  58. // Print the list of car body styles:
  59. doc.Body.Paragraphs.Add("Car Body Styles", doc.Styles[BuiltInStyleId.Heading2]);
  60. doc.Body.Paragraphs.Add("{{#styles}}{{styles.style}}{{/styles}}", doc.Styles[BuiltInStyleId.ListBullet]);
  61.  
  62. doc.DataTemplate.Process(CultureInfo.GetCultureInfo("en-US"));
  63.  
  64. // Done:
  65. return doc;
  66. }
  67. }
  68. }
  69.