''
'' 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 how to use 'sequence' ('seq') and 'follow' formatters
'' to synchronize two ranges from two different JSON data sources.
Public Class DataTplSequence
Function CreateDocx() As GcWordDocument
'' The first JSON data source - the sequence master.
'' In this sample we use it to spell sequence numbers.
'' Note that the resulting number of iterations will be the MINIMUM
'' of the lengths of the sequence and follower, so print all oceans
'' we need to provide at least 5 numbers, but more won't hurt:
Dim sequence =
"[" +
"{ ""num"": ""First"" }," +
"{ ""num"": ""Second"" }," +
"{ ""num"": ""Third"" }," +
"{ ""num"": ""Fourth"" }," +
"{ ""num"": ""Fifth"" }," +
"{ ""num"": ""Sixth"" }," +
"{ ""num"": ""Seventh"" }," +
"{ ""num"": ""Eights"" }," +
"{ ""num"": ""Ninth"" }," +
"{ ""num"": ""Tenth"" }" +
"]"
Dim doc = New GcWordDocument()
'' Add the data sources to the data template data sources:
doc.DataTemplate.DataSources.Add("nums", sequence)
'' The second 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 only paragraph added to the document will print the sequence number
'' fetched from the first data source (the sequence master), and the ocean name
'' fetched from the second data source (the sequence follower):
Dim p = doc.Body.Paragraphs.Add("{{#nums}:seq(seq1)}{{nums.num}} -- {{#ds}:follow(seq1)}{{ds.name}}{{/ds}}{{/nums}}", 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