DataTplCalcText.cs
//
// This code is part of GrapeCity Documents for Word samples.
// Copyright (c) GrapeCity, Inc. All rights reserved.
//
using System;
using System.IO;
using System.Drawing;
using System.Collections.Generic;
using System.Linq;
using GrapeCity.Documents.Word;

namespace GcWordWeb.Samples
{
    // 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
    {
        public GcWordDocument CreateDocx()
        {
            const int MinLen = 5;

            int next()
            {
                return Util.NextRnd(1, 5);
            }

            string nextWord()
            {
                for (int i = 0; i < 50; ++i)
                {
                    var t = Util.LoremIpsumWord();
                    if (t.Length >= MinLen)
                        return t;
                }
                return "lorem";
            }

            // Generate a simple data source with pairs of random numeric data:
            var data = new[]
            {
                new { a = nextWord(), b = nextWord(), c = nextWord(), n = next(), },
            };

            var doc = new GcWordDocument();

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

            // A list template to print the random data:
            var myListTemplate = doc.ListTemplates.Add(BuiltInListTemplateId.BulletDefault, "myListTemplate");

            var style = doc.Styles[BuiltInStyleId.ListParagraph];

            add("Asc (ASCII code of char)", "Asc(\"{{ds.a}}\") = {{calc Asc(ds.a)}}");
            // TBD: format? add("Chr (ASCII code to char)", "Asc(49) = {{calc Asc(49)}}");
            add("Concat", "Concat(\"{{ds.a}}\", \"{{ds.b}}\", \"{{ds.c}}\") = \"{{calc Concat(ds.a, ds.b, ds.c)}}\"");
            add("Contains", "Contains(\"{{ds.a}}\", \"s\") = {{calc Contains(ds.a, \"s\")}}");
            add("EndsWith", "EndsWith(\"{{ds.a}}\", \"s\") = {{calc EndsWith(ds.a, \"s\")}}");
            add("Insert", "Insert(\"{{ds.a}}\", {{ds.n}}, \"{{ds.b}}\") = \"{{calc Insert(ds.a, ds.n, ds.b)}}\"");
            add("InStr", "InStr(\"{{ds.a}}\", \"s\") = {{calc InStr(ds.a, \"s\")}}");
            add("InStr", "InStr(\"{{ds.a}}\", \"s\", {{ds.n}}) = {{calc InStr(ds.a, \"s\", ds.n)}}");
            add("LCase", "LCase(\"Lorem Ipsum!\") = \"{{calc LCase(\"Lorem Ipsum!\")}}\"");
            add("Len", "Len(\"{{ds.a}}\") = \"{{calc Len(ds.a)}}\"");
            add("LSet", "LSet(\"{{ds.a}}\", 12) = \"{{calc LSet(ds.a, 12)}}\"");
            add("LSet", "LSet(\"{{ds.a}}\", 12, '!') = \"{{calc LSet(ds.a, 12, \"!\")}}\"");
            add("Mid", "Mid(\"{{ds.a}}\", 2) = \"{{calc Mid(ds.a, 2)}}\"");
            add("Mid", "Mid(\"{{ds.a}}\", 2, 1) = \"{{calc Mid(ds.a, 2, 1)}}\"");
            add("Remove", "Remove(\"{{ds.a}}\", 2) = \"{{calc Remove(ds.a, 2)}}\"");
            add("Remove", "Remove(\"{{ds.a}}\", 2, 1) = \"{{calc Remove(ds.a, 2, 1)}}\"");
            add("Replace", "Replace(\"loremipsumlobortis\", \"lorem\",\"ipsum\") = \"{{calc Replace(\"loremipsumlobortis\", \"lorem\",\"ipsum\")}}\"");
            add("RSet", "RSet(\"{{ds.a}}\", 12) = \"{{calc RSet(ds.a, 12)}}\"");
            add("RSet", "RSet(\"{{ds.a}}\", 12, '!') = \"{{calc RSet(ds.a, 12, \"!\")}}\"");
            add("StartsWith", "StartsWith(\"{{ds.a}}\", \"s\") = {{calc StartsWith(ds.a, \"s\")}}");
            add("StrReverse", "StrReverse(\"{{ds.a}}\") = \"{{calc StrReverse(ds.a)}}\"");
            add("Trim", "Trim(\"  Lorem Ipsum  \") = \"{{calc Trim(\"  Lorem Ipsum  \")}}\"");
            add("UCase", "UCase(\"Lorem Ipsum!\") = \"{{calc UCase(\"Lorem Ipsum!\")}}\"");

            // Process the templates:
            doc.DataTemplate.Process();

            // Add a short note describing the demo at the top of the document:
            doc.Body.Paragraphs.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 values. " +
                "Please see this sample source code for full details.",
                InsertLocation.Start);
            doc.Body.Paragraphs.Insert("Report templates: available calc operators", doc.Styles[BuiltInStyleId.Heading1], InsertLocation.Start);

            // Done:
            return doc;

            void add(string caption, string expr)
            {
                var pp = doc.Body.Paragraphs.Add(caption + ": " + expr, style);
                pp.ListFormat.Template = myListTemplate;
            }
        }
    }
}