RangeCopyMove.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 copy or move ranges within a document,
  14. // or between documents, using the RangeBase.CopyTo()/MoveTo() methods.
  15. //
  16. // The original SampleParagraphs.docx used in this sample can be
  17. // seen by running the SampleParagraphs sample.
  18. public class RangeCopyMove
  19. {
  20. public GcWordDocument CreateDocx()
  21. {
  22. const string p1start = "This is the first paragraph of the original document";
  23. const string p2start = "This is the second paragraph of the original document";
  24. const string p3start = "This is the third paragraph of the original document";
  25. const string p4start = "This is the fourth paragraph of the original document";
  26.  
  27. // Load the sample DOCX file (see SampleParagraphs) into a GcWordDocument instance:
  28. var srcDoc = new GcWordDocument();
  29. srcDoc.Load(Path.Combine("Resources", "WordDocs", "SampleParagraphs.docx"));
  30.  
  31. // Get the range of paragraphs to copy:
  32. var srcRng = srcDoc.Body.GetPersistentRange(srcDoc.Body.Paragraphs[2], srcDoc.Body.Paragraphs.Last);
  33.  
  34. // We use another document here to demonstrate that CopyTo()/MoveTo() methods
  35. // can be used to copy/move content between documents:
  36. var doc = new GcWordDocument();
  37. srcRng.CopyTo(doc.Body, InsertLocation.Start);
  38.  
  39. // Find individual paragraphs inside the new document to manipulate:
  40. Paragraph p1 = null, p2 = null, p3 = null, p4 = null;
  41. foreach (var p in doc.Body.Paragraphs)
  42. {
  43. var t = p.GetRange().Text;
  44. if (t.StartsWith(p1start))
  45. p1 = p;
  46. else if (t.StartsWith(p2start))
  47. p2 = p;
  48. else if (t.StartsWith(p3start))
  49. p3 = p;
  50. else if (t.StartsWith(p4start))
  51. p4 = p;
  52. }
  53. if (p1 == null || p2 == null || p3 == null || p4 == null)
  54. throw new Exception("Unexpected: could not find paragraphs.");
  55.  
  56. // Move first paragraph to the end of the document with default formatting option
  57. // (preserve formatting):
  58. p1.GetRange().MoveTo(doc.Body, InsertLocation.End);
  59.  
  60. // Move second paragraph to the end of the document, clearing formatting:
  61. p2.GetRange().MoveTo(doc.Body, InsertLocation.End, FormattingCopyStrategy.Clear);
  62.  
  63. // Copy third paragraph to the end of the document, clearing formatting:
  64. p3.GetRange().CopyTo(doc.Body, InsertLocation.End, FormattingCopyStrategy.Clear);
  65.  
  66. // Add a note at the end of the document:
  67. doc.Body.Sections.Last.GetRange().Paragraphs.Add($"Created by DsWord on {Util.TimeNow():R}.");
  68.  
  69. // Done:
  70. return doc;
  71. }
  72. }
  73. }
  74.