TextFrames.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.Collections.Generic;
  9. using System.Linq;
  10. using GrapeCity.Documents.Word;
  11.  
  12. namespace DsWordWeb.Demos
  13. {
  14. // This sample demonstrates how to add linked text frames to a DOCX,
  15. // with text spanning the frames.
  16. public class TextFrames
  17. {
  18. public GcWordDocument CreateDocx()
  19. {
  20. GcWordDocument doc = new GcWordDocument();
  21.  
  22. doc.Load(Path.Combine("Resources", "WordDocs", "SampleParagraphs.docx"));
  23.  
  24. // Locate the paragraphs for inserting text frames:
  25. const string p1start = "This is the first paragraph of the original document";
  26. const string p2start = "This is the second paragraph of the original document";
  27. const string p3start = "This is the third paragraph of the original document";
  28. const string p4start = "This is the fourth paragraph of the original document";
  29.  
  30. // Find individual paragraphs inside the document:
  31. Paragraph p1 = null, p2 = null, p3 = null, p4 = null;
  32. foreach (var p in doc.Body.Paragraphs)
  33. {
  34. var t = p.GetRange().Text;
  35. if (t.StartsWith(p1start))
  36. p1 = p;
  37. else if (t.StartsWith(p2start))
  38. p2 = p;
  39. else if (t.StartsWith(p3start))
  40. p3 = p;
  41. else if (t.StartsWith(p4start))
  42. p4 = p;
  43. }
  44. if (p1 == null || p2 == null || p3 == null || p4 == null)
  45. throw new Exception("Unexpected: could not find paragraphs.");
  46.  
  47. // Add new style for text in frames:
  48. var style = doc.Styles.Add("FrameTextStyle", StyleType.Character);
  49. style.Font.Color.RGB = Color.DarkBlue;
  50. style.Font.Bold = true;
  51.  
  52. // Generate a long text that we will put into linked text frames:
  53. string ts = "";
  54. for (int i = 0; i < 12; ++i)
  55. ts += $"Text frame content {i}. ";
  56.  
  57. // NOTE: shapes (including text frames) can be added to text runs only
  58. // (e.g. we cannot add a shape directly to a paragraph).
  59.  
  60. // Add a text run at the end of the first paragraph:
  61. Run r1 = p1.GetRange().Runs.Add(" Text run added to paragraph 1. The first shape is inserted inline after this. ");
  62.  
  63. // Add a shape to the new text run:
  64. Shape s1 = r1.GetRange().Shapes.Add(96, 72);
  65.  
  66. // Add a text frame with the generated text to the new shape:
  67. TextFrame tf1 = s1.AddTextFrame(ts);
  68. tf1.Format.Margin.AllEdges = 4;
  69.  
  70. foreach (var r in tf1.GetRange().Runs)
  71. if (r1 != r)
  72. r.Style = style;
  73.  
  74. // The text in the frame is rather long, and will not fit inside s1.
  75. // Text frames can be linked so that a long text can span several frames.
  76. // We add two linked frames, one to 2nd paragraph, and one to the first
  77. // paragraph on the page:
  78. var r2 = p2.GetRange().Runs.Add(" Text run added to paragraph 2. The second shape is inserted inline after this. ");
  79. var shapes = r2.GetRange().Shapes;
  80.  
  81. var s2 = shapes.Add(192, 72);
  82. var ltf2 = s2.AddLinkedTextFrame(tf1);
  83.  
  84. // Default wrap format type for new shapes is inline.
  85. // Here we change the 3rd shape's wrap to square with absolute position
  86. // relative to the very first paragraph in the document:
  87. var s3 = doc.Body.Paragraphs.First.GetRange().Runs.First.GetRange().Shapes.Add(144, 96);
  88. var ltf3 = s3.AddLinkedTextFrame(tf1);
  89.  
  90. s3.WrapFormat.Type = WrapType.Square;
  91. s3.Position.Vertical.Type = ShapePositionType.Points;
  92. s3.Position.Vertical.Offset = 0;
  93. s3.Position.Horizontal.Type = ShapePositionType.Points;
  94. s3.Position.Horizontal.Offset = doc.Body.Sections.First.PageSetup.ClientWidth - 144;
  95.  
  96. return doc;
  97. }
  98. }
  99. }
  100.