ReplaceText.cs
//
// This code is part of GrapeCity Documents for Word samples.
// Copyright (c) GrapeCity, Inc. All rights reserved.
//
using System;
using System.IO;
using System.Drawing;
using System.Text;
using System.Text.RegularExpressions;
using GrapeCity.Documents.Word;

namespace GcWordWeb.Samples
{
    // 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(
                $"GcWord replaced {nreplacements} occurrences of '{tFind}' with '{tRepl}' on {DateTime.Now}.");

            // Done:
            return doc;
        }
    }
}