SubSuperScript.vb
  1. ''
  2. '' This code is part of Document Solutions for PDF demos.
  3. '' Copyright (c) MESCIUS inc. All rights reserved.
  4. ''
  5. Imports System.IO
  6. Imports System.Drawing
  7. Imports System.Text.RegularExpressions
  8. Imports GrapeCity.Documents.Drawing
  9. Imports GrapeCity.Documents.Pdf
  10. Imports GrapeCity.Documents.Text
  11. Imports GCTEXT = GrapeCity.Documents.Text
  12. Imports GCDRAW = GrapeCity.Documents.Drawing
  13.  
  14. '' This sample shows how to render subscript And superscript text.
  15. Public Class SubSuperScript
  16. Sub CreatePDF(ByVal stream As Stream)
  17. Dim doc = New GcPdfDocument()
  18. Dim page = doc.NewPage()
  19. Dim g = page.Graphics
  20.  
  21. Dim rc = Util.AddNote(
  22. "Demo of the TextFormat.Subscript and TextFormat.Superscript properties. " +
  23. "We draw a random 'lorem ipsum' paragraph, rendering all instances of 'lorem' as subscript, " +
  24. "and all instances of 'ipsum' as superscript.",
  25. page)
  26.  
  27. '' Get a random 'lorem ipsum' paragraph:
  28. Dim para = Util.LoremIpsum(1, 18, 20, 20, 20)
  29.  
  30. '' Split the paragraph into 'lorem', 'ipsum' and everything else:
  31. Const subs = "lorem"
  32. Const super = "ipsum"
  33. Dim frags = Regex.Split(para, $"({subs})|({super})")
  34.  
  35. Dim fnt = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "times.ttf"))
  36.  
  37. '' Create text formats for subscript And superscript:
  38. Dim tfSub = New TextFormat() With {.Font = fnt, .FontSize = 12, .Subscript = True}
  39. Dim tfSuper = New TextFormat(tfSub) With {.Subscript = False, .Superscript = True}
  40.  
  41. '' Add text to a TextLayout using special formats for 'lorem' and 'ipsum':
  42. Dim tl = g.CreateTextLayout()
  43. tl.DefaultFormat.Font = fnt
  44. tl.DefaultFormat.FontSize = 12
  45. For Each frag In frags
  46. If (frag = subs) Then
  47. tl.Append(frag, tfSub)
  48. ElseIf (frag = super) Then
  49. tl.Append(frag, tfSuper)
  50. Else
  51. tl.Append(frag)
  52. End If
  53. Next
  54.  
  55. '' Set layout properties And render the text
  56. tl.MaxWidth = page.Size.Width
  57. tl.MaxHeight = page.Size.Height - rc.Height
  58. tl.MarginAll = 72
  59. tl.MarginTop = rc.Bottom + 36
  60. tl.PerformLayout(True)
  61. g.DrawTextLayout(tl, PointF.Empty)
  62.  
  63. '' Done
  64. doc.Save(stream)
  65. End Sub
  66. End Class
  67.