SplitDocs.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 split a DOCX into several documents.
  11. '' To see see the original document, run the SampleParagraphs sample.
  12. Public Class SplitDocs
  13. Public Function CreateDocx() As GcWordDocument
  14. '' The document will be split before the paragraph beginning with
  15. Const p2start = "This is the second paragraph of the original document"
  16.  
  17. Dim doc = New GcWordDocument()
  18. doc.Load(Path.Combine("Resources", "WordDocs", "SampleParagraphs.docx"))
  19.  
  20. Dim rng0 As RangeBase = Nothing, rng1 As RangeBase = Nothing
  21. For Each p In doc.Body.Paragraphs
  22. If p.GetRange().Text.StartsWith(p2start) Then
  23. rng0 = doc.Body.GetPersistentRange(doc.Body.Start, p.Previous.End)
  24. rng1 = doc.Body.GetPersistentRange(p.Start, doc.Body.End)
  25. Exit For
  26. End If
  27. Next
  28.  
  29. If rng0 Is Nothing OrElse rng1 Is Nothing Then
  30. Throw New Exception("Unexpected: could not find the split point.")
  31. End If
  32.  
  33. Dim docs = doc.SplitDocument(rng0, rng1)
  34. If docs.Count() <> 2 Then
  35. Throw New Exception("Unexpected: the split should have returned 2 documents.")
  36. End If
  37.  
  38. '' Return the 2nd of the two documents (the one starting with 'p2start':
  39. Return docs.Last()
  40. End Function
  41. End Class
  42.