SubSuperScript.cs
// 完毕:
using System;
using System.IO;
using System.Drawing;
using System.Text.RegularExpressions;
using GrapeCity.Documents.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 SubSuperScript
{
public int CreatePDF(Stream stream)
{
var doc = new GcPdfDocument();
var page = doc.NewPage();
var g = page.Graphics;
var rc = Common.Util.AddNote(
"TextFormat.Subscript 和 TextFormat.Superscript 属性的演示。" +
"",
page);
// 获取随机的“传说本身”段落:
var para = Common.Util.LoremIpsum(1, 18, 20, 20, 20);
// 将段落拆分为“lorem”、“ipsum”和其他所有内容:
const string sub = "lorem";
const string super = "ipsum";
var frags = Regex.Split(para, $"({sub})|({super})");
var font = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "times.ttf"));
// 创建下标和上标的文本格式:
var tfSub = new TextFormat() { Font = font, FontSize = 12, Subscript = true };
var tfSuper = new TextFormat(tfSub) { Subscript = false, Superscript = true };
// 使用“lorem”和“ipsum”的特殊格式将文本添加到 TextLayout:
var tl = g.CreateTextLayout();
tl.DefaultFormat.Font = font;
tl.DefaultFormat.FontSize = 12;
foreach (var frag in frags)
{
if (frag == sub)
tl.Append(frag, tfSub);
else if (frag == super)
tl.Append(frag, tfSuper);
else
tl.Append(frag);
}
// 设置布局属性并渲染文本:
tl.MaxWidth = page.Size.Width;
tl.MaxHeight = page.Size.Height - rc.Height;
tl.MarginAll = 72;
tl.MarginTop = rc.Bottom + 36;
tl.PerformLayout(true);
g.DrawTextLayout(tl, PointF.Empty);
// 完毕:
doc.Save(stream);
return doc.Pages.Count;
}
}
}