DataTplBatchOceans.vb
''
'' This code is part of Document Solutions for Word demos.
'' Copyright (c) MESCIUS inc. All rights reserved.
''
Imports System.IO
Imports System.Drawing
Imports System.Text
Imports System.Data
Imports System.Linq
Imports System.Globalization
Imports GrapeCity.Documents.Word

'' This sample demonstrates the use of DataTemplate.BatchProcess() method,
'' which allows to loop over the root items of a data source, generating
'' a separate DOCX for each data source item.
Public Class DataTplBatchOceans
    Function CreateDocx() As GcWordDocument
        '' The data source (ocean And sea data from Wikipedia):
        Dim oceans =
        {
            New With {.name = "Pacific", .areaOfWorldOcean = 0.466, .volumeOfWorldOcean = 0.501, .seas =
                {New With {.name = "Australasian Mediterranean Sea"}, New With {.name = "Philippine Sea"}, New With {.name = "Coral Sea"}, New With {.name = "South China Sea"}}},
            New With {.name = "Atlantic", .areaOfWorldOcean = 0.235, .volumeOfWorldOcean = 0.233, .seas =
                {New With {.name = "Sargasso Sea"}, New With {.name = "Caribbean Sea"}, New With {.name = "Mediterranean Sea"}, New With {.name = "Gulf of Guinea"}}},
            New With {.name = "Indian", .areaOfWorldOcean = 0.195, .volumeOfWorldOcean = 0.198, .seas =
                {New With {.name = "Arabian Sea"}, New With {.name = "Bay of Bengal"}, New With {.name = "Andaman Sea"}, New With {.name = "Laccadive Sea"}}},
            New With {.name = "Southern", .areaOfWorldOcean = 0.061, .volumeOfWorldOcean = 0.054, .seas =
                {New With {.name = "Weddell Sea"}, New With {.name = "Somov Sea"}, New With {.name = "Riiser-Larsen Sea"}, New With {.name = "Lazarev Sea"}}},
            New With {.name = "Arctic", .areaOfWorldOcean = 0.043, .volumeOfWorldOcean = 0.014, .seas =
                {New With {.name = "Barents Sea"}, New With {.name = "Greenland Sea"}, New With {.name = "East Siberian Sea"}, New With {.name = "Kara Sea"}}}
        }

        '' Create And load the template DOCX:
        Dim doc = New GcWordDocument()
        doc.Load(Path.Combine("Resources", "WordDocs", "BatchOceansTemplate.docx"))

        '' Add the data source to the data template data sources
        doc.DataTemplate.DataSources.Add("ds", oceans)

        '' In this sample, we use the DataTemplate.BatchProcess() method which iterates over the root data items,
        '' generating a separate document for each item. To allow the sample to show the result as a single document,
        '' we add each generated document as a separate section to a New DOCX.
        '' Note that when DataTemplate.BatchProcess() finishes processing, the original GcWordDocument on which
        '' it was called reverts to its initial state (i.e. it contains the unprocessed template tags rather than data).
        '' If you want to preserve the results of the batch processing, you must save the document in its current
        '' state each time the 'itemProcessed' action gets called.

        '' The document to contain all results:
        Dim allDocs = New GcWordDocument()
        '' This makes sure the resulting document has the same styles as the template:
        allDocs.Load(Path.Combine("Resources", "WordDocs", "BatchOceansTemplate.docx"))
        allDocs.Body.Clear()

        '' The extra section break:
        Dim lastSection As Section = Nothing

        '' The callback method called as each root data item Is processed:
        Dim itemProcessed As Action =
            Sub()
                '' In order to keep the processed template, we must either save the original document
                '' in its current state to a file, Or copy its content to another document as we do here,
                '' as the content Is regenerated for each root data item, And Is reset to the original
                '' template when processing completes:
                doc.Body.CopyTo(allDocs.Body, InsertLocation.End, FormattingCopyStrategy.Copy)
                lastSection = allDocs.Body.Paragraphs.Last.AddSectionBreak()
            End Sub

        '' Batch process the template:
        doc.DataTemplate.BatchProcess(itemProcessed, CultureInfo.GetCultureInfo("en-US"))
        '' Delete the extra section break:
        lastSection.Delete()

        '' Done
        Return allDocs
    End Function
End Class