DataTplArithmeticOps.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 arithmetic operators
'' that can be used with the 'calc' report templates feature
'' to perform arithmetic operations on data.
'' For example, to insert the sum of two data fields,
'' the template '{{calc ds.a + ds.b)}}' can be used.
'' The data source used in this demo is a list of pairs of random decimal values.
Public Class DataTplArithmeticOps
    Function CreateDocx() As GcWordDocument
        Dim rnd = Util.NewRandom()
        Dim [next] As Func(Of Decimal) =
            Function()
                Return New Decimal(Math.Round(CDbl(rnd.Next(1, 1000000)) / rnd.Next(1, 100), 2))
            End Function

        '' A simple data source with pairs of random numeric data:
        Dim data =
        {
            New With {.a = [next](), .b = [next]()},
            New With {.a = [next](), .b = [next]()},
            New With {.a = [next](), .b = [next]()},
            New With {.a = [next](), .b = [next]()},
            New With {.a = [next](), .b = [next]()},
            New With {.a = [next](), .b = [next]()}
        }

        Dim doc = New GcWordDocument()

        '' Add the data source:
        doc.DataTemplate.DataSources.Add("ds", data)
        Dim paras = doc.Body.Paragraphs

        '' Styles and templates to show results:
        Dim myListTemplate = doc.ListTemplates.Add(BuiltInListTemplateId.NumberDefault, "myListTemplate")
        Dim resStyle = doc.Styles(BuiltInStyleId.Strong)

        '' Print data and result for each record (a zero-width space is used to prevent template expansion):
        paras.Add("Pairs of data values and results of arithmetic operations on them:", doc.Styles(BuiltInStyleId.Heading2))

        Dim p = paras.Add("{{#ds}}a = ", doc.Styles(BuiltInStyleId.ListParagraph))
        p.GetRange().Runs.Add("{{ds.a}}", resStyle)
        p.GetRange().Runs.Add(", b = ")
        p.GetRange().Runs.Add("{{ds.b}}" + vbLf, resStyle)
        p.GetRange().Runs.Add("{" + ChrW(&H200B) + "{ calc ds.a + ds.b }} :  ")
        p.GetRange().Runs.Add("{{ calc ds.a + ds.b }}" + vbLf, resStyle)

        p.GetRange().Runs.Add("{" + ChrW(&H200B) + "{ calc ds.a - ds.b }} :  ")
        p.GetRange().Runs.Add("{{ calc ds.a - ds.b }}" + vbLf, resStyle)
        p.GetRange().Runs.Add("{" + ChrW(&H200B) + "{ calc ds.a * ds.b }} :  ")
        p.GetRange().Runs.Add("{{ calc ds.a * ds.b }}" + vbLf, resStyle)
        p.GetRange().Runs.Add("{" + ChrW(&H200B) + "{ calc ds.a / ds.b }} :  ")
        p.GetRange().Runs.Add("{{ calc ds.a / ds.b }}" + vbLf, resStyle)
        p.GetRange().Runs.Add("{{/ds}}")
        p.ListFormat.Template = myListTemplate

        '' 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 arithmetic operators " +
            "that can be used with the 'calc' report templates feature " +
            "to perform arithmetic operations on data." +
            "For example, to insert the sum of two data fields, " +
            "the template '{{calc ds.a + ds.b)}}' can be used. " +
            "The data source used in this demo is a list of pairs of random decimal values. " +
            "Please see this sample source code for full details.",
            InsertLocation.Start)
        paras.Insert("Report templates: calc arithmetic operators", doc.Styles(BuiltInStyleId.Heading1), InsertLocation.Start)

        '' Done:
        Return doc
    End Function
End Class