DataTplArithmeticOps.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 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
    {
        public GcWordDocument CreateDocx()
        {
            var rnd = Util.NewRandom();
            decimal next()
            {
                return new decimal(Math.Round((double)rnd.Next(1, 1000000) / rnd.Next(1, 100), 2));
            }

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

            var doc = new GcWordDocument();

            // Add the data source:
            doc.DataTemplate.DataSources.Add("ds", data);
            var paras = doc.Body.Paragraphs;

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

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

            var 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}}\n", resStyle);
            p.GetRange().Runs.Add("{\x200B{ calc ds.a + ds.b }} :  ");
            p.GetRange().Runs.Add("{{ calc ds.a + ds.b }}\n", resStyle);

            p.GetRange().Runs.Add("{\x200B{ calc ds.a - ds.b }} :  ");
            p.GetRange().Runs.Add("{{ calc ds.a - ds.b }}\n", resStyle);
            p.GetRange().Runs.Add("{\x200B{ calc ds.a * ds.b }} :  ");
            p.GetRange().Runs.Add("{{ calc ds.a * ds.b }}\n", resStyle);
            p.GetRange().Runs.Add("{\x200B{ calc ds.a / ds.b }} :  ");
            p.GetRange().Runs.Add("{{ calc ds.a / ds.b }}\n", 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;
        }
    }
}