更多资源请查看:【Spire.PDF-文档操作教程】 | 【Spire.Doc教程】 | 【Spire.XLS教程】
Spire.PDF是一个专业的PDF组件,能够独立地创建、编写、编辑、操作和阅读PDF文件,支持 .NET、Java、WPF和Silverlight。
【下载Spire.PDF最新试用版】
Spire.PDF支持在PDF文档中直接绘制线段、矩形、椭圆、扇形、多边形等基本图形,根据已有的方法程序员可以创建自定义函数绘制特殊图形。除此以外,Spire.PDF还支持为同一色彩创建不同的透明度,让图形的着色更有层次感。
直接绘制基本图形
//新建一个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");
效果图:
绘制自定义图形
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); }
效果图:
创建不同透明度的颜色
//新建一个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("Arial Unicode MS", 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("Arial Unicode MS", 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("Arial Unicode MS", 10f),true), brush, new PointF(156, 100)); //保存文档 doc.SaveToFile("设置透明度.pdf");
效果图: