//// This code is part of Document Solutions for Word demos.// Copyright (c) MESCIUS inc. All rights reserved.//usingSystem;usingSystem.IO;usingSystem.Drawing;usingSystem.Linq;usingGrapeCity.Documents.Word; namespaceDsWordWeb.Demos{// This sample shows how to replace all occurrences of a string // in a DOCX document (see JsFrameworkExcerpt) using the// RangeBase.Replace extension method provided by the// RangeBaseFindReplaceExtensions class. // This sample is similar to ReplaceText, with the addition of// also updating the replacement's style.publicclassReplaceTextFmt {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";// Name for the replacement's style:conststringstyleName = "my new style"; // Load the source document:var doc = newGcWordDocument(); doc.Load(path); // The new style for replaced text:var style = doc.Styles.Add(styleName, StyleType.Character); style.Font.Color.RGB = Color.Red; // Replace text and replacements' style:var nreplacements = doc.Body.Replace(tFind, tRepl,newFindReplaceOptions(doc) {IgnoreCase = true,ReplacedCallback = (args_) => args_.ReplacedRange.Runs.ToList().ForEach(r_ => r_.Style = style) }); // Add a note at the end of the document: doc.Body.Sections.Last.GetRange().Paragraphs.Add($"DsWord replaced {nreplacements} occurrences of '{tFind}' with '{tRepl}' on {Util.TimeNow():R}."); // Done:return doc; } }}