DataTplArithmeticOps.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 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()
        {
            decimal next()
            {
                return new decimal(Math.Round((double)Util.NextRnd(1, 1000000) / Util.NextRnd(1, 100), 2));
            }

            // Generate 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);

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

            // Print data and result for each record (\x200B is a zero-width space to prevent template expansion):
            doc.Body.Paragraphs.Add("Pairs of data values and results of arithmetic operations on them:", doc.Styles[BuiltInStyleId.Heading2]);
            var p = doc.Body.Paragraphs.Add(
                "{{#ds}}" +
                "a = {{ds.a}}, b = {{ds.b}}\n" +
                "{\x200B{calc ds.a + ds.b}} : {{calc ds.a + ds.b}}\n" +
                "{\x200B{calc ds.a - ds.b}} : {{calc ds.a - ds.b}}\n" +
                "{\x200B{calc ds.a * ds.b}} : {{calc ds.a * ds.b}}\n" +
                "{\x200B{calc ds.a / ds.b}} : {{calc ds.a / ds.b}}" +
                "{{/ds}}",
                doc.Styles[BuiltInStyleId.ListParagraph]);
            p.ListFormat.Template = myListTemplate;

            // 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 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);
            doc.Body.Paragraphs.Insert("Report templates: calc arithmetic operators", doc.Styles[BuiltInStyleId.Heading1], InsertLocation.Start);

            // Done:
            return doc;
        }
    }
}