您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
在PDF中我们可以通过C#程序代码来添加非常丰富的元素来呈现我们想要表达的内容,如绘制表格、文字,添加图形、图像等等。在本篇文章中,我将介绍如何在PDF中绘制图形,并设置图形属性的操作。
文章中将分以下要点进行介绍:
提示:安装后,直接引用安装路径下Bin文件夹中的dll文件到项目程序中即可。
步骤1:新建一个PDF文档,添加页,添加画笔、画刷
//新建一个PDF文档,添加页
PdfDocument doc = new PdfDocument();
PdfPageBase page = doc.Pages.Add();
//设置画笔和画刷
PdfPen pen = new PdfPen(PdfBrushes.Black, 1f);
PdfBrush brush2 = PdfBrushes.RosyBrown;
PdfBrush brush3 = PdfBrushes.Goldenrod;
步骤2:绘制圆形、矩形、线段、三角形
//绘入矩形(此处通过设置值来绘制成正方形)
page.Canvas.DrawRectangle(pen, brush2, new Rectangle(new Point(50, 50), new Size(60, 60)));
//绘入椭圆(此处通过设置值来绘制成圆形)
page.Canvas.DrawEllipse(pen, brush3, 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");
图形添加效果:
步骤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 brush2 = new PdfSolidBrush(Color.PaleGreen);
PdfBrush brush3 = 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, brush2, path);
//平移坐标并在新的位置绘入第三个五角星
page.Canvas.TranslateTransform(2f, 0f);
path.FillMode = PdfFillMode.Winding;
page.Canvas.DrawPath(pen, brush3, path);
//实例化画刷3,平移坐标并在新的位置绘入第四个五角星
PdfLinearGradientBrush brush4
= 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, brush4, path);
//实例化画刷4,平移坐标并在新的位置绘入第五个五角星
PdfRadialGradientBrush brush5
= 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, brush5, path);
//实例化画刷5,平移坐标并在新的位置绘入第六个五角星
PdfTilingBrush brush6 = new PdfTilingBrush(new RectangleF(0, 0, 4f, 4f));
brush6.Graphics.DrawRectangle(brush4, 0, 0, 4f, 4f);
page.Canvas.TranslateTransform(2f, 0f);
path.FillMode = PdfFillMode.Winding;
page.Canvas.DrawPath(pen, brush6, path);
//再次保存画布状态
page.Canvas.Restore(state);
}
自定义图形添加效果:
步骤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 brush2 = new PdfSolidBrush(color1);
PdfSolidBrush brush3 = new PdfSolidBrush(color2);
PdfSolidBrush brush4 = new PdfSolidBrush(color3);
步骤4:绘入图形及文字,应用色彩透明度到图形
//绘入图形及文字并着色
page.Canvas.DrawPie(brush2, 10, 30, 60, 60, 360, 360);
page.Canvas.DrawPie(brush3, 10, 120, 60, 60, 360, 360);
page.Canvas.DrawPie(brush4, 10, 210, 60, 60, 360, 360);
page.Canvas.DrawString("透明度=1.0", new PdfTrueTypeFont(new Font("Arial Unicode MS", 10f), true), brush2, new PointF(16, 100));
//将基本色透明度设置为0.5,并绘入图片及文字
color1 = new PdfSeparationColor(cs1, 0.5f);
brush2 = new PdfSolidBrush(color1);
page.Canvas.DrawPie(brush2, 80, 30, 60, 60, 360, 360);
color2 = new PdfSeparationColor(cs2, 0.5f);
brush3 = new PdfSolidBrush(color2);
page.Canvas.DrawPie(brush3, 80, 120, 60, 60, 360, 360);
color3 = new PdfSeparationColor(cs3, 0.5f);
brush4 = new PdfSolidBrush(color3);
page.Canvas.DrawPie(brush4, 80, 210, 60, 60, 360, 360);
page.Canvas.DrawString("透明度=0.5", new PdfTrueTypeFont(new Font("Arial Unicode MS", 10f), true), brush2, new PointF(86, 100));
//将基本色透明度设置为0.25,并绘入图片及文字
color1 = new PdfSeparationColor(cs1, 0.25f);
brush2 = new PdfSolidBrush(color1);
page.Canvas.DrawPie(brush2, 150, 30, 60, 60, 360, 360);
color2 = new PdfSeparationColor(cs2, 0.25f);
brush3 = new PdfSolidBrush(color2);
page.Canvas.DrawPie(brush3, 150, 120, 60, 60, 360, 360);
color3 = new PdfSeparationColor(cs3, 0.25f);
brush4 = new PdfSolidBrush(color3);
page.Canvas.DrawPie(brush4, 150, 210, 60, 60, 360, 360);
page.Canvas.DrawString("透明度=0.25", new PdfTrueTypeFont(new Font("Arial Unicode MS", 10f), true), brush2, new PointF(156, 100));
步骤5:保存文档
doc.SaveToFile("设置透明度.pdf");
System.Diagnostics.Process.Start("设置透明度.pdf");
色彩透明度实现效果:
以上是关于C#绘制PDF图形的全部内容,如需转载,请注明出处!
(本文完)
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。