FontFromFile.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. '' This short sample demonstrates how a Font can be loaded from a file
  13. '' And used in your code to render text.
  14. '' The sample relies on font files Gabriola.ttf And timesbi.ttf to exist in the
  15. '' Resources/Fonts folder.
  16. ''
  17. '' NOTE 1: When Font.FromFile() Is used, the actual data Is loaded on demand,
  18. '' so that usually a Font instance will Not take too much space.
  19. '' The situation Is different for fonts created using Font.FromArray()
  20. '' And Font.FromStream() methods - in those cases the whole font Is
  21. '' immediately loaded into memory. The font will still be parsed
  22. '' only on demand, but memory consumption Is slightly higher,
  23. '' so using Font.FromFile() should generally be preferred.
  24. ''
  25. '' NOTE 2: When different Font instances (created using any of the static ctors
  26. '' mentioned above) are used to render text in a PDF, each instance will result
  27. '' in embedding a separate subset of glyphs even if the glyphs are the same,
  28. '' because DsPdf has no way of knowing that two different Font instances
  29. '' represent the same physical font. So either make sure that only one Font instance
  30. '' Is created for each physical font, Or better yet use the FontCollection class
  31. '' to add the fonts you need, And specify them via TextFormat.FontName.
  32. Public Class FontFromFile
  33. Function CreatePDF(ByVal stream As Stream) As Integer
  34. Dim gabriola = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "Gabriola.ttf"))
  35. If gabriola Is Nothing Then
  36. Throw New Exception("Could not load font Gabriola")
  37. End If
  38.  
  39. '' Now that we have our font, use it to render some text:
  40. Dim tf = New TextFormat() With {.Font = gabriola, .FontSize = 16}
  41. Dim doc = New GcPdfDocument()
  42. Dim g = doc.NewPage().Graphics
  43. g.DrawString($"Sample text drawn with font {gabriola.FontFamilyName}.", tf, New PointF(72, 72))
  44. '' We can change the font size:
  45. tf.FontSize += 4
  46. g.DrawString("The quick brown fox jumps over the lazy dog.", tf, New PointF(72, 72 * 2))
  47. '' We can force DsPdf to emulate bold or italic style with a non-bold (non-italic) font, e.g.:
  48. tf.FontStyle = GCTEXT.FontStyle.Bold
  49. g.DrawString("This line prints with the same font, using emulated bold style.", tf, New PointF(72, 72 * 3))
  50. '' But of course rather than emulated, it is much better to use real bold/italic fonts.
  51. '' So finally, get a real bold italic font and print a line with it:
  52. Dim timesbi = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "timesbi.ttf"))
  53. If timesbi Is Nothing Then
  54. Throw New Exception("Could not load font timesbi")
  55. End If
  56. tf.Font = timesbi
  57. tf.FontStyle = GCTEXT.FontStyle.Regular
  58. g.DrawString($"This line prints with {timesbi.FullFontName}.", tf, New PointF(72, 72 * 4))
  59. ''
  60. '' Done:
  61. doc.Save(stream)
  62. Return doc.Pages.Count
  63. End Function
  64. End Class
  65.