RoundRectangle.cs
// 完毕:
using System;
using System.IO;
using System.Drawing;
using GrapeCity.Documents.Pdf;
using GrapeCity.Documents.Text;
using GrapeCity.Documents.Drawing;
namespace DsPdfWeb.Demos
{
// 该示例演示了如何绘制圆角矩形
// 使用专用的 DrawRoundRect/FillRoundRect 方法。
// 它还显示了如何实现相同的结果
// 图形路径。
public class RoundRectangle
{
public int CreatePDF(Stream stream)
{
var doc = new GcPdfDocument();
var page = doc.NewPage();
var g = page.Graphics;
var rc = Common.Util.AddNote(
"GcPdfGraphics 有专门的方法可以轻松绘制和填充圆角矩形。" +
"",
page);
// 圆角矩形的半径:
float rx = 36, ry = 24;
// 使用专用方法绘制和填充圆角矩形:
var rEasy = new RectangleF(rc.Left, rc.Bottom + 36, 144, 72);
g.FillRoundRect(rEasy, rx, ry, Color.PaleGreen);
g.DrawRoundRect(rEasy, rx, ry, Color.Blue, 4);
// 添加标签:
var tf = new TextFormat() { Font = StandardFonts.Times, FontSize = 14 };
g.DrawString("最简单的方法。", tf, rEasy, TextAlignment.Center, ParagraphAlignment.Center, false);
// 使用图形路径达到相同的结果:
var rHard = rEasy;
rHard.Offset(0, rEasy.Height + 36);
var path = MakeRoundRect(g, rHard, rx, ry);
g.FillPath(path, Color.PaleVioletRed);
g.DrawPath(path, Color.Purple, 4);
// 添加标签:
g.DrawString("艰难的方式。", tf, rHard, TextAlignment.Center, ParagraphAlignment.Center, false);
// 完毕:
doc.Save(stream);
return doc.Pages.Count;
}
// 该方法展示了如何创建可能使用的图形路径
// 在 GcGraphics 上填充或绘制任意形状。
private static IPath MakeRoundRect(GcGraphics g, RectangleF rc, float rx, float ry)
{
var path = g.CreatePath();
var sz = new SizeF(rx, ry);
// 从水平左上角开始:
path.BeginFigure(new PointF(rc.Left + rx, rc.Top));
path.AddLine(new PointF(rc.Right - rx, rc.Top));
path.AddArc(new ArcSegment() { Point = new PointF(rc.Right, rc.Top + ry), SweepDirection = SweepDirection.Clockwise, Size = sz });
path.AddLine(new PointF(rc.Right, rc.Bottom - ry));
path.AddArc(new ArcSegment() { Point = new PointF(rc.Right - rx, rc.Bottom), SweepDirection = SweepDirection.Clockwise, Size = sz });
path.AddLine(new PointF(rc.Left + rx, rc.Bottom));
path.AddArc(new ArcSegment() { Point = new PointF(rc.Left, rc.Bottom - ry), SweepDirection = SweepDirection.Clockwise, Size = sz });
path.AddLine(new PointF(rc.Left, rc.Top + ry));
path.AddArc(new ArcSegment() { Point = new PointF(rc.Left + rx, rc.Top), SweepDirection = SweepDirection.Clockwise, Size = sz });
path.EndFigure(FigureEnd.Closed);
return path;
}
}
}