# 引言

1. 绘制基本图形（线条、椭圆、圆形、矩形、三角形）
2. 绘制自定义图形
3. 绘制图形并设置图形透明度
所需工具Spire.PDF for .NET 4.0

# 【示例1】绘制基本图形

``````           //新建一个PDF文档，添加页
PdfDocument doc = new PdfDocument();
PdfPageBase page = doc.Pages.Add();

//设置画笔和画刷
PdfPen pen = new PdfPen(PdfBrushes.Black, 1f);
PdfBrush brush1 = PdfBrushes.RosyBrown;
PdfBrush brush2 = PdfBrushes.Goldenrod;``````

``````            //绘入矩形（此处通过设置值来绘制成正方形）
page.Canvas.DrawRectangle(pen, brush1, new Rectangle(new Point(50, 50), new Size(60, 60)));

//绘入椭圆（此处通过设置值来绘制成圆形）
page.Canvas.DrawEllipse(pen, brush2, 210, 50, 60, 60);

//绘入线段
page.Canvas.DrawLine(pen, new PointF(50, 115), new PointF(270, 115));

//绘入多边形（此处绘制成三角形）
PointF p1 = new PointF(130, 172);
PointF p2 = new PointF(160, 120);
PointF p3 = new PointF(190, 172);
PointF[] points = new PointF[] { p1, p2, p3 };
page.Canvas.DrawPolygon(pen, points);``````

``````            //保存并打开文档
doc.SaveToFile("基本图形.pdf");
System.Diagnostics.Process.Start("基本图形.pdf");``````

# 【示例2】绘制自定义图形

``````            //新建一个PDF文档，添加页
PdfDocument doc = new PdfDocument();
PdfPageBase page = doc.Pages.Add();

//调用DrawStar()方法绘入五角星图形
DrawStar(page);

//保存并打开文档
doc.SaveToFile("自定义图形.pdf");
System.Diagnostics.Process.Start("自定义图形.pdf");``````

``````private static void DrawStar(PdfPageBase page)
{
//设置五角星的5个顶点坐标
PointF[] points = new PointF[5];
for (int i = 0; i < points.Length; i++)
{
float x = (float)Math.Cos(i * 2 * Math.PI / 5);
float y = (float)Math.Sin(i * 2 * Math.PI / 5);
points[i] = new PointF(x, y);
}

//创建PdfPath类，在顶点之间添加线段组成五角星
PdfPath path = new PdfPath();

//保存画布状态
PdfGraphicsState state = page.Canvas.Save();

//实例化画笔和画刷1、画刷2
PdfPen pen = new PdfPen(Color.DeepSkyBlue, 0.02f);
PdfBrush brush1 = new PdfSolidBrush(Color.PaleGreen);
PdfBrush brush2 = new PdfSolidBrush(Color.Bisque);
//将坐标放大40倍
page.Canvas.ScaleTransform(40f, 40f);

//平移坐标
page.Canvas.TranslateTransform(1f, 1.5f);

//绘入第一个五角星
page.Canvas.DrawPath(pen, path);

//平移坐标并在新的位置绘入第二个五角星
page.Canvas.TranslateTransform(2f, 0f);
path.FillMode = PdfFillMode.Alternate;
page.Canvas.DrawPath(pen, brush1, path);

//平移坐标并在新的位置绘入第三个五角星
page.Canvas.TranslateTransform(2f, 0f);
path.FillMode = PdfFillMode.Winding;
page.Canvas.DrawPath(pen, brush2, path);

//实例化画刷3，平移坐标并在新的位置绘入第四个五角星
= new PdfLinearGradientBrush(new PointF(-2, 0), new PointF(2, 0), Color.OrangeRed, Color.Yellow);
page.Canvas.TranslateTransform(-4f, 2f);
path.FillMode = PdfFillMode.Alternate;
page.Canvas.DrawPath(pen, brush3, path);

//实例化画刷4，平移坐标并在新的位置绘入第五个五角星
= new PdfRadialGradientBrush(new PointF(0f, 0f), 0f, new PointF(0f, 0f), 1f, Color.Orchid, Color.LightBlue);
page.Canvas.TranslateTransform(2f, 0f);
path.FillMode = PdfFillMode.Winding;
page.Canvas.DrawPath(pen, brush4, path);

//实例化画刷5，平移坐标并在新的位置绘入第六个五角星
PdfTilingBrush brush5 = new PdfTilingBrush(new RectangleF(0, 0, 4f, 4f));
brush5.Graphics.DrawRectangle(brush3, 0, 0, 4f, 4f);
page.Canvas.TranslateTransform(2f, 0f);
path.FillMode = PdfFillMode.Winding;
page.Canvas.DrawPath(pen, brush5, path);

//再次保存画布状态
page.Canvas.Restore(state);
}``````

