InsertAtFound.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.Linq
  8. Imports GrapeCity.Documents.Word
  9.  
  10. '' This sample shows how to insert the whole body of an existing DOCX into
  11. '' another document at a location where an arbitrary search string is found.
  12. '' One interesting point in this sample is that the code ensures that the
  13. '' insert position is valid, i.e. the source content (all paragraphs of
  14. '' the source document) can be inserted at the found point. If necessary,
  15. '' the target is split so that there is a content object of a valid type
  16. '' at the point where the source is to be inserted.
  17. '' In this sample, the target document is JsFrameworkExcerpt.
  18. '' The source document inserted is SampleParagraphs, and it is inserted
  19. '' on page 2 of the source document, immediately before the string
  20. '' "software design principles".
  21. Public Class InsertAtFound
  22. Public Function CreateDocx() As GcWordDocument
  23. '' The string to find. The source document will be inserted before this string
  24. Const findPattern = "software design principles"
  25.  
  26. '' Target document where the source document will be inserted:
  27. Dim doc = New GcWordDocument()
  28. doc.Load(Path.Combine("Resources", "WordDocs", "JsFrameworkExcerpt.docx"))
  29.  
  30. '' Source document that will be inserted into the target:
  31. Dim sourceDoc = New GcWordDocument()
  32. sourceDoc.Load(Path.Combine("Resources", "WordDocs", "SampleParagraphs.docx"))
  33. '' Note: the inserted document starts with a 'Heading 1', which adds a page break.
  34. '' We change it to 'Heading 2' to avoid this so that the result is more clear:
  35. sourceDoc.Body.Paragraphs.First.Style = sourceDoc.Styles(BuiltInStyleId.Heading2)
  36.  
  37. '' Find the first occurrence of the search string:
  38. Dim findResult = doc.Body.Find(findPattern, New FindOptions(doc) With {.IgnoreCase = True}).FirstOrDefault()
  39. If findResult Is Nothing Then
  40. Throw New Exception("Unexpected: search string not found.")
  41. End If
  42.  
  43. '' Find a valid insertion point near the found string:
  44. Dim insertObject = CreateInsertPoint(findResult)
  45.  
  46. '' Copy the source document to the target at insertion point
  47. sourceDoc.Body.CopyTo(insertObject.GetRange(), InsertLocation.Before)
  48.  
  49. '' Done
  50. Return doc
  51. End Function
  52.  
  53.  
  54. '' Walk up the parent chain And determine where we are - inside body, cell Or contentcontrol.
  55. '' Return the original object if we are inside body Or cell.
  56. '' Return contentcontrol if we are inside contentcontrol.
  57. Private Shared Function GetAnchorObject(testedObject As ContentObject) As ContentObject
  58. Dim originalObject = testedObject
  59. While True
  60. If testedObject.ParentContent Is Nothing Then
  61. Return originalObject
  62. ElseIf TypeOf testedObject Is Cell Then
  63. Return originalObject
  64. ElseIf TypeOf testedObject Is ContentControl Then
  65. Return testedObject
  66. Else
  67. testedObject = testedObject.ParentContent
  68. End If
  69. End While
  70. Return Nothing
  71. End Function
  72.  
  73. '' This method assumes that testObject Is always an entity inside a paragraph.
  74. Private Shared Function GetParentParagraph(testObject As ContentObject) As Paragraph
  75. While testObject IsNot Nothing
  76. If TypeOf testObject Is Paragraph Then
  77. Return CType(testObject, Paragraph)
  78. End If
  79. testObject = testObject.ParentContent
  80. End While
  81. Throw New ArgumentException("testObject is not inside a paragraph.")
  82. End Function
  83.  
  84. Private Shared Function CreateInsertPoint(fr As FindResult) As ContentObject
  85. Dim anchorObject = GetAnchorObject(fr.Range.First())
  86. Dim foundParagraph = GetParentParagraph(anchorObject)
  87.  
  88. If fr.StartIndex > 0 AndAlso TypeOf anchorObject Is Text Then
  89. anchorObject = CType(anchorObject, Text).Split(fr.StartIndex)
  90. End If
  91. If foundParagraph Is anchorObject Then
  92. anchorObject = foundParagraph.Split(anchorObject, InsertLocation.End)
  93. Else
  94. anchorObject = foundParagraph.Split(anchorObject, InsertLocation.Before)
  95. End If
  96. Return anchorObject
  97. End Function
  98. End Class
  99.