DataTplCalcMath.cs
C# VB
//// This code is part of Document Solutions for Word demos.// Copyright (c) MESCIUS inc. All rights reserved.//using System;using System.Globalization;using GrapeCity.Documents.Word;
namespace DsWordWeb.Demos{ // This example demonstrates the available math functions // that can be used with the 'calc' report templates feature. public class DataTplCalcMath { public GcWordDocument CreateDocx() { // A simple data source to be used by the math functions: var data = new double[] { Math.PI };
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 paras = doc.Body.Paragraphs; add("{{ ds.value }}"); add("{{ calc Abs(-ds.value) }}"); add("{{ calc Round(ds.value, 2) }}"); add("{{ calc Round(ds.value, 2, \"banker\") }}"); // "banker" aliases: "toeven", "halfeven" add("{{ calc Round(ds.value, 2, \"halfup\") }}"); // "halfup" alias: "awayfromzero" add("{{ calc Round(ds.value, 2, \"halfdown\") }}"); add("{{ calc Round(ds.value, 2, \"up\") }}"); add("{{ calc Round(ds.value, 2, \"down\") }}"); // "down" aliases: "truncate", "tozero" add("{{ calc Round(ds.value, 2, \"ceiling\") }}"); // "ceiling" alias: "ceil" add("{{ calc Round(ds.value, 2, \"floor\") }}"); add("{{ calc Acos(ds.value) }}"); add("{{ calc Asin(ds.value) }}"); add("{{ calc Atan(ds.value) }}"); add("{{ calc Atan2(ds.value, 1) }}"); add("{{ calc Ceiling(ds.value) }}"); add("{{ calc Cos(ds.value) }}"); add("{{ calc Cosh(ds.value) }}"); add("{{ calc Exp(ds.value) }}"); add("{{ calc Floor(ds.value) }}"); add("{{ calc Log10(ds.value) }}"); add("{{ calc Log(ds.value) }}"); add("{{ calc Log(ds.value, 10) }}"); add("{{ calc Pow(ds.value, 2) }}"); add("{{ calc Rand() }}"); add("{{ calc RandBetween(0, 100) }}"); add("{{ calc Sign(ds.value) }}"); add("{{ calc Sin(ds.value) }}"); add("{{ calc Sinh(ds.value) }}"); add("{{ calc Sqrt(ds.value) }}"); add("{{ calc Tan(ds.value) }}"); add("{{ calc Tanh(ds.value) }}");
// 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 math functions " + "that can be used with the 'calc' report templates feature. " + "Please see this sample source code for full details.", InsertLocation.Start); paras.Insert("Report templates: available calc math 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); } } }}