//// 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; 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 almost identical to ReplaceTextFmt// but uses PersistentRange instead of Range.// Its primary purpose is to illustrate the differences between// Range and PersistentRange.publicclassReplaceTextFmt2 {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<PersistentRange> runRanges = newList<PersistentRange>(runs.Count);foreach (var run in runs) runRanges.Add(run.GetPersistentRange()); 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 pos = 0;foreach (Match m in matches) { rr.Runs.Add(str.Substring(pos, m.Index - pos)).Font.Color.RGB = color; rr.Runs.Add(tRepl).Font.Color.RGB = Color.Red; pos = m.Index + m.Length; } rr.Runs.Add(str.Substring(pos)).Font.Color.RGB = color; if (!string.IsNullOrEmpty(rr.Runs.First.GetRange().Text))thrownewException("Unexpected"); rr.Runs.First.Delete(); // PersistentRange is kept up to date when the document changes,// so it should be disposed when no longer needed to improve// performance and reduce memory consumption: rr.Dispose(); }// 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; } }}