DataTplBatchOceans.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 DataTemplate.BatchProcess() method,
  16. // which allows to loop over the root items of a data source, generating
  17. // a separate DOCX for each data source item.
  18. public class DataTplBatchOceans
  19. {
  20. public GcWordDocument CreateDocx()
  21. {
  22. // The 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.  
  42. // Create and load the template DOCX:
  43. var doc = new GcWordDocument();
  44. doc.Load(Path.Combine("Resources", "WordDocs", "BatchOceansTemplate.docx"));
  45.  
  46. // Add the data source to the data template data sources
  47. doc.DataTemplate.DataSources.Add("ds", oceans);
  48.  
  49. // In this sample, we use the DataTemplate.BatchProcess() method which iterates over the root data items,
  50. // generating a separate document for each item. To allow the sample to show the result as a single document,
  51. // we add each generated document as a separate section to a new DOCX.
  52. // Note that when DataTemplate.BatchProcess() finishes processing, the original GcWordDocument on which
  53. // it was called reverts to its initial state (i.e. it contains the unprocessed template tags rather than data).
  54. // If you want to preserve the results of the batch processing, you must save the document in its current
  55. // state each time the 'itemProcessed' action gets called.
  56.  
  57. // The document to contain all results:
  58. var allDocs = new GcWordDocument();
  59. // This makes sure the resulting document has the same styles as the template:
  60. allDocs.Load(Path.Combine("Resources", "WordDocs", "BatchOceansTemplate.docx"));
  61. allDocs.Body.Clear();
  62.  
  63. // The extra section break:
  64. Section lastSection = null;
  65. // Batch process the template:
  66. doc.DataTemplate.BatchProcess(itemProcessed, CultureInfo.GetCultureInfo("en-US"));
  67. // Delete the extra section break:
  68. lastSection.Delete();
  69.  
  70. // Done:
  71. return allDocs;
  72.  
  73. // The callback method called as each root data item is processed:
  74. void itemProcessed()
  75. {
  76. // In order to keep the processed template, we must either save the original document
  77. // in its current state to a file, or copy its content to another document as we do here,
  78. // as the content is regenerated for each root data item, and is reset to the original
  79. // template when processing completes:
  80. doc.Body.CopyTo(allDocs.Body, InsertLocation.End, FormattingCopyStrategy.Copy);
  81. lastSection = allDocs.Body.Paragraphs.Last.AddSectionBreak();
  82. }
  83. }
  84. }
  85. }
  86.