# 【示例3】设置色彩透明度

``````            PdfDocument doc = new PdfDocument();
PdfPageBase page = doc.Pages.Add();``````

``````             PdfSeparationColorSpace cs1 = new PdfSeparationColorSpace("MySpotColor", Color.DarkGreen);
PdfSeparationColorSpace cs2 = new PdfSeparationColorSpace("MySpotColor", Color.Yellow);
PdfSeparationColorSpace cs3 = new PdfSeparationColorSpace("MySpotColor", Color.DeepPink);
PdfSeparationColor color1 = new PdfSeparationColor(cs1, 1f);
PdfSeparationColor color2 = new PdfSeparationColor(cs2, 1f);
PdfSeparationColor color3 = new PdfSeparationColor(cs3, 1f);``````

``````            PdfSolidBrush brush1 = new PdfSolidBrush(color1);
PdfSolidBrush brush2 = new PdfSolidBrush(color2);
PdfSolidBrush brush3 = new PdfSolidBrush(color3);``````

``````            //绘入图形及文字并着色
page.Canvas.DrawPie(brush1, 10, 30, 60, 60, 360, 360);
page.Canvas.DrawPie(brush2, 10, 120, 60, 60, 360, 360);
page.Canvas.DrawPie(brush3, 10, 210, 60, 60, 360, 360);
page.Canvas.DrawString("透明度=1.0", new PdfTrueTypeFont(new Font("Arial Unicode MS", 10f), true), brush1, new PointF(16, 100));

//将基本色透明度设置为0.5,并绘入图片及文字
color1 = new PdfSeparationColor(cs1, 0.5f);
brush1 = new PdfSolidBrush(color1);
page.Canvas.DrawPie(brush1, 80, 30, 60, 60, 360, 360);
color2 = new PdfSeparationColor(cs2, 0.5f);
brush2 = new PdfSolidBrush(color2);
page.Canvas.DrawPie(brush2, 80, 120, 60, 60, 360, 360);
color3 = new PdfSeparationColor(cs3, 0.5f);
brush3 = new PdfSolidBrush(color3);
page.Canvas.DrawPie(brush3, 80, 210, 60, 60, 360, 360);

page.Canvas.DrawString("透明度=0.5", new PdfTrueTypeFont(new Font("Arial Unicode MS", 10f), true), brush1, new PointF(86, 100));

//将基本色透明度设置为0.25,并绘入图片及文字
color1 = new PdfSeparationColor(cs1, 0.25f);
brush1 = new PdfSolidBrush(color1);
page.Canvas.DrawPie(brush1, 150, 30, 60, 60, 360, 360);
color2 = new PdfSeparationColor(cs2, 0.25f);
brush2 = new PdfSolidBrush(color2);
page.Canvas.DrawPie(brush2, 150, 120, 60, 60, 360, 360);
color3 = new PdfSeparationColor(cs3, 0.25f);
brush3 = new PdfSolidBrush(color3);
page.Canvas.DrawPie(brush3, 150, 210, 60, 60, 360, 360);
page.Canvas.DrawString("透明度=0.25", new PdfTrueTypeFont(new Font("Arial Unicode MS", 10f), true), brush1, new PointF(156, 100));``````

``````            doc.SaveToFile("设置透明度.pdf");
System.Diagnostics.Process.Start("设置透明度.pdf");``````

（本文完）