引言
在PDF中我们可以通过C#程序代码来添加非常丰富的元素来呈现我们想要表达的内容,如绘制表格、文字,添加图形、图像等等。在本篇文章中,我将介绍如何在PDF中绘制图形,并设置图形属性的操作。
文章中将分以下要点进行介绍:
1. 绘制基本图形(线条、椭圆、圆形、矩形、三角形)
2. 绘制自定义图形
3. 绘制图形并设置图形透明度
提示:安装后,直接引用安装路径下Bin文件夹中的dll文件到项目程序中即可。
【示例1】绘制基本图形
C#
步骤1:新建一个PDF文档,添加页,添加画笔、画刷
//新建一个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;
步骤2:绘制圆形、矩形、线段、三角形
//绘入矩形(此处通过设置值来绘制成正方形) 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);
步骤3:保存文档
//保存并打开文档 doc.SaveToFile("基本图形.pdf"); System.Diagnostics.Process.Start("基本图形.pdf");
全部代码
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】绘制自定义图形
步骤1:创建pdf文档,调用方法DrawStar()绘制自定义图形,并保存
//新建一个PDF文档,添加页 PdfDocument doc = new PdfDocument(); PdfPageBase page = doc.Pages.Add(); //调用DrawStar()方法绘入五角星图形 DrawStar(page); //保存并打开文档 doc.SaveToFile("自定义图形.pdf"); System.Diagnostics.Process.Start("自定义图形.pdf");
步骤2:自定义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); }
全部代码:
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】设置色彩透明度
步骤1:新建一个PDF文档,添加页
PdfDocument doc = new PdfDocument(); PdfPageBase page = doc.Pages.Add();
步骤2:初始化一个PdfSeparationColorSpace的对象,用于创建基本色,并将基本色透明度设置为1
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);
步骤3:根据颜色创建画刷
PdfSolidBrush brush1 = new PdfSolidBrush(color1); PdfSolidBrush brush2 = new PdfSolidBrush(color2); PdfSolidBrush brush3 = new PdfSolidBrush(color3);
步骤4:绘入图形及文字,应用色彩透明度到图形
//绘入图形及文字并着色 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));
步骤5:保存文档
doc.SaveToFile("设置透明度.pdf"); System.Diagnostics.Process.Start("设置透明度.pdf");
全部代码:
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图形的全部内容,如需转载,请注明出处!
(本文完)
原文地址:https://www.cnblogs.com/Yesi/p/9110195.html
时间: 2024-10-04 10:59:06