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. fc.RegisterDirectory(Path.Combine("Resources", "Fonts"));
  22. var doc = new GcPdfDocument();
  23. var g = doc.NewPage().Graphics;
  24. var rc = Common.Util.AddNote(
  25. "TextFormat.FontStyle enables using Bold and/or Italic emulation\n" +
  26. "on a regular (not a bold/italic) font.", doc.Pages.Last);
  27. // Text insertion point:
  28. var ip = new PointF(rc.Left, rc.Bottom + 36);
  29. var tf = new TextFormat();
  30. // We specifically get a non-bold/non-italic version of the font:
  31. tf.Font = fc.FindFamilyName("Times New Roman", false, false);
  32. tf.FontSize = 16;
  33. g.DrawString($"Regular Times font: {tf.Font.FullFontName}", tf, ip);
  34. ip.Y += 36;
  35. // Draw some strings using the same (regular) font but emulating bold and italic:
  36. tf.FontStyle = GCTEXT.FontStyle.Bold;
  37. g.DrawString($"Bold emulation using font: {tf.Font.FullFontName}", tf, ip);
  38. ip.Y += 36;
  39. tf.FontStyle = GCTEXT.FontStyle.Italic;
  40. g.DrawString($"Italic emulation using font: {tf.Font.FullFontName}", tf, ip);
  41. ip.Y += 36;
  42. tf.FontStyle = GCTEXT.FontStyle.BoldItalic;
  43. g.DrawString($"Bold+Italic emulation using font: {tf.Font.FullFontName}", tf, ip);
  44. ip.Y += 36;
  45. //
  46. // Now we render some strings using the "real" bold/italic variants of the font:
  47. tf.FontStyle = GCTEXT.FontStyle.Regular;
  48. tf.Font = fc.FindFamilyName("Times New Roman", true, false);
  49. g.DrawString($"Using real bold font: {tf.Font.FullFontName}", tf, ip);
  50. ip.Y += 36;
  51. tf.Font = fc.FindFamilyName("Times New Roman", false, true);
  52. g.DrawString($"Using real italic font: {tf.Font.FullFontName}", tf, ip);
  53. ip.Y += 36;
  54. tf.Font = fc.FindFamilyName("Times New Roman", true, true);
  55. g.DrawString($"Using real bold italic font: {tf.Font.FullFontName}", tf, ip);
  56. ip.Y += 36;
  57. // Done:
  58. doc.Save(stream);
  59. return doc.Pages.Count;
  60. }
  61. }
  62. }
  63.