CharacterStyles.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.Drawing
  6. Imports GrapeCity.Documents.Word
  7.  
  8. '' This sample demonstrates how to create, specify and apply
  9. '' character styles.
  10. '' It creates a paragraph of text, splits it into two runs,
  11. '' then creates And applies two different character styles
  12. '' to the two runs.
  13. '' The part creating And splitting the paragraph Is based
  14. '' on the FormatChars sample.
  15. Public Class CharacterStyles
  16. Function CreateDocx() As GcWordDocument
  17. '' Get a sample paragraph of text:
  18. Dim lorem = LoremIpsumPar()
  19. '' The code below is similar to FormatChars code that creates and splits a paragraph:
  20. Dim doc = New GcWordDocument()
  21. Dim p = doc.Body.Paragraphs.Add(lorem)
  22. Dim r = p.GetRange()
  23. Dim run = r.Runs.First
  24. Dim text = run.GetRange().Texts.First
  25. Dim tIpsum = text.Split(lorem.Length / 2)
  26. Dim rIpsum = run.Split(tIpsum, InsertLocation.Before)
  27. '' We now have two runs, create and apply different styles to each:
  28. ''
  29. '' Create a new char style "Lorem" for the first half:
  30. Dim sLorem = doc.Styles.Add("Lorem", StyleType.Character)
  31. sLorem.Font.Name = "Times New Roman"
  32. sLorem.Font.Size = 16
  33. sLorem.Font.Bold = True
  34. sLorem.Font.Italic = True
  35. sLorem.Font.Underline = Underline.Thick
  36. '' 'text' was split into 2 halves, but the head can be still
  37. '' accessed via the 'text' variable. We set its style to the
  38. '' newly created style (the style of the 2nd half remains default):
  39. text.ParentRun.Style = sLorem
  40. '' Create a new char style "Ipsum" for the 2nd half:
  41. Dim sIpsum = doc.Styles.Add("Ipsum", StyleType.Character)
  42. sIpsum.Font.Name = "Gabriola"
  43. sIpsum.Font.Size = 18
  44. sIpsum.Font.Color.RGB = Color.BlueViolet
  45. rIpsum.Style = sIpsum
  46. '' Done:
  47. Return doc
  48. End Function
  49. End Class
  50.