SubSuperScript.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 System.Text.RegularExpressions
  10. Imports GrapeCity.Documents.Word
  11.  
  12. '' This sample shows how to render subscript And superscript text.
  13. Public Class SubSuperScript
  14. Public Function CreateDocx() As GcWordDocument
  15. Dim doc = New GcWordDocument()
  16. Dim sec = doc.Body.Sections.First
  17. Dim para = sec.GetRange().Paragraphs.Add()
  18.  
  19. '' Get a random 'lorem ipsum' paragraph:
  20. Dim text = Util.LoremIpsumPar(18, 20, 20, 20)
  21.  
  22. '' Split the paragraph into 'lorem', 'ipsum' and everything else:
  23. Const subs = "lorem"
  24. Const super = "ipsum"
  25. Dim frags = Regex.Split(text, $"({subs})|({super})")
  26.  
  27. '' Create subscript And superscript styles:
  28. Dim sSub = doc.Styles.Add("My Subscript", StyleType.Character)
  29. sSub.Font.VerticalPosition = VerticalTextPosition.Subscript
  30. Dim sSup = doc.Styles.Add("My Superscript", StyleType.Character)
  31. sSup.Font.VerticalPosition = VerticalTextPosition.Superscript
  32.  
  33. '' Add text to a TextLayout using special formats for 'lorem' and 'ipsum':
  34. For Each frag In frags
  35. If frag = subs Then
  36. para.GetRange().Runs.Add(frag, sSub)
  37. ElseIf frag = super Then
  38. para.GetRange().Runs.Add(frag, sSup)
  39. Else
  40. para.GetRange().Runs.Add(frag)
  41. End If
  42. Next
  43.  
  44. '' Done
  45. Return doc
  46. End Function
  47. End Class
  48.