BoldItalicEmulation.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. // Sample shows how to control bold and/or italic emulation when using normal fonts.
  16. public class BoldItalicEmulation
  17. {
  18. public int CreatePDF(Stream stream)
  19. {
  20. var fc = new FontCollection();
  21. // Register the font directory for convenience:
  22. // fc.RegisterDirectory(Path.Combine("Resources", "Fonts"));
  23. // or individual fonts for more control:
  24. fc.RegisterFont(Path.Combine("Resources", "Fonts", "NotoSerif-Regular.ttf"));
  25. fc.RegisterFont(Path.Combine("Resources", "Fonts", "NotoSerif-Bold.ttf"));
  26. fc.RegisterFont(Path.Combine("Resources", "Fonts", "NotoSerif-Italic.ttf"));
  27. fc.RegisterFont(Path.Combine("Resources", "Fonts", "NotoSerif-BoldItalic.ttf"));
  28.  
  29. var doc = new GcPdfDocument();
  30. var g = doc.NewPage().Graphics;
  31. var rc = Common.Util.AddNote(
  32. "TextFormat.FontStyle enables using Bold and/or Italic emulation\n" +
  33. "on a regular (not a bold/italic) font.", doc.Pages.Last);
  34. // Text insertion point:
  35. var ip = new PointF(rc.Left, rc.Bottom + 36);
  36. var tf = new TextFormat();
  37. // We specifically get a non-bold/non-italic version of the font:
  38. tf.Font = fc.FindFamilyName("Noto Serif", false, false);
  39. tf.FontSize = 16;
  40. g.DrawString($"Regular Noto Serif font: {tf.Font.FullFontName}", tf, ip);
  41. ip.Y += 36;
  42. // Draw some strings using the same (regular) font but emulating bold and italic:
  43. tf.FontStyle = GCTEXT.FontStyle.Bold;
  44. g.DrawString($"Bold emulation using font: {tf.Font.FullFontName}", tf, ip);
  45. ip.Y += 36;
  46. tf.FontStyle = GCTEXT.FontStyle.Italic;
  47. g.DrawString($"Italic emulation using font: {tf.Font.FullFontName}", tf, ip);
  48. ip.Y += 36;
  49. tf.FontStyle = GCTEXT.FontStyle.BoldItalic;
  50. g.DrawString($"Bold+Italic emulation using font: {tf.Font.FullFontName}", tf, ip);
  51. ip.Y += 36;
  52. //
  53. // Now we render some strings using the "real" bold/italic variants of the font:
  54. tf.FontStyle = GCTEXT.FontStyle.Regular;
  55. tf.Font = fc.FindFamilyName("Noto Serif", true, false);
  56. g.DrawString($"Using real bold font: {tf.Font.FullFontName}", tf, ip);
  57. ip.Y += 36;
  58. tf.Font = fc.FindFamilyName("Noto Serif", false, true);
  59. g.DrawString($"Using real italic font: {tf.Font.FullFontName}", tf, ip);
  60. ip.Y += 36;
  61. tf.Font = fc.FindFamilyName("Noto Serif", true, true);
  62. g.DrawString($"Using real bold italic font: {tf.Font.FullFontName}", tf, ip);
  63. ip.Y += 36;
  64. // Done:
  65. doc.Save(stream);
  66. return doc.Pages.Count;
  67. }
  68. }
  69. }
  70.