ReplaceText.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.Text;
  9. using System.Text.RegularExpressions;
  10. using GrapeCity.Documents.Word;
  11.  
  12. namespace DsWordWeb.Demos
  13. {
  14. // This sample shows how to replace all occurrences of a string
  15. // in a DOCX document (see JsFrameworkExcerpt) using the
  16. // RangeBase.Replace extension method provided by the
  17. // RangeBaseFindReplaceExtensions class.
  18. public class ReplaceText
  19. {
  20. public GcWordDocument CreateDocx()
  21. {
  22. // The text to find:
  23. const string tFind = "javascript";
  24. // The replacement:
  25. const string tRepl = "ArabicaScroll";
  26.  
  27. // Load the original JsFrameworkExcerpt document:
  28. var doc = new GcWordDocument();
  29. doc.Load(Path.Combine("Resources", "WordDocs", "JsFrameworkExcerpt.docx"));
  30.  
  31. // Replace all occurrences of the search text in the document body
  32. // (note the option that we change from defaults):
  33. var nreplacements = doc.Body.Replace(tFind, tRepl, new FindReplaceOptions(doc) { IgnoreCase = true });
  34.  
  35. // Add a note at the end of the document:
  36. doc.Body.Sections.Last.GetRange().Paragraphs.Add(
  37. $"DsWord replaced {nreplacements} occurrences of '{tFind}' with '{tRepl}' on {Util.TimeNow():R}.");
  38.  
  39. // Done:
  40. return doc;
  41. }
  42. }
  43. }
  44.