OutlinedText.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 System.Collections.Generic;
  9. using GrapeCity.Documents.Pdf;
  10. using GrapeCity.Documents.Text;
  11. using GrapeCity.Documents.Drawing;
  12. using GCTEXT = GrapeCity.Documents.Text;
  13. using GCDRAW = GrapeCity.Documents.Drawing;
  14.  
  15. namespace DsPdfWeb.Demos.Basics
  16. {
  17. // This sample shows how to render text with stroked glyph outlines,
  18. // and with glyphs filled using solid or gradient brushes.
  19. public class OutlinedText
  20. {
  21. public int CreatePDF(Stream stream)
  22. {
  23. var doc = new GcPdfDocument();
  24. var page = doc.NewPage();
  25. var g = page.Graphics;
  26.  
  27. var rc = Common.Util.AddNote(
  28. "This sample shows how to draw text with stroked glyph outlines, " +
  29. "and how to fill glyphs with solid or gradient brushes.",
  30. page);
  31.  
  32. var tl = g.CreateTextLayout();
  33.  
  34. // Set text flow and other layout properties:
  35. tl.MaxWidth = page.Size.Width;
  36. tl.MaxHeight = page.Size.Height;
  37. tl.MarginAll = 72;
  38. tl.MarginTop = rc.Bottom + 36;
  39.  
  40. var rcBack = new RectangleF(tl.MarginLeft, tl.MarginTop, tl.MaxWidth.Value - tl.MarginLeft - tl.MarginRight, tl.MaxHeight.Value - tl.MarginTop - tl.MarginBottom);
  41. g.DrawImage(GCDRAW.Image.FromFile(Path.Combine("Resources", "Images", "purples.jpg")), rcBack, null, ImageAlign.StretchImage);
  42.  
  43. var tf0 = new TextFormat()
  44. {
  45. ForeColor = Color.LemonChiffon,
  46. Hollow = true,
  47. Font = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "GOTHICB.TTF")),
  48. FontSize = 48,
  49. };
  50. tl.AppendLine("Hollow Text", tf0);
  51.  
  52. var tf1 = new TextFormat()
  53. {
  54. StrokePen = Color.DarkMagenta,
  55. FillBrush = new GCDRAW.SolidBrush(Color.Yellow),
  56. Font = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "FoglihtenNo07.otf")),
  57. FontSize = 48,
  58. };
  59. tl.AppendLine("Outlined Text", tf1);
  60.  
  61. var grad0 = new LinearGradientBrush(Color.Red, new PointF(0, 0), Color.Blue, new PointF(1, 0));
  62. var tf2 = new TextFormat()
  63. {
  64. FillBrush = grad0,
  65. Font = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "cambriab.ttf")),
  66. FontSize = 48,
  67. };
  68. tl.AppendLine("Gradient Fill", tf2);
  69.  
  70. var grad1 = new LinearGradientBrush(Color.Red, Color.Purple);
  71. grad1.GradientStops = new List<GradientStop>();
  72. grad1.GradientStops.Add(new GradientStop(Color.Orange, 1 / 6f));
  73. grad1.GradientStops.Add(new GradientStop(Color.Yellow, 2 / 6f));
  74. grad1.GradientStops.Add(new GradientStop(Color.Green, 3 / 6f));
  75. grad1.GradientStops.Add(new GradientStop(Color.Cyan, 4/ 6f));
  76. grad1.GradientStops.Add(new GradientStop(Color.Blue, 5 / 6f));
  77. var tf3 = new TextFormat()
  78. {
  79. FillBrush = grad1,
  80. Font = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "cambriab.ttf")),
  81. FontSize = 48,
  82. };
  83. tl.AppendLine("Multi-stop gradient", tf3);
  84.  
  85. var tf4 = new TextFormat(tf3);
  86. tf4.StrokePen = Color.GreenYellow;
  87. tl.AppendLine("Multi-stop gradient with outline", tf4);
  88.  
  89. var tf5 = new TextFormat(tf4);
  90. tf5.Hollow = true;
  91. tf5.StrokePen = new GCDRAW.Pen(Color.DarkRed, 1);
  92. tl.AppendLine("Text outlined with 1pt wide pen", tf5);
  93.  
  94. // It is not necessary to call PerformLayout() for a newly created TextLayout
  95. // or after a call to TextLayout.Clear():
  96. g.DrawTextLayout(tl, PointF.Empty);
  97.  
  98. // Done:
  99. doc.Save(stream);
  100. return doc.Pages.Count;
  101. }
  102. }
  103. }
  104.