DataTplBatchOceans.vb
  1. ''
  2. '' This code is part of Document Solutions for Word demos.
  3. '' Copyright (c) MESCIUS inc. All rights reserved.
  4. ''
  5. Imports System.IO
  6. Imports System.Drawing
  7. Imports System.Text
  8. Imports System.Data
  9. Imports System.Linq
  10. Imports System.Globalization
  11. Imports GrapeCity.Documents.Word
  12.  
  13. '' This sample demonstrates the use of DataTemplate.BatchProcess() method,
  14. '' which allows to loop over the root items of a data source, generating
  15. '' a separate DOCX for each data source item.
  16. Public Class DataTplBatchOceans
  17. Function CreateDocx() As GcWordDocument
  18. '' The data source (ocean And sea data from Wikipedia):
  19. Dim oceans =
  20. {
  21. New With {.name = "Pacific", .areaOfWorldOcean = 0.466, .volumeOfWorldOcean = 0.501, .seas =
  22. {New With {.name = "Australasian Mediterranean Sea"}, New With {.name = "Philippine Sea"}, New With {.name = "Coral Sea"}, New With {.name = "South China Sea"}}},
  23. New With {.name = "Atlantic", .areaOfWorldOcean = 0.235, .volumeOfWorldOcean = 0.233, .seas =
  24. {New With {.name = "Sargasso Sea"}, New With {.name = "Caribbean Sea"}, New With {.name = "Mediterranean Sea"}, New With {.name = "Gulf of Guinea"}}},
  25. New With {.name = "Indian", .areaOfWorldOcean = 0.195, .volumeOfWorldOcean = 0.198, .seas =
  26. {New With {.name = "Arabian Sea"}, New With {.name = "Bay of Bengal"}, New With {.name = "Andaman Sea"}, New With {.name = "Laccadive Sea"}}},
  27. New With {.name = "Southern", .areaOfWorldOcean = 0.061, .volumeOfWorldOcean = 0.054, .seas =
  28. {New With {.name = "Weddell Sea"}, New With {.name = "Somov Sea"}, New With {.name = "Riiser-Larsen Sea"}, New With {.name = "Lazarev Sea"}}},
  29. New With {.name = "Arctic", .areaOfWorldOcean = 0.043, .volumeOfWorldOcean = 0.014, .seas =
  30. {New With {.name = "Barents Sea"}, New With {.name = "Greenland Sea"}, New With {.name = "East Siberian Sea"}, New With {.name = "Kara Sea"}}}
  31. }
  32.  
  33. '' Create And load the template DOCX:
  34. Dim doc = New GcWordDocument()
  35. doc.Load(Path.Combine("Resources", "WordDocs", "BatchOceansTemplate.docx"))
  36.  
  37. '' Add the data source to the data template data sources
  38. doc.DataTemplate.DataSources.Add("ds", oceans)
  39.  
  40. '' In this sample, we use the DataTemplate.BatchProcess() method which iterates over the root data items,
  41. '' generating a separate document for each item. To allow the sample to show the result as a single document,
  42. '' we add each generated document as a separate section to a New DOCX.
  43. '' Note that when DataTemplate.BatchProcess() finishes processing, the original GcWordDocument on which
  44. '' it was called reverts to its initial state (i.e. it contains the unprocessed template tags rather than data).
  45. '' If you want to preserve the results of the batch processing, you must save the document in its current
  46. '' state each time the 'itemProcessed' action gets called.
  47.  
  48. '' The document to contain all results:
  49. Dim allDocs = New GcWordDocument()
  50. '' This makes sure the resulting document has the same styles as the template:
  51. allDocs.Load(Path.Combine("Resources", "WordDocs", "BatchOceansTemplate.docx"))
  52. allDocs.Body.Clear()
  53.  
  54. '' The extra section break:
  55. Dim lastSection As Section = Nothing
  56.  
  57. '' The callback method called as each root data item Is processed:
  58. Dim itemProcessed As Action =
  59. Sub()
  60. '' In order to keep the processed template, we must either save the original document
  61. '' in its current state to a file, Or copy its content to another document as we do here,
  62. '' as the content Is regenerated for each root data item, And Is reset to the original
  63. '' template when processing completes:
  64. doc.Body.CopyTo(allDocs.Body, InsertLocation.End, FormattingCopyStrategy.Copy)
  65. lastSection = allDocs.Body.Paragraphs.Last.AddSectionBreak()
  66. End Sub
  67.  
  68. '' Batch process the template:
  69. doc.DataTemplate.BatchProcess(itemProcessed, CultureInfo.GetCultureInfo("en-US"))
  70. '' Delete the extra section break:
  71. lastSection.Delete()
  72.  
  73. '' Done
  74. Return allDocs
  75. End Function
  76. End Class
  77.