DataTplCalcConvert.cs
//
// This code is part of Document Solutions for Word demos.
// Copyright (c) MESCIUS inc. All rights reserved.
//
using System;
using System.IO;
using System.Drawing;
using System.Collections.Generic;
using System.Linq;
using System.Globalization;
using GrapeCity.Documents.Word;

namespace DsWordWeb.Demos
{
    // This example demonstrates the available conversion functions
    // that can be used with the 'calc' report templates feature.
    public class DataTplCalcConvert
    {
        public GcWordDocument CreateDocx()
        {
            // A simple data source to be used by the logical functions:
            var data = new bool[] { true };

            var doc = new GcWordDocument();

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

            // Styles and templates to show results:
            var bulletListTemplate = doc.ListTemplates.Add(BuiltInListTemplateId.BulletDefault, "bulletListTemplate");
            var exStyle = doc.Styles[BuiltInStyleId.ListParagraph];
            var resStyle = doc.Styles[BuiltInStyleId.Strong];

            var paras = doc.Body.Paragraphs;
            add("{{ calc CBool(\"True\") }}");
            add("{{ calc CBool(\"False\") }}");
            add("{{ calc CByte(\"127\") }}");
            add("{{ calc CChar(\"\u4249\") }}");
            add("{{ calc CDate(\"1-jan-2001\") }}");
            add("{{ calc CDbl(\"123.456\") }}");
            add("{{ calc CDec(\"123.456\") }}");
            add("{{ calc CInt(\"123\") }}");
            add("{{ calc CLng(\"123456789\") }}");
            add("{{ calc CObj(\"Nothing\") }}");
            add("{{ calc CByte(\"123\") }}");
            add("{{ calc CShort(\"123\") }}");
            add("{{ calc CSng(\"123\") }}");
            add("{{ calc CStr(123.456) }}");
            add("{{ calc CType(123, \"System.Int64\") }}");
            add("{{ calc CUInt(\"123\") }}");
            add("{{ calc CULong(\"123\") }}");
            add("{{ calc CUShort(\"123\") }}");

            // 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 conversion functions " +
                "that can be used with the 'calc' report templates feature. " +
                "Please see this example's source code for full details.",
                InsertLocation.Start);
            paras.Insert("Report templates: available calc conversion functions", doc.Styles[BuiltInStyleId.Heading1], InsertLocation.Start);

            // Done:
            return doc;

            void add(string expr)
            {
                // \x200B is a zero-width space used to prevent template expansion:
                paras.Add(expr.Insert(1, "​​​\x200B") + " :  ", exStyle).ListFormat.Template = bulletListTemplate;
                paras.Last.GetRange().Runs.Add(expr, resStyle);
            }
        }
    }
}