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 NotoSerif-Regular.ttf and
  15. '' NotoSerif-BoldItalic.ttf to be present in the 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.  
  35. Const sample = "The quick brown fox jumps over the lazy dog."
  36. Const fnRegular = "NotoSerif-Regular.ttf"
  37. Const fnBoldItalic = "NotoSerif-BoldItalic.ttf"
  38.  
  39.  
  40. Dim fRegular = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", fnRegular))
  41. If fRegular Is Nothing Then
  42. Throw New Exception($"Could not load font {fnRegular}")
  43. End If
  44.  
  45. '' Use the loaded font to draw some text:
  46. Dim tf = New TextFormat() With {.Font = fRegular, .FontSize = 12}
  47. Dim doc = New GcPdfDocument()
  48. Dim g = doc.NewPage().Graphics
  49. g.DrawString($"Font {fRegular.FontFamilyName}, size {tf.FontSize}: {sample}", tf, New PointF(18, 72))
  50. '' We can change the font size:
  51. tf.FontSize += 2
  52. g.DrawString($"Font {fRegular.FontFamilyName}, size {tf.FontSize}: {sample}", tf, New PointF(18, 72 * 2))
  53. '' We can tell DsPdf to emulate bold and/or italic style with a regular font:
  54. tf.FontStyle = GCTEXT.FontStyle.BoldItalic
  55. g.DrawString($"Font {fRegular.FontFamilyName}, emulated Bold Italic: {sample}", tf, New PointF(18, 72 * 3))
  56. '' But of course rather than emulated, it is better to use real bold/italic fonts.
  57. '' So finally, get a real bold italic font and print a line with it:
  58. Dim fBoldItalic = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", fnBoldItalic))
  59. If fBoldItalic Is Nothing Then
  60. Throw New Exception($"Could not load font {fnBoldItalic}")
  61. End If
  62. tf.Font = fBoldItalic
  63. tf.FontStyle = GCTEXT.FontStyle.Regular
  64. g.DrawString($"Font {fBoldItalic.FontFamilyName}, real Bold Italic: {sample}", tf, New PointF(18, 72 * 4))
  65. '' Done:
  66. doc.Save(stream)
  67. Return doc.Pages.Count
  68. End Function
  69. End Class
  70.