DataTplCalcFormat.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 use of 'calc' Format function
    // with optional format string and locale arguments.
    public class DataTplCalcFormat
    {
        public GcWordDocument CreateDocx()
        {
            var dsDate = new DateTime[] { new DateTime(2023, 04, 12) };
            var dsNum = new double[] { 123.456 };

            var doc = new GcWordDocument();
            var list = doc.ListTemplates.Add(BuiltInListTemplateId.BulletDefault, "myListTemplate");
            var style = doc.Styles[BuiltInStyleId.ListParagraph];

            doc.DataTemplate.DataSources.Add("dsDate", dsDate);
            doc.DataTemplate.DataSources.Add("dsNum", dsNum);

            var paras = doc.Body.Paragraphs;
            var p = paras.Add("Date formatting examples:", style);
            p.ListFormat.Template = list;
            p = paras.Add("Date with default formatting:  {{calc Format(dsDate.value)}}", style);
            p.ListFormat.Template = list;
            p.ListFormat.LevelNumber = 1;
            p = paras.Add("Date formatted as ('D', 'en-US'):  {{calc Format(dsDate.value, \"D\", \"en-US\")}}", style);
            p.ListFormat.Template = list;
            p.ListFormat.LevelNumber = 1;
            p = paras.Add("Date formatted as ('D', 'fr-FR'):  {{calc Format(dsDate.value, \"D\", \"fr-FR\")}}", style);
            p.ListFormat.Template = list;
            p.ListFormat.LevelNumber = 1;
            p = paras.Add("Date formatted as ('D', 'ja-JP'):  {{calc Format(dsDate.value, \"D\", \"ja-JP\")}}", style);
            p.ListFormat.Template = list;
            p.ListFormat.LevelNumber = 1;

            p = paras.Add("Number formatting examples:", style);
            p.ListFormat.Template = list;
            p = paras.Add("Number with default formatting:  {{calc Format(dsNum.value)}}", style);
            p.ListFormat.Template = list;
            p.ListFormat.LevelNumber = 1;
            p = paras.Add("Number formatted as ('C', 'en-US'):  {{calc Format(dsNum.value, \"C\", \"en-US\")}}", style);
            p.ListFormat.Template = list;
            p.ListFormat.LevelNumber = 1;
            p = paras.Add("Number formatted as ('C', 'fr-FR'):  {{calc Format(dsNum.value, \"C\", \"fr-FR\")}}", style);
            p.ListFormat.Template = list;
            p.ListFormat.LevelNumber = 1;
            p = paras.Add("Number formatted as ('C3', 'ja-JP'):  {{calc Format(dsNum.value, \"C3\", \"ja-JP\")}}", style);
            p.ListFormat.Template = list;
            p.ListFormat.LevelNumber = 1;

            p = paras.Add("Expression formatting examples:", style);
            p.ListFormat.Template = list;
            p = paras.Add("Numeric expression:  {{calc Format(dsNum.value / 2)}}", style);
            p.ListFormat.Template = list;
            p.ListFormat.LevelNumber = 1;
            p = paras.Add("Boolean expression:  {{calc Format(IsFirst(dsNum.value))}}", style);
            p.ListFormat.Template = list;
            p.ListFormat.LevelNumber = 1;

            doc.DataTemplate.Process(CultureInfo.GetCultureInfo("en-US"));

            // Done:
            return doc;
        }
    }
}