MultiFormattedText.cs
// 完毕:
using System;
using System.IO;
using System.Drawing;
using GrapeCity.Documents.Drawing;
using GrapeCity.Documents.Pdf;
using GrapeCity.Documents.Text;

namespace DsPdfWeb.Demos.Basics
{
    // 该示例展示了如何使用不同的文本格式
    // (字体、颜色)在一个段落中。
    public class MultiFormattedText
    {
        public int CreatePDF(Stream stream)
        {
            // 生成引用其格式选项的示例文本的函数:
            Func<TextFormat, string> makeSampleText = (tf_) =>
            {
                string boldItalic = string.Empty;
                if (tf_.Font.FontBold)
                    boldItalic = "bold ";
                if (tf_.Font.FontItalic)
                    boldItalic += "italic ";
                if (boldItalic == string.Empty)
                    boldItalic = "normal ";
                return $"This is {boldItalic}text drawn using font '{tf_.Font.FullFontName}', font size {tf_.FontSize} points, " +
                    $"text color {tf_.ForeColor}, background color {tf_.BackColor}. ";
            };
            // 字体名称:
            const string times = "times new roman";
            const string arial = "arial";
            // 创建文档和文本布局:
            var doc = new GcPdfDocument();
            var page = doc.NewPage();
            var g = page.Graphics;
            var tl = g.CreateTextLayout();
            // 使用 TextLayout 布局整个页面并保持边距:
            tl.MaxHeight = page.Size.Height;
            tl.MaxWidth = page.Size.Width;
            tl.MarginAll = 72;
            // 获取一些字体:
            var fc = new FontCollection();
            fc.RegisterDirectory(Path.Combine("Resources", "Fonts"));
            var fTimes = fc.FindFamilyName(times, false, false);
            var fTimesBold = fc.FindFamilyName(times, true, false);
            var fTimesItalic = fc.FindFamilyName(times, false, true);
            var fTimesBoldItalic = fc.FindFamilyName(times, true, true);
            var fArial = fc.FindFamilyName(arial, false, false);
            // 使用不同的字体和字体大小将文本添加到 TextLayout:
            var tf = new TextFormat() { Font = fTimes, FontSize = 12, };
            tl.Append(makeSampleText(tf), tf);
            tf.Font = fTimesBold;
            tf.FontSize += 2;
            tl.Append(makeSampleText(tf), tf);
            tf.Font = fTimesItalic;
            tf.FontSize += 2;
            tl.Append(makeSampleText(tf), tf);
            tf.Font = fTimesBoldItalic;
            tf.FontSize += 2;
            tl.Append(makeSampleText(tf), tf);
            tf.Font = fArial;
            tf.FontSize += 2;
            tl.Append(makeSampleText(tf), tf);
            // 添加具有不同前景色和背景色的文本:
            tf.Font = fTimesBold;
            tf.ForeColor = Color.Tomato;
            tl.Append(makeSampleText(tf), tf);
            tf.Font = fTimesBoldItalic;
            tf.FontSize = 16;
            tf.ForeColor = Color.SlateBlue;
            tf.BackColor = Color.Orange;
            tl.Append(makeSampleText(tf), tf);
            // 再次在透明上涂上纯黑色:
            tl.Append("The end.", new TextFormat() { Font = fTimes, FontSize = 14, });
            // 布局和绘制文本:
            tl.PerformLayout(true);
            g.DrawTextLayout(tl, PointF.Empty);
            // 完毕:
            doc.Save(stream);
            return doc.Pages.Count;
        }
    }
}