DataTplCalcText.vb
''
'' This code is part of Document Solutions for Word demos.
'' Copyright (c) MESCIUS inc. All rights reserved.
''
Imports System.Globalization
Imports GrapeCity.Documents.Word

'' This example demonstrates the available text functions
'' that can be used with the 'calc' report templates feature
'' to perform various operations on strings.
'' For example, to insert the concatenation of three text data fields,
'' the template '{{calc Concat(ds.a, ds.b, ds.c)}}' can be used.
'' The data source used in this demo contains a single record
'' with random string values.
Public Class DataTplCalcText
    Function CreateDocx() As GcWordDocument
        Const MinLen As Integer = 5
        Dim rnd = Util.NewRandom()
        Dim [next] As Func(Of Integer) =
            Function()
                Return rnd.Next(1, 5)
            End Function

        Dim nextWord As Func(Of String) =
            Function()
                For i = 0 To 49
                    Dim t = Util.LoremIpsumWord()
                    If t.Length >= MinLen Then
                        Return t
                    End If
                Next
                Return "lorem"
            End Function

        '' Generate a simple data source with random string and numeric data:
        Dim data =
        {
            New With {.a = nextWord(), .b = nextWord(), .c = nextWord(), .n = [next](), .q = nextWord() + """" + nextWord()}
        }

        Dim doc = New GcWordDocument()
        doc.Body.Sections.First.PageSetup.Margin.Top = 36
        doc.Body.Sections.First.PageSetup.Margin.Bottom = 36
        doc.Body.Sections.First.PageSetup.Margin.Left = 36
        doc.Body.Sections.First.PageSetup.Margin.Right = 36
        Dim paras = doc.Body.Paragraphs

        '' Add the data source:
        doc.DataTemplate.DataSources.Add("ds", data)

        '' Styles and templates to show results:
        Dim bulletListTemplate = doc.ListTemplates.Add(BuiltInListTemplateId.BulletDefault, "bulletListTemplate")
        Dim exStyle = doc.Styles(BuiltInStyleId.Heading3)
        exStyle.ParagraphFormat.TabStops.Add(110)
        Dim resStyle = doc.Styles(BuiltInStyleId.Strong)

        Dim add As Action(Of String, String) =
            Sub(caption, expr)
                '' Four zero-width spaces are inserted after the opening brace to prevent template expansion:
                paras.Add(caption + vbTab + expr.Insert(1, New String(ChrW(&H200B), 4)) + " :  ", exStyle).ListFormat.Template = bulletListTemplate
                paras.Last.GetRange().Runs.Add(expr, resStyle)
            End Sub

        add("Data field a", "{{ds.a}}")
        add("Data field b", "{{ds.b}}")
        add("Data field c", "{{ds.c}}")
        add("Data field n", "{{ds.n}}")
        add("Data field q", "{{ds.q}}") '' q is used to show how to handle double quotes in string functions

        add("Asc", "{{calc Asc(ds.a)}}")
        add("Concat", "{{calc Concat(ds.a, ds.b, ds.c)}}")
        add("Contains", "{{calc Contains(ds.a, ""s"")}}")
        add("Contains '" + Chr(34) + "'", "{{calc Contains(ds.q, Chr(34))}}")
        add("EndsWith", "{{calc EndsWith(ds.a, ""s"")}}")
        add("Insert", "{{calc Insert(ds.a, ds.n, ds.b)}}")
        add("InStr", "{{calc InStr(ds.a, ""s"")}}")
        add("InStr", "{{calc InStr(ds.a, ""s"", ds.n)}}")
        add("LCase", "{{calc LCase(""Lorem Ipsum!"")}}")
        add("Len", "{{calc Len(ds.a)}}")
        add("LSet", "{{calc LSet(ds.a, 12)}}")
        add("LSet", "{{calc LSet(ds.a, 12, ""!"")}}")
        add("Mid", "{{calc Mid(ds.a, 2)}}")
        add("Mid", "{{calc Mid(ds.a, 2, 1)}}")
        add("Remove", "{{calc Remove(ds.a, 2)}}")
        add("Remove", "{{calc Remove(ds.a, 2, 1)}}")
        add("Replace", "{{calc Replace(""loremipsumlobortis"", ""lorem"", ""ipsum"")}}")
        add("Replace " + Chr(34) + "'" + Chr(34), "{{calc Replace(ds.q, Chr(34), ""-"")}}")
        add("RSet", "{{calc RSet(ds.a, 12)}}")
        add("RSet", "{{calc RSet(ds.a, 12, ""!"")}}")
        add("StartsWith", "{{calc StartsWith(ds.a, ""s"")}}")
        add("StrReverse", "{{calc StrReverse(ds.a)}}")
        add("Trim", "{{calc Trim(""  Lorem Ipsum  "")}}")
        add("UCase", "{{calc UCase(""Lorem Ipsum!"")}}")

        '' Process the templates:
        doc.DataTemplate.Process(CultureInfo.GetCultureInfo("en-US"))

        '' Add a short note describing the demo at the top of the document:
        paras.Insert(
            "This example demonstrates the available text functions " +
            "that can be used with the 'calc' report templates feature " +
            "to perform various operations on strings." +
            "For example, to insert the concatenation of three text data fields, " +
            "the template '{{calc Concat(ds.a, ds.b, ds.c)}}' can be used. " +
            "The data source used in this demo contains a single record " +
            "with random string and numeric values shown by the first items in the list below. " +
            "Please see this sample source code for full details.",
            InsertLocation.Start)
        paras.Insert("Report templates: available calc operators", doc.Styles(BuiltInStyleId.Heading1), InsertLocation.Start)

        '' Done:
        Return doc
    End Function
End Class