Spire.PDF支持在PDF文档中直接绘制线段、矩形、椭圆、扇形、多边形等基本图形,根据已有的方法程序员可以创建自定义函数绘制特殊图形。除此以外,Spire.PDF还支持为同一色彩创建不同的透明度,让图形的着色更有层次感。
直接绘制基本图形
C#
//新建一个PDF文档
PdfDocument doc = new PdfDocument();
PdfPageBase page = doc.Pages.Add();
//设置画笔和画刷
PdfPen pen = new PdfPen(PdfBrushes.Black, 1f);
PdfBrush brush = PdfBrushes.Red;
//绘入矩形
page.Canvas.DrawRectangle(pen, brush, new Rectangle(new Point(50, 50), new Size(60, 60)));
//绘入椭圆(此处为圆形)
page.Canvas.DrawEllipse(pen, brush, 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");
VB.NET
'新建一个PDF文档
Dim doc As New PdfDocument()
Dim page As PdfPageBase = doc.Pages.Add()
'设置画笔和画刷
Dim pen As New PdfPen(PdfBrushes.Black, 1F)
Dim brush As PdfBrush = PdfBrushes.Red
'绘入矩形
page.Canvas.DrawRectangle(pen, brush, New Rectangle(New Point(50, 50), New Size(60, 60)))
'绘入椭圆(此处为圆形)
page.Canvas.DrawEllipse(pen, brush, 210, 50, 60, 60)
'绘入线段
page.Canvas.DrawLine(pen, New PointF(50, 115), New PointF(270, 115))
'绘入多边形(此处为三角形)
Dim p1 As New PointF(130, 172)
Dim p2 As New PointF(160, 120)
Dim p3 As New PointF(190, 172)
Dim points As PointF() = New PointF() {p1, p2, p3}
page.Canvas.DrawPolygon(pen, points)
'保存文档
doc.SaveToFile("基本图形.pdf")
绘制自定义图形
C#
static void Main(string[] args)
{
//新建一个PDF文档
PdfDocument doc = new PdfDocument();
PdfPageBase page = doc.Pages.Add();
//调用DrawStar方法绘入五角星
DrawStar(page);
//保存文档
doc.SaveToFile("自定义图形.pdf");
}
//自定义DrawStar方法画几个不同样式的五角星
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();
path.AddLine(points[2], points[0]);
path.AddLine(points[0], points[3]);
path.AddLine(points[3], points[1]);
path.AddLine(points[1], points[4]);
path.AddLine(points[4], points[2]);
//保存画布状态
PdfGraphicsState state = page.Canvas.Save();
//设置画笔和画刷
PdfPen pen = new PdfPen(Color.DeepSkyBlue, 0.02f);
PdfBrush brush1 = new PdfSolidBrush(Color.CadetBlue);
//将坐标放大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, brush1, path);
//平移坐标并在新的位置绘入五角星
PdfLinearGradientBrush brush2
= new PdfLinearGradientBrush(new PointF(-2, 0), new PointF(2, 0), Color.Red, Color.Blue);
page.Canvas.TranslateTransform(-4f, 2f);
path.FillMode = PdfFillMode.Alternate;
page.Canvas.DrawPath(pen, brush2, path);
//平移坐标并在新的位置绘入五角星
PdfRadialGradientBrush brush3
= new PdfRadialGradientBrush(new PointF(0f, 0f), 0f, new PointF(0f, 0f), 1f, Color.Red, Color.Blue);
page.Canvas.TranslateTransform(2f, 0f);
path.FillMode = PdfFillMode.Winding;
page.Canvas.DrawPath(pen, brush3, path);
//平移坐标并在新的位置绘入五角星
PdfTilingBrush brush4 = new PdfTilingBrush(new RectangleF(0, 0, 4f, 4f));
brush4.Graphics.DrawRectangle(brush2, 0, 0, 4f, 4f);
page.Canvas.TranslateTransform(2f, 0f);
path.FillMode = PdfFillMode.Winding;
page.Canvas.DrawPath(pen, brush4, path);
//再次保存画布状态
page.Canvas.Restore(state);
}
VB.NET
Private Shared Sub Main(args As String())
'新建一个PDF文档
Dim doc As New PdfDocument()
Dim page As PdfPageBase = doc.Pages.Add()
'调用DrawStar方法绘入五角星
DrawStar(page)
'保存文档
doc.SaveToFile("自定义图形.pdf")
End Sub
'自定义DrawStar方法画几个不同样式的五角星
Private Shared Sub DrawStar(page As PdfPageBase)
'设置五角星的5个顶点坐标
Dim points As PointF() = New PointF(4) {}
For i As Integer = 0 To points.Length - 1
Dim x As Single = CSng(Math.Cos(i * 2 * Math.PI / 5))
Dim y As Single = CSng(Math.Sin(i * 2 * Math.PI / 5))
points(i) = New PointF(x, y)
Next
'创建PdfPath类,在顶点之间添加线段组成五角星
Dim path As New PdfPath()
path.AddLine(points(2), points(0))
path.AddLine(points(0), points(3))
path.AddLine(points(3), points(1))
path.AddLine(points(1), points(4))
path.AddLine(points(4), points(2))
'保存画布状态
Dim state As PdfGraphicsState = page.Canvas.Save()
'设置画笔和画刷
Dim pen As New PdfPen(Color.DeepSkyBlue, 0.02F)
Dim brush1 As PdfBrush = New PdfSolidBrush(Color.CadetBlue)
'将坐标放大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, brush1, path)
'平移坐标并在新的位置绘入五角星
Dim brush2 As New PdfLinearGradientBrush(New PointF(-2, 0), New PointF(2, 0), Color.Red, Color.Blue)
page.Canvas.TranslateTransform(-4F, 2F)
path.FillMode = PdfFillMode.Alternate
page.Canvas.DrawPath(pen, brush2, path)
'平移坐标并在新的位置绘入五角星
Dim brush3 As New PdfRadialGradientBrush(New PointF(0F, 0F), 0F, New PointF(0F, 0F), 1F, Color.Red, Color.Blue)
page.Canvas.TranslateTransform(2F, 0F)
path.FillMode = PdfFillMode.Winding
page.Canvas.DrawPath(pen, brush3, path)
'平移坐标并在新的位置绘入五角星
Dim brush4 As New PdfTilingBrush(New RectangleF(0, 0, 4F, 4F))
brush4.Graphics.DrawRectangle(brush2, 0, 0, 4F, 4F)
page.Canvas.TranslateTransform(2F, 0F)
path.FillMode = PdfFillMode.Winding
page.Canvas.DrawPath(pen, brush4, path)
'再次保存画布状态
page.Canvas.Restore(state)
End Sub
创建不同透明度的颜色
C#
//新建一个PDF文档
PdfDocument doc = new PdfDocument();
PdfPageBase page = doc.Pages.Add();
//初始化一个PdfSeparationColorSpace的对象,用于创建基本色
PdfSeparationColorSpace cs = new PdfSeparationColorSpace("MySpotColor", Color.Purple);
//将基本色透明度设置为1
PdfSeparationColor color = new PdfSeparationColor(cs, 1f);
//根据颜色创建画刷
PdfSolidBrush brush = new PdfSolidBrush(color);
//绘入图形及文字并着色
page.Canvas.DrawPie(brush, 10, 30, 60, 60, 360, 360);
page.Canvas.DrawString("透明度=1.0", new PdfTrueTypeFont(new Font("宋体", 10f), true), brush, new PointF(16, 100));
//将基本色透明度设置为0.5,并绘入图片及文字
color = new PdfSeparationColor(cs, 0.5f);
brush = new PdfSolidBrush(color);
page.Canvas.DrawPie(brush, 80, 30, 60, 60, 360, 360);
page.Canvas.DrawString("透明度=0.5", new PdfTrueTypeFont(new Font("宋体", 10f),true), brush, new PointF(86, 100));
//将基本色透明度设置为0.25,并绘入图片及文字
color = new PdfSeparationColor(cs, 0.25f);
brush = new PdfSolidBrush(color);
page.Canvas.DrawPie(brush, 150, 30, 60, 60, 360, 360);
page.Canvas.DrawString("透明度=0.25", new PdfTrueTypeFont(new Font("宋体", 10f),true), brush, new PointF(156, 100));
//保存文档
doc.SaveToFile("设置透明度.pdf");
VB.NET
'新建一个PDF文档
Dim doc As New PdfDocument()
Dim page As PdfPageBase = doc.Pages.Add()
'初始化一个PdfSeparationColorSpace的对象,用于创建基本色
Dim cs As New PdfSeparationColorSpace("MySpotColor", Color.Purple)
'将基本色透明度设置为1
Dim color__1 As New PdfSeparationColor(cs, 1F)
'根据颜色创建画刷
Dim brush As New PdfSolidBrush(color__1)
'绘入图形及文字并着色
page.Canvas.DrawPie(brush, 10, 30, 60, 60, 360, _
360)
page.Canvas.DrawString("透明度=1.0", New PdfTrueTypeFont(New Font("宋体", 10F), True), brush, New PointF(16, 100))
'将基本色透明度设置为0.5,并绘入图片及文字
color__1 = New PdfSeparationColor(cs, 0.5F)
brush = New PdfSolidBrush(color__1)
page.Canvas.DrawPie(brush, 80, 30, 60, 60, 360, _
360)
page.Canvas.DrawString("透明度=0.5", New PdfTrueTypeFont(New Font("宋体", 10F), True), brush, New PointF(86, 100))
'将基本色透明度设置为0.25,并绘入图片及文字
color__1 = New PdfSeparationColor(cs, 0.25F)
brush = New PdfSolidBrush(color__1)
page.Canvas.DrawPie(brush, 150, 30, 60, 60, 360, _
360)
page.Canvas.DrawString("透明度=0.25", New PdfTrueTypeFont(New Font("宋体", 10F), True), brush, New PointF(156, 100))
'保存文档
doc.SaveToFile("设置透明度.pdf")