//// This code is part of Document Solutions for Word demos.// Copyright (c) MESCIUS inc. All rights reserved.//usingSystem.Globalization;usingGrapeCity.Documents.Word; namespaceDsWordWeb.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. " +publicclassDataTplCalcText {publicGcWordDocumentCreateDocx() {constintMinLen = 5;var rnd = Util.NewRandom();intnext() {return rnd.Next(1, 5); } stringnextWord() {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 random string and numeric data:var data = new[] {new { a = nextWord(), b = nextWord(), c = nextWord(), n = next(), q = nextWord() + "\"" + nextWord()}, }; var doc = newGcWordDocument(); 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 field a", "{{ds.a}}");add("Data field b", "{{ds.b}}");add("Data field c", "{{ds.c}}");add("Data field n", "{{ds.n}}");add("Data field q", "{{ds.q}}"); // q is used to show how to handle double quotes in string functions add("Asc", "{{calc Asc(ds.a)}}");add("Concat", "{{calc Concat(ds.a, ds.b, ds.c)}}");add("Contains", "{{calc Contains(ds.a, \"s\")}}");add("Contains '\"'", "{{calc Contains(ds.q, Chr(34))}}");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("Replace \"'\"", "{{calc Replace(ds.q, Chr(34), \"-\")}}");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 and numeric values shown by the first items in the list below. " +"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; voidadd(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); } } }}