''
'' 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 is similar to DataTplSimpleList
'' but uses a JSON data source to generate
'' the list of ocean seas.
Public Class DataTplSimpleListJson
Function CreateDocx() As GcWordDocument
Dim doc = New GcWordDocument()
'' The JSON data source is loaded from oceans.json:
Using oceans = File.OpenRead(Path.Combine("Resources", "data", "oceans.json"))
doc.DataTemplate.DataSources.Add("ds", oceans)
End Using
'' Add a list template so that the data is formatted as a list:
Dim myListTemplate = doc.ListTemplates.Add(BuiltInListTemplateId.BulletDefault, "myListTemplate")
'' The single paragraph added to the document contains template tags:
'' - {{#ds}}..{{/ds}} -- root template, 'ds' is the name of the data source;
'' - {ds.seas.name} -- fetches the sea name;
'' - :toupper() -- uppercases the data:
Dim p = doc.Body.Paragraphs.Add("{{#ds}}{{ds.seas.name}:toupper()}{{/ds}}", doc.Styles(BuiltInStyleId.ListParagraph))
p.ListFormat.Template = myListTemplate
'' 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