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