DataTplCalcText.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 text functions " + // that can be used with the 'calc' report templates feature " + // to perform various operations on strings." + // For example, to insert the concatenation of three text data fields, " + // the template '{{calc Concat(ds.a, ds.b, ds.c)}}' can be used. " + // The data source used in this demo contains a single record " + // with random string values. " + public class DataTplCalcText { public GcWordDocument CreateDocx() { const int MinLen = 5; var rnd = Util.NewRandom(); int next() { return rnd.Next(1, 5); } string nextWord() { for (int i = 0; i < 50; ++i) { var t = Util.LoremIpsumWord(); if (t.Length >= MinLen) return t; } return "lorem"; } // Generate a simple data source with pairs of random numeric data: var data = new[] { new { a = nextWord(), b = nextWord(), c = nextWord(), n = next(), }, }; var doc = new GcWordDocument(); doc.Body.Sections.First.PageSetup.Margin.Top = 36; doc.Body.Sections.First.PageSetup.Margin.Bottom = 36; doc.Body.Sections.First.PageSetup.Margin.Left = 36; doc.Body.Sections.First.PageSetup.Margin.Right = 36; var paras = doc.Body.Paragraphs; // 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]; exStyle.ParagraphFormat.TabStops.Add(110); var resStyle = doc.Styles[BuiltInStyleId.Strong]; add("Data", "{{ds.a}}"); add("Data", "{{ds.b}}"); add("Data", "{{ds.c}}"); add("Data", "{{ds.n}}"); add("Asc", "{{calc Asc(ds.a)}}"); add("Concat", "{{calc Concat(ds.a, ds.b, ds.c)}}"); add("Contains", "{{calc Contains(ds.a, \"s\")}}"); add("EndsWith", "{{calc EndsWith(ds.a, \"s\")}}"); add("Insert", "{{calc Insert(ds.a, ds.n, ds.b)}}"); add("InStr", "{{calc InStr(ds.a, \"s\")}}"); add("InStr", "{{calc InStr(ds.a, \"s\", ds.n)}}"); add("LCase", "{{calc LCase(\"Lorem Ipsum!\")}}"); add("Len", "{{calc Len(ds.a)}}"); add("LSet", "{{calc LSet(ds.a, 12)}}"); add("LSet", "{{calc LSet(ds.a, 12, \"!\")}}"); add("Mid", "{{calc Mid(ds.a, 2)}}"); add("Mid", "{{calc Mid(ds.a, 2, 1)}}"); add("Remove", "{{calc Remove(ds.a, 2)}}"); add("Remove", "{{calc Remove(ds.a, 2, 1)}}"); add("Replace", "{{calc Replace(\"loremipsumlobortis\", \"lorem\",\"ipsum\")}}"); add("RSet", "{{calc RSet(ds.a, 12)}}"); add("RSet", "{{calc RSet(ds.a, 12, \"!\")}}"); add("StartsWith", "{{calc StartsWith(ds.a, \"s\")}}"); add("StrReverse", "{{calc StrReverse(ds.a)}}"); add("Trim", "{{calc Trim(\" Lorem Ipsum \")}}"); add("UCase", "{{calc UCase(\"Lorem Ipsum!\")}}"); // 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 text functions " + "that can be used with the 'calc' report templates feature " + "to perform various operations on strings." + "For example, to insert the concatenation of three text data fields, " + "the template '{{calc Concat(ds.a, ds.b, ds.c)}}' can be used. " + "The data source used in this demo contains a single record " + "with random string values. " + "Please see this sample source code for full details.", InsertLocation.Start); paras.Insert("Report templates: available calc operators", doc.Styles[BuiltInStyleId.Heading1], InsertLocation.Start); // Done: return doc; void add(string caption, string expr) { // \x200B is a zero-width space used to prevent template expansion: paras.Add(caption + "\t" + expr.Insert(1, "\x200B") + " : ", exStyle).ListFormat.Template = bulletListTemplate; paras.Last.GetRange().Runs.Add(expr, resStyle); } } } }