引言
在PDF中我们可以通过C#程序代码来添加非常丰富的元素来呈现我们想要表达的内容,如绘制表格、文字,添加图形、图像等等。在本篇文章中,我将介绍如何在PDF中绘制图形,并设置图形属性的操作。
文章中将分以下要点进行介绍:
1. 绘制基本图形(线条、椭圆、圆形、矩形、三角形)
2. 绘制自定义图形
3. 绘制图形并设置图形透明度
【示例1】绘制基本图形
【C#】
using Spire.Pdf; using Spire.Pdf.Graphics; using System.Drawing; namespace DrawRectangle_PDF { class Program { static void Main(string[] args) { //新建一个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】绘制自定义图形
【C#】
using Spire.Pdf; using Spire.Pdf.Graphics; using System; using System.Drawing; namespace DrawCustomGraphics_PDF { class Program { static void Main(string[] args) { //新建一个PDF文档,添加页 PdfDocument doc = new PdfDocument(); PdfPageBase page = doc.Pages.Add(); //调用DrawStar()方法绘入五角星图形 DrawStar(page); //保存并打开文档 doc.SaveToFile("自定义图形.pdf"); System.Diagnostics.Process.Start("自定义图形.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(); //实例化画笔和画刷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,平移坐标并在新的位置绘入第四个五角星 PdfLinearGradientBrush brush3 = 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,平移坐标并在新的位置绘入第五个五角星 PdfRadialGradientBrush brush4 = 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】设置色彩透明度
【C#】
using Spire.Pdf; using Spire.Pdf.ColorSpace; using Spire.Pdf.Graphics; using System.Drawing; namespace CrearteSpotColor_PDF { class Program { static void Main(string[] args) { //新建一个PDF文档 PdfDocument doc = new PdfDocument(); PdfPageBase page = doc.Pages.Add(); //初始化一个PdfSeparationColorSpace的对象,用于创建基本色 PdfSeparationColorSpace cs1 = new PdfSeparationColorSpace("MySpotColor", Color.DarkGreen); PdfSeparationColorSpace cs2 = new PdfSeparationColorSpace("MySpotColor", Color.Yellow); PdfSeparationColorSpace cs3 = new PdfSeparationColorSpace("MySpotColor", Color.DeepPink); //将基本色透明度设置为1 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"); } } }
效果图:
以上是关于C#绘制PDF图形的全部内容,如需转载,请注明出处!
(本文完)
相关推荐
C# 绘制各种图形代码
图形操作:选择、修改图形要素、修改图形要素大小、修改、undo、redo、旋转、移动旋转图形、复制、粘贴、剪切、编辑文字、修改线型(端点/中间加图形)、修改图形要素属性、上移、下移等操作。 事件:图形要素...
C#绘制图形,绘制一个扇形C#绘制图形,
C# 绘制自定义虚线C# 绘制自定义虚线C# 绘制自定义虚线C# 绘制自定义虚线C# 绘制自定义虚线C# 绘制自定义虚线
C# 绘制图形.rar,柱状图,曲线图,饼状图
vs05,08,10自带的trackbar不支持透明背景,尤其是在tabcontrol中,导致做出来的东西在有主题的xp、win7下很难看,这是个三方控件,经过测试可用。
C# 初级者天堂——入门教程 PDF C# 初级者天堂——入门教程 PDF
C#绘制图形和统计图 柱形图,线性图! 分类统计!!! 用vs2005直接打开!
C# 绘制八边形图形窗体,这里只是一个框架!!!!!
c#绘制矩形、圆形、直线等及其简单的选择、拉伸、缩放、平移,以及对直线的裁剪操作
C#绘制坐标轴绘制坐标轴绘制坐标轴绘制坐标轴C#C#绘制坐标轴绘制坐标轴绘制坐标轴绘制坐标轴C#绘制坐标轴绘制坐标轴绘制坐标轴绘制坐标轴C#绘制坐标轴绘制坐标轴绘制坐标轴绘制坐标轴
本书全面详细地阐述了c#图形设计技术,专门列举了许多c#二维三维图形绘制的工程实例,可称为c#图形处理方面的一本宝典。 本书分为五个部分共10章,第一部分介绍c#基本的数据类型和图形基础技术,第二部分讲述二维...
绘制三维模型时,先根据点画出面,通过面与面之间的连接绘制出形体。 适用于一些不规则的形体绘制。
c#图形书中最经典的一本书当中包括饼图,条形图,绘图板制作等 第1章 GDI+ ——下一代图形接口 1.1 理解GDI+ 1.2 探索GDI+ 的功能 1.3 从GDI的角度学习GDI+ 1.4 .NET中的GDI+ 名称空间和类 总结 第2章 第一个...
C# 实现鼠标拖动绘制线段、矩形,所绘图形支持拖拽,分享给有需要的朋友。
该程序详细描述了c#绘图编程技法,为C#图形绘制提供帮助
用C#绘制自定义虚线