CharacterFormatting.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. Imports GCTEXT = GrapeCity.Documents.Text
  11. Imports GCDRAW = GrapeCity.Documents.Drawing
  12.  
  13. '' Demonstrates the basics of character formatting in DsPdf.
  14. ''
  15. '' Character formatting in Document Solutions for PDF is done via GrapeCity.Documents.Text.TextFormat class.
  16. '' An instance of that class with the required formatting options
  17. '' is passed to most text rendering methods available in DsPdf (e.g. DrawString).
  18. '' Rendering text with different character formatting in the same paragraph
  19. '' is done by using TextLayout/DrawTextLayout.
  20. '' See also TextRendering, MultiFormattedText, ParagraphAlign,
  21. '' ParagraphFormatting, TextAlign.
  22. Public Class CharacterFormatting
  23. Function CreatePDF(ByVal stream As Stream) As Integer
  24. Dim doc = New GcPdfDocument()
  25. Dim page = doc.NewPage()
  26. Dim g = page.Graphics
  27. Const Inch = 72.0F, vStep = Inch / 2
  28. Dim ip = New PointF(Inch, Inch)
  29.  
  30. '' 1. The only mandatory property that must be set on a TextFormat is Font:
  31. Dim tf = New TextFormat() With {.Font = StandardFonts.Times}
  32.  
  33. g.DrawString("1. The only mandatory property that must always be set on a TextFormat is Font." +
  34. "Even FontSize is optional, and defaults to 12pts.", tf, ip)
  35. ip.Y += vStep * 2
  36.  
  37. '' 2. Standard font properties are available:
  38. tf.Underline = True
  39. tf.Strikethrough = True
  40. tf.FontSize = 10
  41. g.DrawString("2. Standard properties are available, here we turn Underline and Strikethrough on, and set FontSize to 10.", tf, ip)
  42. ip.Y += vStep
  43.  
  44. '' 3. TextFormat.FontStyle allows emulating bold and/or italic styles
  45. '' using a regular font (see also BoldItalicEmulation):
  46. tf.Underline = tf.Strikethrough = False
  47. tf.FontStyle = GCTEXT.FontStyle.BoldItalic
  48. tf.FontSize = 12
  49. g.DrawString("3. Using TextFormat.FontStyle.BoldItalic to emulate bold italic style.", tf, ip)
  50. ip.Y += vStep
  51.  
  52. '' 4. Other properties include foreground and background colors:
  53. tf.FontStyle = GCTEXT.FontStyle.Regular
  54. tf.ForeColor = Color.DarkSlateBlue
  55. tf.BackColor = Color.PaleGreen
  56. g.DrawString("4. Using TextFormat.ForeColor and TextFormat.BackColor to colorize the text.", tf, ip)
  57. ip.Y += vStep
  58.  
  59. '' 5. Different text formats may be mixed in the same paragraph.
  60. '' For that, TextLayout and GcPdfGraphics.DrawTextLayout must be used:
  61. Dim tl = g.CreateTextLayout()
  62. tl.Append("5. Different text formats can be easily mixed in the same paragraph",
  63. New TextFormat() With {.Font = StandardFonts.Times})
  64. tl.Append("when the paragraph is built with TextLayout",
  65. New TextFormat() With {.Font = StandardFonts.TimesBold, .BackColor = Color.PaleTurquoise})
  66. tl.Append("as this sample paragraph shows.",
  67. New TextFormat() With {.Font = StandardFonts.HelveticaBoldItalic, .ForeColor = Color.DarkOrange})
  68. tl.Append("Various other options are available on TextFormat, including",
  69. New TextFormat() With {.Font = StandardFonts.Times, .ForeColor = Color.DarkSlateBlue})
  70. tl.Append(" GlyphAdvanceFactor ",
  71. New TextFormat() With {.Font = StandardFonts.TimesBoldItalic, .Underline = True})
  72. tl.Append(" (spreading glyphs out ",
  73. New TextFormat() With {.Font = StandardFonts.Times, .GlyphAdvanceFactor = 1.5F, .ForeColor = Color.BlueViolet})
  74. tl.Append("or putting them closer together), ",
  75. New TextFormat() With {.Font = StandardFonts.Times, .GlyphAdvanceFactor = 0.8F, .ForeColor = Color.BlueViolet})
  76. tl.Append("TransverseOffset",
  77. New TextFormat() With {.Font = StandardFonts.TimesBoldItalic, .Underline = True})
  78. tl.Append(" (lowering the glyphs below the base line, ",
  79. New TextFormat() With {.Font = StandardFonts.Times, .TransverseOffset = -5, .ForeColor = Color.MediumVioletRed})
  80. tl.Append("or raising them above it)",
  81. New TextFormat() With {.Font = StandardFonts.Times, .TransverseOffset = 5, .ForeColor = Color.MediumVioletRed})
  82. tl.Append(" and more (for example, specific fonts' features are accessible via TextFormat.FontFeatures).",
  83. New TextFormat() With {.Font = StandardFonts.Times, .FontFeatures = New FontFeature() {New FontFeature(FeatureTag.clig)}})
  84.  
  85. '' For this sample, we just set the max width of the text layout,
  86. '' in a real app you would probably set at least the MaxHeight too:
  87. tl.MaxWidth = page.Size.Width - Inch * 2
  88. tl.PerformLayout(True)
  89. g.DrawTextLayout(tl, ip)
  90. ''
  91. '' Done:
  92. doc.Save(stream)
  93. Return doc.Pages.Count
  94. End Function
  95. End Class
  96.