// 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 logical functions
    // that can be used with the 'calc' report templates feature.
    public class DataTplCalcLogical
        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.Heading3];
            var resStyle = doc.Styles[BuiltInStyleId.Strong];

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

            var paras = doc.Body.Paragraphs;
            add("{{ calc Iif(ds.value, \"Iif result true\", \"Iif result false\") }}");
            add("{{ calc IsNull(\"Nothing\") }}");

            // Process the templates:

            // Add a short note describing the demo at the top of the document:
                "This example demonstrates the available logical functions " +
                "that can be used with the 'calc' report templates feature. " +
                "Please see this example's source code for full details.",
            paras.Insert("Report templates: available calc logical 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);