DataTplRunBlockBehavior.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 shows how to use the 'rbb' ('run-block-behavior') formatter
  16. // to print the data items of a list within a single paragraph rather than
  17. // as a list of paragraphs.
  18. public class DataTplRunBlockBehavior
  19. {
  20. public GcWordDocument CreateDocx()
  21. {
  22. using var oceans = File.OpenRead(Path.Combine("Resources", "data", "oceans.json"));
  23. var doc = new GcWordDocument();
  24. doc.DataTemplate.DataSources.Add("ds", oceans);
  25.  
  26. // Add a list for oceans, and a run for seas:
  27. var p = doc.Body.Paragraphs.Add("{{#ds}}{{ds.name}} ocean: ", doc.Styles[BuiltInStyleId.ListParagraph]);
  28. var myListTemplate = doc.ListTemplates.Add(BuiltInListTemplateId.BulletDefault, "myListTemplate");
  29. p.ListFormat.Template = myListTemplate;
  30.  
  31. // The 'rbb' ('run-block-behavior') formatter forces the range to be expanded
  32. // within the current paragraph, each data item generating a run rather than a paragraph:
  33. doc.Body.Paragraphs.First.GetRange().Runs.Add("{{#ds.seas}:rbb()}{{ds.seas.name}}; {{/ds.seas}}");
  34.  
  35. // Close the parent range '#ds':
  36. doc.Body.Paragraphs.First.GetRange().Runs.Add("{{/ds}}");
  37.  
  38. // Process the template:
  39. doc.DataTemplate.Process(CultureInfo.GetCultureInfo("en-US"));
  40.  
  41. return doc;
  42. }
  43. }
  44. }
  45.