OutlinedText.cs
// 完毕:
using System;
using System.IO;
using System.Drawing;
using System.Collections.Generic;
using GrapeCity.Documents.Pdf;
using GrapeCity.Documents.Text;
using GrapeCity.Documents.Drawing;
using GCTEXT = GrapeCity.Documents.Text;
using GCDRAW = GrapeCity.Documents.Drawing;

namespace DsPdfWeb.Demos.Basics
{
    // 此示例展示了如何使用描边字形轮廓渲染文本,
    // 并使用实心或渐变画笔填充字形。
    public class OutlinedText
    {
        public int CreatePDF(Stream stream)
        {
            var doc = new GcPdfDocument();
            var page = doc.NewPage();
            var g = page.Graphics;

            var rc = Common.Util.AddNote(
                "此示例演示了如何使用描边字形轮廓绘制文本," +
                "",
                page);

            var tl = g.CreateTextLayout();

            // 设置文本流和其他布局属性:
            tl.MaxWidth = page.Size.Width;
            tl.MaxHeight = page.Size.Height;
            tl.MarginAll = 72;
            tl.MarginTop = rc.Bottom + 36;

            var rcBack = new RectangleF(tl.MarginLeft, tl.MarginTop, tl.MaxWidth.Value - tl.MarginLeft - tl.MarginRight, tl.MaxHeight.Value - tl.MarginTop - tl.MarginBottom);
            g.DrawImage(GCDRAW.Image.FromFile(Path.Combine("Resources", "Images", "purples.jpg")), rcBack, null, ImageAlign.StretchImage);

            var tf0 = new TextFormat()
            {
                ForeColor = Color.LemonChiffon,
                Hollow = true,
                Font = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "GOTHICB.TTF")),
                FontSize = 48,
            };
            tl.AppendLine("Hollow Text", tf0);

            var tf1 = new TextFormat()
            {
                StrokePen = Color.DarkMagenta,
                FillBrush = new GCDRAW.SolidBrush(Color.Yellow),
                Font = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "FoglihtenNo07.otf")),
                FontSize = 48,
            };
            tl.AppendLine("Outlined Text", tf1);

            var grad0 = new LinearGradientBrush(Color.Red, new PointF(0, 0), Color.Blue, new PointF(1, 0));
            var tf2 = new TextFormat()
            {
                FillBrush = grad0,
                Font = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "cambriab.ttf")),
                FontSize = 48,
            };
            tl.AppendLine("Gradient Fill", tf2);

            var grad1 = new LinearGradientBrush(Color.Red, Color.Purple);
            grad1.GradientStops = new List<GradientStop>();
            grad1.GradientStops.Add(new GradientStop(Color.Orange, 1 / 6f));
            grad1.GradientStops.Add(new GradientStop(Color.Yellow, 2 / 6f));
            grad1.GradientStops.Add(new GradientStop(Color.Green, 3 / 6f));
            grad1.GradientStops.Add(new GradientStop(Color.Cyan, 4/ 6f));
            grad1.GradientStops.Add(new GradientStop(Color.Blue, 5 / 6f));
            var tf3 = new TextFormat()
            {
                FillBrush = grad1,
                Font = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "cambriab.ttf")),
                FontSize = 48,
            };
            tl.AppendLine("Multi-stop gradient", tf3);

            var tf4 = new TextFormat(tf3);
            tf4.StrokePen = Color.GreenYellow;
            tl.AppendLine("Multi-stop gradient with outline", tf4);

            var tf5 = new TextFormat(tf4);
            tf5.Hollow = true;
            tf5.StrokePen = new GCDRAW.Pen(Color.DarkRed, 1);
            tl.AppendLine("Text outlined with 1pt wide pen", tf5);

            // 没有必要为新创建的 TextLayout 调用 PerformLayout()
            // 或者在调用 TextLayout.Clear() 之后:
            g.DrawTextLayout(tl, PointF.Empty);

            // 完毕:
            doc.Save(stream);
            return doc.Pages.Count;
        }
    }
}