DataTplRestartList.vb
''
'' 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 the restart list formatter
'' to print the list of oceans and each ocean's list of seas,
'' restarting the inner seas list for each ocean.
'' This sample is similar to DataTplNestedList but uses a JSON
'' data source, and restarts the nested list for each ocean.
Public Class DataTplRestartList
    Function CreateDocx() As GcWordDocument
        Dim doc = New GcWordDocument()

        '' Add JSON data source to the data template data sources:
        Using oceans = File.OpenRead(Path.Combine("Resources", "data", "oceans.json"))
            doc.DataTemplate.DataSources.Add("ds", oceans)
        End Using

        '' Our document consists of a top list with ocean names,
        '' and a nested list with each ocean's sea names.
        '' The ':restart()' formatter associated with a range restarts
        '' the nested lists on each iteration over the range,
        '' but does not restart the outer list - which is what we
        '' want in this and most other cases (to restart the topmost list too,
        '' use the optional 'all' parameter ':restart(all)'):
        Dim pTop = doc.Body.Paragraphs.Add("{{#ds}:restart()}{{ds.name}} Ocean:", doc.Styles(BuiltInStyleId.ListNumber))
        Dim pNested = doc.Body.Paragraphs.Add("{{#ds.seas}}{{ds.seas.name}}{{/ds.seas}}{{/ds}}", doc.Styles(BuiltInStyleId.ListNumber2))

        '' Add list templates and set up list formatting:
        Dim topListTemplate = doc.ListTemplates.Add(BuiltInListTemplateId.NumberDefault, "topListTemplate")
        Dim nestedListTemplate = doc.ListTemplates.Add(BuiltInListTemplateId.NumberDefault, "nestedListTemplate")
        pTop.ListFormat.Template = topListTemplate
        pNested.ListFormat.Template = nestedListTemplate
        pNested.ListFormat.LevelNumber = 1

        '' This call expands all data templates in the document,
        '' replacing template tags with data (iterating over all data items):
        doc.DataTemplate.Process(CultureInfo.GetCultureInfo("en-US"))

        '' Done:
        Return doc
    End Function
End Class