FontFromFile.cs
  1. //
  2. // This code is part of Document Solutions for PDF demos.
  3. // Copyright (c) MESCIUS inc. All rights reserved.
  4. //
  5. using System;
  6. using System.IO;
  7. using System.Drawing;
  8. using GrapeCity.Documents.Pdf;
  9. using GrapeCity.Documents.Text;
  10. using GCTEXT = GrapeCity.Documents.Text;
  11. using GCDRAW = GrapeCity.Documents.Drawing;
  12.  
  13. namespace DsPdfWeb.Demos.Basics
  14. {
  15. // This short sample demonstrates how a Font can be loaded from a file
  16. // and used in your code to render text.
  17. // The sample relies on font files Gabriola.ttf and timesbi.ttf to exist in the
  18. // Resources/Fonts folder.
  19. //
  20. // NOTE 1: When Font.FromFile() is used, the actual data is loaded on demand,
  21. // so that usually a Font instance will not take too much space.
  22. // The situation is different for fonts created using Font.FromArray()
  23. // and Font.FromStream() methods - in those cases the whole font is
  24. // immediately loaded into memory. The font will still be parsed
  25. // only on demand, but memory consumption is slightly higher,
  26. // so using Font.FromFile() should generally be preferred.
  27. //
  28. // NOTE 2: When different Font instances (created using any of the static ctors
  29. // mentioned above) are used to render text in a PDF, each instance will result
  30. // in embedding a separate subset of glyphs even if the glyphs are the same,
  31. // because DsPdf has no way of knowing that two different Font instances
  32. // represent the same physical font. So either make sure that only one Font instance
  33. // is created for each physical font, or better yet use the FontCollection class
  34. // to add the fonts you need, and specify them via TextFormat.FontName.
  35. public class FontFromFile
  36. {
  37. public int CreatePDF(Stream stream)
  38. {
  39. var gabriola = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "Gabriola.ttf"));
  40. if (gabriola == null)
  41. throw new Exception("Could not load font Gabriola");
  42.  
  43. // Now that we have our font, use it to render some text:
  44. var tf = new TextFormat() { Font = gabriola, FontSize = 16 };
  45. var doc = new GcPdfDocument();
  46. var g = doc.NewPage().Graphics;
  47. g.DrawString($"Sample text drawn with font {gabriola.FontFamilyName}.", tf, new PointF(72, 72));
  48. // We can change the font size:
  49. tf.FontSize += 4;
  50. g.DrawString("The quick brown fox jumps over the lazy dog.", tf, new PointF(72, 72 * 2));
  51. // We can force DsPdf to emulate bold or italic style with a non-bold (non-italic) font, e.g.:
  52. tf.FontStyle = GCTEXT.FontStyle.Bold;
  53. g.DrawString("This line prints with the same font, using emulated bold style.", tf, new PointF(72, 72 * 3));
  54. // But of course rather than emulated, it is much better to use real bold/italic fonts.
  55. // So finally, get a real bold italic font and print a line with it:
  56. var timesbi = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "timesbi.ttf"));
  57. tf.Font = timesbi ?? throw new Exception("Could not load font timesbi");
  58. tf.FontStyle = GCTEXT.FontStyle.Regular;
  59. g.DrawString($"This line prints with {timesbi.FullFontName}.", tf, new PointF(72, 72 * 4));
  60. // Done:
  61. doc.Save(stream);
  62. return doc.Pages.Count;
  63. }
  64. }
  65. }
  66.