TextFrames.vb
  1. ''
  2. '' This code is part of Document Solutions for Word demos.
  3. '' Copyright (c) MESCIUS inc. All rights reserved.
  4. ''
  5. Imports System.IO
  6. Imports System.Drawing
  7. Imports System.Collections.Generic
  8. Imports System.Linq
  9. Imports GrapeCity.Documents.Word
  10.  
  11. '' This sample demonstrates how to add linked text frames to a DOCX,
  12. '' with text spanning the frames.
  13. Public Class TextFrames
  14. Public Function CreateDocx() As GcWordDocument
  15. Dim doc = New GcWordDocument()
  16.  
  17. doc.Load(Path.Combine("Resources", "WordDocs", "SampleParagraphs.docx"))
  18.  
  19. '' Locate the paragraphs for inserting text frames:
  20. Const p1start = "This is the first paragraph of the original document"
  21. Const p2start = "This is the second paragraph of the original document"
  22. Const p3start = "This is the third paragraph of the original document"
  23. Const p4start = "This is the fourth paragraph of the original document"
  24.  
  25. '' Find individual paragraphs inside the document:
  26. Dim p1 = Nothing, p2 = Nothing, p3 = Nothing, p4 = Nothing
  27. For Each p In doc.Body.Paragraphs
  28. Dim t = p.GetRange().Text
  29. If (t.StartsWith(p1start)) Then
  30. p1 = p
  31. ElseIf (t.StartsWith(p2start)) Then
  32. p2 = p
  33. ElseIf (t.StartsWith(p3start)) Then
  34. p3 = p
  35. ElseIf (t.StartsWith(p4start)) Then
  36. p4 = p
  37. End If
  38. Next
  39. If p1 Is Nothing OrElse p2 Is Nothing OrElse p3 Is Nothing OrElse p4 Is Nothing Then
  40. Throw New Exception("Unexpected: could not find paragraphs.")
  41. End If
  42.  
  43. '' Add new style for text in frames:
  44. Dim style = doc.Styles.Add("FrameTextStyle", StyleType.Character)
  45. style.Font.Color.RGB = Color.DarkBlue
  46. style.Font.Bold = True
  47.  
  48. '' Generate a long text that we will put into linked text frames:
  49. Dim ts = ""
  50. For i = 0 To 11
  51. ts += $"Text frame content {i}. "
  52. Next
  53.  
  54. '' NOTE: shapes (including text frames) can be added to text runs only
  55. '' (e.g. we cannot add a shape directly to a paragraph).
  56.  
  57. '' Add a text run at the end of the first paragraph:
  58. Dim r1 = p1.GetRange().Runs.Add(" Text run added to paragraph 1. The first shape is inserted inline after this. ")
  59.  
  60. '' Add a shape to the new text run:
  61. Dim s1 = r1.GetRange().Shapes.Add(96, 72)
  62.  
  63. '' Add a text frame with the generated text to the new shape:
  64. Dim tf1 = s1.AddTextFrame(CType(ts, String))
  65. tf1.Format.Margin.AllEdges = 4
  66.  
  67. For Each r In tf1.GetRange().Runs
  68. If r1 IsNot r Then
  69. r.Style = style
  70. End If
  71. Next
  72.  
  73. '' The text in the frame is rather long, and will not fit inside s1.
  74. '' Text frames can be linked so that a long text can span several frames.
  75. '' We add two linked frames, one to 2nd paragraph, and one to the first
  76. '' paragraph on the page:
  77. Dim r2 = p2.GetRange().Runs.Add(" Text run added to paragraph 2. The second shape is inserted inline after this. ")
  78. Dim shapes = r2.GetRange().Shapes
  79.  
  80. Dim s2 = r2.GetRange().Shapes.Add(192, 72)
  81. Dim ltf2 = s2.AddLinkedTextFrame(tf1)
  82.  
  83. '' Default wrap format type for new shapes is inline.
  84. '' Here we change the 3rd shape's wrap to square with absolute position
  85. '' relative to the very first paragraph in the document:
  86. Dim s3 = doc.Body.Paragraphs.First.GetRange().Runs.First.GetRange().Shapes.Add(144, 96)
  87. Dim ltf3 = s3.AddLinkedTextFrame(tf1)
  88.  
  89. s3.WrapFormat.Type = WrapType.Square
  90. s3.Position.Vertical.Type = ShapePositionType.Points
  91. s3.Position.Vertical.Offset = 0
  92. s3.Position.Horizontal.Type = ShapePositionType.Points
  93. s3.Position.Horizontal.Offset = doc.Body.Sections.First.PageSetup.ClientWidth - 144
  94.  
  95. Return doc
  96. End Function
  97. End Class
  98.