FindAll.cs
  1. //
  2. // This code is part of Document Solutions for Word demos.
  3. // Copyright (c) MESCIUS inc. All rights reserved.
  4. //
  5. using System;
  6. using System.IO;
  7. using System.Drawing;
  8. using System.Linq;
  9. using GrapeCity.Documents.Word;
  10.  
  11. namespace DsWordWeb.Demos
  12. {
  13. // This sample shows how to find all occurrences of a string
  14. // in a DOCX document (see JsFrameworkExcerpt) using the
  15. // RangeBase.Find extension method provided by the
  16. // RangeBaseFindReplaceExtensions class.
  17. public class FindAll
  18. {
  19. public GcWordDocument CreateDocx()
  20. {
  21. // The string to find. Note that we use all lowercase,
  22. // as we specify the ignore case option in the find call:
  23. const string findPattern = "javascript framework";
  24.  
  25. // Load the document:
  26. var doc = new GcWordDocument();
  27. doc.Load(Path.Combine("Resources", "WordDocs", "JsFrameworkExcerpt.docx"));
  28.  
  29. // Find all occurrences of the search string, ignoring the case:
  30. var finds = doc.Body.Find(findPattern, new FindOptions(doc) { IgnoreCase = true });
  31.  
  32. // Print the number of found occurrences at the top of the document:
  33. doc.Body.Paragraphs.Insert($"Found {finds.Count()} occurrences of '{findPattern}' in the document.\n\n", InsertLocation.Start);
  34.  
  35. // Done:
  36. return doc;
  37. }
  38. }
  39. }
  40.