FindAll.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.Linq;
using GrapeCity.Documents.Word;

namespace DsWordWeb.Demos
{
    // This sample shows how to find all occurrences of a string
    // in a DOCX document (see JsFrameworkExcerpt) using the
    // RangeBase.Find extension method provided by the
    // RangeBaseFindReplaceExtensions class.
    public class FindAll
    {
        public GcWordDocument CreateDocx()
        {
            // The string to find. Note that we use all lowercase,
            // as we specify the ignore case option in the find call:
            const string findPattern = "javascript framework";

            // Load the document:
            var doc = new GcWordDocument();
            doc.Load(Path.Combine("Resources", "WordDocs", "JsFrameworkExcerpt.docx"));

            // Find all occurrences of the search string, ignoring the case:
            var finds = doc.Body.Find(findPattern, new FindOptions(doc) { IgnoreCase = true });

            // Print the number of found occurrences at the top of the document:
            doc.Body.Paragraphs.Insert($"Found {finds.Count()} occurrences of '{findPattern}' in the document.\n\n", InsertLocation.Start);

            // Done:
            return doc;
        }
    }
}