MultiFormattedText.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 GrapeCity.Documents.Pdf
  8. Imports GrapeCity.Documents.Text
  9. Imports GrapeCity.Documents.Drawing
  10.  
  11. '' Shows how to use different text formats (fonts, colors) in a single paragraph.
  12. Public Class MultiFormattedText
  13. Function CreatePDF(ByVal stream As Stream) As Integer
  14. '' Function to generate sample text quoting its formatting options:
  15. Dim makeSampleText As Func(Of TextFormat, String) =
  16. Function(ByVal tf_)
  17. Dim boldItalic = String.Empty
  18. If tf_.Font.FontBold Then
  19. boldItalic = "bold "
  20. End If
  21. If tf_.Font.FontItalic Then
  22. boldItalic += "italic "
  23. End If
  24. If boldItalic = String.Empty Then
  25. boldItalic = "normal "
  26. End If
  27. Return $"This is {boldItalic}text drawn using font '{tf_.Font.FullFontName}', font size {tf_.FontSize} points, " +
  28. $"text color {tf_.ForeColor}, background color {tf_.BackColor}. "
  29. End Function
  30.  
  31. '' Font names:
  32. Const times = "times new roman"
  33. Const arial = "arial"
  34. '' Create document and text layout:
  35. Dim doc = New GcPdfDocument()
  36. Dim page = doc.NewPage()
  37. Dim g = page.Graphics
  38. Dim tl = g.CreateTextLayout()
  39. '' Use TextLayout to layout the whole page and maintain margins:
  40. tl.MaxHeight = page.Size.Height
  41. tl.MaxWidth = page.Size.Width
  42. tl.MarginAll = 72
  43. '' Get some fonts:
  44. Dim fc = New FontCollection()
  45. fc.RegisterDirectory(Path.Combine("Resources", "Fonts"))
  46. Dim fTimes = fc.FindFamilyName(times, False, False)
  47. Dim fTimesBold = fc.FindFamilyName(times, True, False)
  48. Dim fTimesItalic = fc.FindFamilyName(times, False, True)
  49. Dim fTimesBoldItalic = fc.FindFamilyName(times, True, True)
  50. Dim fArial = fc.FindFamilyName(arial, False, False)
  51. '' Add text to TextLayout using different fonts and font sizes:
  52. Dim tf = New TextFormat() With {.Font = fTimes, .FontSize = 12}
  53. tl.Append(makeSampleText(tf), tf)
  54. tf.Font = fTimesBold
  55. tf.FontSize += 2
  56. tl.Append(makeSampleText(tf), tf)
  57. tf.Font = fTimesItalic
  58. tf.FontSize += 2
  59. tl.Append(makeSampleText(tf), tf)
  60. tf.Font = fTimesBoldItalic
  61. tf.FontSize += 2
  62. tl.Append(makeSampleText(tf), tf)
  63. tf.Font = fArial
  64. tf.FontSize += 2
  65. tl.Append(makeSampleText(tf), tf)
  66. '' Add text with different foreground and background colors:
  67. tf.Font = fTimesBold
  68. tf.ForeColor = Color.Tomato
  69. tl.Append(makeSampleText(tf), tf)
  70. tf.Font = fTimesBoldItalic
  71. tf.FontSize = 16
  72. tf.ForeColor = Color.SlateBlue
  73. tf.BackColor = Color.Orange
  74. tl.Append(makeSampleText(tf), tf)
  75. '' Finish with plain black on transparent again:
  76. tl.Append("The end.", New TextFormat() With {.Font = fTimes, .FontSize = 14})
  77. '' Layout and draw text:
  78. tl.PerformLayout(True)
  79. g.DrawTextLayout(tl, PointF.Empty)
  80. ''
  81. '' Done:
  82. doc.Save(stream)
  83. Return doc.Pages.Count
  84. End Function
  85. End Class
  86.