Gradients.cs
// 完毕:
using System;
using System.IO;
using System.Drawing;
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
{
    // 此示例展示了如何创建渐变填充
    // 使用 LinearGradientBrush 和 RadialGradientBrush。
    public class Gradients
    {
        public int CreatePDF(Stream stream)
        {
            var doc = new GcPdfDocument();
            var g = doc.NewPage().Graphics;
            var testRectSize = new SizeF(72 * 4, 72);
            var dy = 12;
            // TextLayout绘制标签:
            var tl = g.CreateTextLayout();
            tl.DefaultFormat.Font = StandardFonts.Times;
            tl.DefaultFormat.FontSize = 18;
            tl.DefaultFormat.ForeColor = Color.Chartreuse;
            tl.MaxWidth = testRectSize.Width;
            tl.MaxHeight = testRectSize.Height;
            tl.TextAlignment = TextAlignment.Center;
            tl.ParagraphAlignment = ParagraphAlignment.Center;
            // 注释1:
            var rc = Common.Util.AddNote("使用 LinearGradientBrush 的线性渐变:", doc.Pages.Last, new RectangleF(72, 36, 500, 100));
            // 文本插入点:
            var ip = new PointF(rc.Left, rc.Bottom + dy);
            // 绘制渐变填充矩形的本地操作:
            Action<GCDRAW.Brush, string> drawSwatch = (b_, txt_) =>
            {
                var rect = new RectangleF(ip, testRectSize);
                // 用渐变画笔填充矩形:
                g.FillRectangle(rect, b_);
                // 绘制边框、文本信息等:
                g.DrawRectangle(rect, Color.Magenta);
                tl.Clear();
                tl.Append(txt_);
                tl.MaxHeight = testRectSize.Height;
                tl.MaxWidth = testRectSize.Width;
                tl.PerformLayout(true);
                g.DrawTextLayout(tl, ip);
                ip.Y += rect.Height + dy;
            };
            // 水平渐变:
            var linearGradBrush = new LinearGradientBrush(Color.Red, Color.Blue);
            drawSwatch(linearGradBrush, $"Linear gradient\nfrom {linearGradBrush.StartPoint} to {linearGradBrush.EndPoint}");
            // 垂直渐变:
            linearGradBrush = new LinearGradientBrush(Color.Red, new PointF(0, 0), Color.Green, new PointF(0, 1));
            drawSwatch(linearGradBrush, $"Linear gradient\r\nfrom {linearGradBrush.StartPoint} to {linearGradBrush.EndPoint}");
            // 对角渐变(增加样本高度以更好地显示对角线):
            testRectSize.Height *= 2;
            linearGradBrush = new LinearGradientBrush(Color.Red, new PointF(0, 0), Color.Teal, new PointF(1, 1));
            drawSwatch(linearGradBrush, $"Linear gradient\r\nfrom {linearGradBrush.StartPoint} to {linearGradBrush.EndPoint}");
            // 径向渐变画笔
            rc = Common.Util.AddNote("使用 RadialGradientBrush 的径向渐变:", doc.Pages.Last, new RectangleF(ip, new SizeF(500, 100)));
            ip.Y = rc.Bottom + dy;
            // 居中:
            var radialGradBrush = new RadialGradientBrush(Color.Orange, Color.Purple);
            drawSwatch(radialGradBrush, $"Radial gradient\r\nwith origin at {radialGradBrush.GradientOrigin}");
            // 右下角中心:
            radialGradBrush = new RadialGradientBrush(Color.OrangeRed, Color.DarkBlue, new PointF(1, 1));
            drawSwatch(radialGradBrush, $"Radial gradient\r\nwith origin at {radialGradBrush.GradientOrigin}");
            // 完毕:
            doc.Save(stream);
            return doc.Pages.Count;
        }
    }
}