BoldItalicEmulation.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 GCTEXT = GrapeCity.Documents.Text
  10. Imports GCDRAW = GrapeCity.Documents.Drawing
  11.  
  12. '' Sample shows how to control bold and/or italic emulation when using normal fonts.
  13. Public Class BoldItalicEmulation
  14. Function CreatePDF(ByVal stream As Stream) As Integer
  15. Dim fc = New FontCollection()
  16.  
  17. '' Register the font directory for convenience:
  18. '' fc.RegisterDirectory(Path.Combine("Resources", "Fonts"))
  19. '' Or individual fonts for more control:
  20. fc.RegisterFont(Path.Combine("Resources", "Fonts", "NotoSerif-Regular.ttf"))
  21. fc.RegisterFont(Path.Combine("Resources", "Fonts", "NotoSerif-Bold.ttf"))
  22. fc.RegisterFont(Path.Combine("Resources", "Fonts", "NotoSerif-Italic.ttf"))
  23. fc.RegisterFont(Path.Combine("Resources", "Fonts", "NotoSerif-BoldItalic.ttf"))
  24.  
  25. Dim doc = New GcPdfDocument()
  26. Dim g = doc.NewPage().Graphics
  27. Dim rc = Util.AddNote(
  28. "TextFormat.FontStyle enables using Bold and/or Italic emulation" + vbLf +
  29. "on a regular (not a bold/italic) font.", doc.Pages.Last)
  30. '' Text insertion point:
  31. Dim ip = New PointF(rc.Left, rc.Bottom + 36)
  32. Dim tf = New TextFormat()
  33. '' We specifically get a non-bold/non-italic version of the font:
  34. tf.Font = fc.FindFamilyName("Noto Serif", False, False)
  35. tf.FontSize = 16
  36. g.DrawString($"Regular Noto Serif font: {tf.Font.FullFontName}", tf, ip)
  37. ip.Y += 36
  38. '' Draw some strings using the same (regular) font but emulating bold and italic:
  39. tf.FontStyle = GCTEXT.FontStyle.Bold
  40. g.DrawString($"Bold emulation using font: {tf.Font.FullFontName}", tf, ip)
  41. ip.Y += 36
  42. tf.FontStyle = GCTEXT.FontStyle.Italic
  43. g.DrawString($"Italic emulation using font: {tf.Font.FullFontName}", tf, ip)
  44. ip.Y += 36
  45. tf.FontStyle = GCTEXT.FontStyle.BoldItalic
  46. g.DrawString($"Bold+Italic emulation using font: {tf.Font.FullFontName}", tf, ip)
  47. ip.Y += 36
  48. ''
  49. '' Now we render some strings using the "real" bold/italic variants of the font:
  50. tf.FontStyle = GCTEXT.FontStyle.Regular
  51. tf.Font = fc.FindFamilyName("Noto Serif", True, False)
  52. g.DrawString($"Using real bold font: {tf.Font.FullFontName}", tf, ip)
  53. ip.Y += 36
  54. tf.Font = fc.FindFamilyName("Noto Serif", False, True)
  55. g.DrawString($"Using real italic font: {tf.Font.FullFontName}", tf, ip)
  56. ip.Y += 36
  57. tf.Font = fc.FindFamilyName("Noto Serif", True, True)
  58. g.DrawString($"Using real bold italic font: {tf.Font.FullFontName}", tf, ip)
  59. ip.Y += 36
  60. ''
  61. '' Done:
  62. doc.Save(stream)
  63. Return doc.Pages.Count
  64. End Function
  65. End Class
  66.