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

namespace DsPdfWeb.Demos.Basics
{
    // 示例展示了如何在使用普通字体时控制粗体和/或斜体模拟。
    public class BoldItalicEmulation
    {
        public int CreatePDF(Stream stream)
        {
            var fc = new FontCollection();
            fc.RegisterDirectory(Path.Combine("Resources", "Fonts"));
            var doc = new GcPdfDocument();
            var g = doc.NewPage().Graphics;
            var rc = Common.Util.AddNote(
                "TextFormat.FontStyle 允许使用粗体和/或斜体模拟\n" +
                "", doc.Pages.Last);
            // 文本插入点:
            var ip = new PointF(rc.Left, rc.Bottom + 36);
            var tf = new TextFormat();
            // 我们特别获得了该字体的非粗体/非斜体版本:
            tf.Font = fc.FindFamilyName("Times New Roman", false, false);
            tf.FontSize = 16;
            g.DrawString($"Regular Times font: {tf.Font.FullFontName}", tf, ip);
            ip.Y += 36;
            // 使用相同(常规)字体但模拟粗体和斜体绘制一些字符串:
            tf.FontStyle = GCTEXT.FontStyle.Bold;
            g.DrawString($"Bold emulation using font: {tf.Font.FullFontName}", tf, ip);
            ip.Y += 36;
            tf.FontStyle = GCTEXT.FontStyle.Italic;
            g.DrawString($"Italic emulation using font: {tf.Font.FullFontName}", tf, ip);
            ip.Y += 36;
            tf.FontStyle = GCTEXT.FontStyle.BoldItalic;
            g.DrawString($"Bold+Italic emulation using font: {tf.Font.FullFontName}", tf, ip);
            ip.Y += 36;
            //
            // 现在我们使用字体的“真正的”粗体/斜体变体渲染一些字符串:
            tf.FontStyle = GCTEXT.FontStyle.Regular;
            tf.Font = fc.FindFamilyName("Times New Roman", true, false);
            g.DrawString($"Using real bold font: {tf.Font.FullFontName}", tf, ip);
            ip.Y += 36;
            tf.Font = fc.FindFamilyName("Times New Roman", false, true);
            g.DrawString($"Using real italic font: {tf.Font.FullFontName}", tf, ip);
            ip.Y += 36;
            tf.Font = fc.FindFamilyName("Times New Roman", true, true);
            g.DrawString($"Using real bold italic font: {tf.Font.FullFontName}", tf, ip);
            ip.Y += 36;
            // 完毕:
            doc.Save(stream);
            return doc.Pages.Count;
        }
    }
}