//// This code is part of Document Solutions for Word demos.// Copyright (c) MESCIUS inc. All rights reserved.//usingSystem;usingSystem.IO;usingSystem.Drawing;usingSystem.Collections.Generic;usingSystem.Text.RegularExpressions;usingGrapeCity.Documents.Word;usingRange = GrapeCity.Documents.Word.Range; namespaceDsWordWeb.Demos{// NOTE: this sample is obsolete, use the RangeBase.Replace() // extension method instead (see ReplaceTextFmt).//// This sample loads an existing document, finds all occurrences// of a certain string in it, and replaces that string with another one,// also changing the character format of the replacement string. // This sample is similar to ReplaceText, with the addition of// formatting change, and has the same limitation - it only finds// occurrences of the search string that are completely within a single run. // See ReplaceTextFmt2 for an example of using PersistentRange in the// same scenario.publicclassReplaceTextFmtOld {publicGcWordDocumentCreateDocx() {// The document to replace text in:var path = Path.Combine("Resources", "WordDocs", "JsFrameworkExcerpt.docx");// The text to find:conststringtFind = "javascript";// The replacement:conststringtRepl = "ArabicaScroll"; var doc = newGcWordDocument(); doc.Load(path); var runs = doc.Body.Runs;List<Range> runRanges = newList<Range>(runs.Count);foreach (var run in runs) runRanges.Add(run.GetRange()); foreach (var rr in runRanges) {var str = rr.Text;var matches = Regex.Matches(str, tFind, RegexOptions.IgnoreCase);if (matches.Count == 0)continue; var color = rr.ParentRun.Font.Color.RGB; rr.Clear();var r = rr.Runs.Last;var pos = 0;foreach (Match m in matches) { r = r.GetRange().Runs.Insert(str.Substring(pos, m.Index - pos), InsertLocation.After); r.Font.Color.RGB = color; r = r.GetRange().Runs.Insert(tRepl, InsertLocation.After); r.Font.Color.RGB = Color.Red; pos = m.Index + m.Length; } r = r.GetRange().Runs.Insert(str.Substring(pos), InsertLocation.After); r.Font.Color.RGB = color;if (!string.IsNullOrEmpty(rr.Runs.First.GetRange().Text))thrownewException("Unexpected"); rr.Runs.First.Delete(); }// Not strictky necessary but a good practice: runRanges.Clear(); // Add a note at the end of the document: doc.Body.Sections.Last.GetRange().Paragraphs.Add($"DsWord replaced '{tFind}' with '{tRepl}' on {Util.TimeNow():R}."); // Done:return doc; } }}