ReplaceText.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.Text;
- using System.Text.RegularExpressions;
- using GrapeCity.Documents.Word;
-
- namespace DsWordWeb.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.
- public class ReplaceText
- {
- public GcWordDocument CreateDocx()
- {
- // The text to find:
- const string tFind = "javascript";
- // The replacement:
- const string tRepl = "ArabicaScroll";
-
- // Load the original JsFrameworkExcerpt document:
- var doc = new GcWordDocument();
- doc.Load(Path.Combine("Resources", "WordDocs", "JsFrameworkExcerpt.docx"));
-
- // Replace all occurrences of the search text in the document body
- // (note the option that we change from defaults):
- var nreplacements = doc.Body.Replace(tFind, tRepl, new FindReplaceOptions(doc) { IgnoreCase = true });
-
- // 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;
- }
- }
- }
-