Using different text formats in a single paragraph

PDF TIFF SVG JPG C# VB
MultiFormattedText.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.Drawing;
  9. using GrapeCity.Documents.Pdf;
  10. using GrapeCity.Documents.Text;
  11.  
  12. namespace DsPdfWeb.Demos.Basics
  13. {
  14. // This sample shows how to use different text formats
  15. // (fonts, colors) in a single paragraph.
  16. public class MultiFormattedText
  17. {
  18. public int CreatePDF(Stream stream)
  19. {
  20. // Function to generate sample text quoting its formatting options:
  21. Func<TextFormat, string> makeSampleText = (tf_) =>
  22. {
  23. string boldItalic = string.Empty;
  24. if (tf_.Font.FontBold)
  25. boldItalic = "bold ";
  26. if (tf_.Font.FontItalic)
  27. boldItalic += "italic ";
  28. if (boldItalic == string.Empty)
  29. boldItalic = "normal ";
  30. return $"This is {boldItalic}text drawn using font '{tf_.Font.FullFontName}', font size {tf_.FontSize} points, " +
  31. $"text color {tf_.ForeColor}, background color {tf_.BackColor}. ";
  32. };
  33. // Font names:
  34. const string times = "times new roman";
  35. const string arial = "arial";
  36. // Create document and text layout:
  37. var doc = new GcPdfDocument();
  38. var page = doc.NewPage();
  39. var g = page.Graphics;
  40. var tl = g.CreateTextLayout();
  41. // Use TextLayout to layout the whole page and maintain margins:
  42. tl.MaxHeight = page.Size.Height;
  43. tl.MaxWidth = page.Size.Width;
  44. tl.MarginAll = 72;
  45. // Get some fonts:
  46. var fc = new FontCollection();
  47. fc.RegisterDirectory(Path.Combine("Resources", "Fonts"));
  48. var fTimes = fc.FindFamilyName(times, false, false);
  49. var fTimesBold = fc.FindFamilyName(times, true, false);
  50. var fTimesItalic = fc.FindFamilyName(times, false, true);
  51. var fTimesBoldItalic = fc.FindFamilyName(times, true, true);
  52. var fArial = fc.FindFamilyName(arial, false, false);
  53. // Add text to TextLayout using different fonts and font sizes:
  54. var tf = new TextFormat() { Font = fTimes, FontSize = 12, };
  55. tl.Append(makeSampleText(tf), tf);
  56. tf.Font = fTimesBold;
  57. tf.FontSize += 2;
  58. tl.Append(makeSampleText(tf), tf);
  59. tf.Font = fTimesItalic;
  60. tf.FontSize += 2;
  61. tl.Append(makeSampleText(tf), tf);
  62. tf.Font = fTimesBoldItalic;
  63. tf.FontSize += 2;
  64. tl.Append(makeSampleText(tf), tf);
  65. tf.Font = fArial;
  66. tf.FontSize += 2;
  67. tl.Append(makeSampleText(tf), tf);
  68. // Add text with different foreground and background colors:
  69. tf.Font = fTimesBold;
  70. tf.ForeColor = Color.Tomato;
  71. tl.Append(makeSampleText(tf), tf);
  72. tf.Font = fTimesBoldItalic;
  73. tf.FontSize = 16;
  74. tf.ForeColor = Color.SlateBlue;
  75. tf.BackColor = Color.Orange;
  76. tl.Append(makeSampleText(tf), tf);
  77. // Finish with plain black on transparent again:
  78. tl.Append("The end.", new TextFormat() { Font = fTimes, FontSize = 14, });
  79. // Layout and draw text:
  80. tl.PerformLayout(true);
  81. g.DrawTextLayout(tl, PointF.Empty);
  82. // Done:
  83. doc.Save(stream);
  84. return doc.Pages.Count;
  85. }
  86. }
  87. }
  88.