C# 绘制PDF图形——基本图形、自定义图形、色彩透明度

简介: 引言在PDF中我们可以通过C#程序代码来添加非常丰富的元素来呈现我们想要表达的内容,如绘制表格、文字,添加图形、图像等等。在本篇文章中,我将介绍如何在PDF中绘制图形,并设置图形属性的操作。 文章中将分以下要点进行介绍:1. 绘制基本图形(线条、椭圆、圆形、矩形、三角形)2. 绘制自定义图形3. 绘制图形并设置图形透明度 所需工具:Spire.PDF for .NET 4.0提示:安装后,直接引用安装路径下Bin文件夹中的dll文件到项目程序中即可。

引言

在PDF中我们可以通过C#程序代码来添加非常丰富的元素来呈现我们想要表达的内容,如绘制表格、文字,添加图形、图像等等。在本篇文章中,我将介绍如何在PDF中绘制图形,并设置图形属性的操作。

 

文章中将分以下要点进行介绍:

1. 绘制基本图形(线条、椭圆、圆形、矩形、三角形)

2. 绘制自定义图形

3. 绘制图形并设置图形透明度

 

所需工具Spire.PDF for .NET 4.0

提示:安装后,直接引用安装路径下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");
        }
    }
}
View Code

 

效果图:

【示例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);
        }
    }
}
View Code

 

效果图:

【示例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");
        }
    }
}
View Code

 

效果图:

以上是关于C#绘制PDF图形的全部内容,如需转载,请注明出处!

(本文完)

目录
相关文章
|
2月前
|
开发框架 前端开发 JavaScript
在Vue&Element前端项目中,使用FastReport + pdf.js生成并展示自定义报表
在Vue&Element前端项目中,使用FastReport + pdf.js生成并展示自定义报表
|
11月前
|
关系型数据库 MySQL C#
C# winform 一个窗体需要调用自定义用户控件的控件名称
给用户控件ucQRCode增加属性: //二维码图片 private PictureBox _pictureBoxFSHLQrCode; public PictureBox PictureBoxFSHLQrCode {   get { return _pictureBoxFSHLQrCode; }   set { this.pictureBoxFSHLQrCode = value; } } 在Form1窗体直接调用即可: ucQRCode uQRCode=new ucQRCode(); ucQRCode.PictureBoxFSHLQrCode.属性= 要复制或传给用户控件上的控件的值
61 0
|
前端开发 C#
C# 基于NPOI+Office COM组件 实现20行代码在线预览文档(word,excel,pdf,txt,png)
C# 基于NPOI+Office COM组件 实现20行代码在线预览文档(word,excel,pdf,txt,png)
|
3月前
|
C# C++
C# 自定义时间进度条
本文作者通过参考leslie_xin的一篇文章,成功创建了一个自定义的WinForms控件——时间进度条,该控件带有时间刻度和多种可定制的属性,如颜色、时间间隔等。作者在控件中加入了开始和结束时间,以及自适应的时间刻度间隔。控件能根据设置显示时间标签,并提供了事件处理,如值改变时的触发。代码中包含了计算时间刻度、绘制刻度线和时间标签的逻辑。作者强调了避免循环调用事件、使用OnXXX()形式的事件处理函数以及注意自定义控件中的属性和事件设计。
87 7
|
1月前
|
开发框架 .NET 编译器
总结一下 C# 如何自定义特性 Attribute 并进行应用
总结一下 C# 如何自定义特性 Attribute 并进行应用
|
1月前
|
存储 搜索推荐 C#
WPF/C#:让绘制的图形可以被选中并将信息显示在ListBox中
WPF/C#:让绘制的图形可以被选中并将信息显示在ListBox中
28 0
|
4月前
|
移动开发 JavaScript 安全
C# 实现微信自定义分享
C# 实现微信自定义分享
|
编译器 C#
c# 自定义扩展方法
c# 自定义扩展方法
|
4月前
|
C#
C#学习相关系列之自定义遍历器
C#学习相关系列之自定义遍历器
|
11月前
|
编译器 C#
C#中导入其它自定义的命名空间
c#中怎么导入其它自定义的命名空间首先要确保已经导入了想要导入的自定义的命名空间。如上图这时编译器应该会报错,此时就需要手动去添加引用了,cs文件默认没有添加引用,只是加载了想要导入的命名空间,但是没有添加引用,所以需要自己要手动添加引用。切记!然后会有一个对话框选择你想引用的命名空间,点击确定即可。注意:一般而言,C#中如果没有改变那么一般项目的类名都默认是Program,在引用时需要注...
95 1
C#中导入其它自定义的命名空间