''
'' This code is part of Document Solutions for Word demos.
'' Copyright (c) MESCIUS inc. All rights reserved.
''
Imports System.IO
Imports System.Globalization
Imports GrapeCity.Documents.Word
'' This sample demonstrates the use of BatchStepProcessor class,
'' which allows you to execute a single step of batch processing,
'' processing a single specified record of the data source.
''
'' See also the DataTplBatchOceans sample, which uses the same data source
'' but processes all data items in a batch.
'' NOTE: the C# original (DataTplBatchStep.cs) has a typo in its SampleId
'' attribute value - "DataTplBatchSte" instead of "DataTplBatchStep". Preserved
'' here verbatim so this VB sample resolves correctly against that existing C# sample.
Public Class DataTplBatchStep
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)
'' Initialize the batch step processor:
Dim batchProcessor = doc.DataTemplate.InitBatchStepProcessor(CultureInfo.GetCultureInfo("en-US"))
'' Get a random record index to process:
Dim recordIndex = Random.Shared.Next(0, batchProcessor.MaxRecordIndex)
'' Process the specific record index only.
'' Note that we must save the document using the action passed to ProcessRecord(),
'' as the original document reverts to its initial state after processing:
Dim ms = New MemoryStream()
batchProcessor.ProcessRecord(recordIndex, Sub() doc.Save(ms))
'' Load the processed document from the memory stream and return it:
ms.Position = 0
doc.Load(ms)
doc.Body.Paragraphs.Insert($"Note: This document was generated by processing record index {recordIndex} only.",
doc.Styles(BuiltInStyleId.BlockText), InsertLocation.Start)
Return doc
End Function
End Class