C# 绘制PDF图形——基本图形、自定义图形、色彩透明度
引言
在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(, ), new Size(, ))); //绘入椭圆(此处通过设置值来绘制成圆形)
page.Canvas.DrawEllipse(pen, brush2, , , , ); //绘入线段
page.Canvas.DrawLine(pen, new PointF(, ), new PointF(, )); //绘入多边形(此处绘制成三角形)
PointF p1 = new PointF(, );
PointF p2 = new PointF(, );
PointF p3 = new PointF(, );
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(, ), new Size(, ))); //绘入椭圆(此处通过设置值来绘制成圆形)
page.Canvas.DrawEllipse(pen, brush2, , , , ); //绘入线段
page.Canvas.DrawLine(pen, new PointF(, ), new PointF(, )); //绘入多边形(此处绘制成三角形)
PointF p1 = new PointF(, );
PointF p2 = new PointF(, );
PointF p3 = new PointF(, );
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[];
for (int i = ; i < points.Length; i++)
{
float x = (float)Math.Cos(i * * Math.PI / );
float y = (float)Math.Sin(i * * Math.PI / );
points[i] = new PointF(x, y);
} //创建PdfPath类,在顶点之间添加线段组成五角星
PdfPath path = new PdfPath();
path.AddLine(points[], points[]);
path.AddLine(points[], points[]);
path.AddLine(points[], points[]);
path.AddLine(points[], points[]);
path.AddLine(points[], points[]); //保存画布状态
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(-, ), new PointF(, ), 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(, , 4f, 4f));
brush5.Graphics.DrawRectangle(brush3, , , 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[];
for (int i = ; i < points.Length; i++)
{
float x = (float)Math.Cos(i * * Math.PI / );
float y = (float)Math.Sin(i * * Math.PI / );
points[i] = new PointF(x, y);
} //创建PdfPath类,在顶点之间添加线段组成五角星
PdfPath path = new PdfPath();
path.AddLine(points[], points[]);
path.AddLine(points[], points[]);
path.AddLine(points[], points[]);
path.AddLine(points[], points[]);
path.AddLine(points[], points[]); //保存画布状态
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(-, ), new PointF(, ), 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(, , 4f, 4f));
brush5.Graphics.DrawRectangle(brush3, , , 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, , , , , , );
page.Canvas.DrawPie(brush2, , , , , , );
page.Canvas.DrawPie(brush3, , , , , , );
page.Canvas.DrawString("透明度=1.0", new PdfTrueTypeFont(new Font("Arial Unicode MS", 10f), true), brush1, new PointF(, )); //将基本色透明度设置为0.5,并绘入图形及文字
color1 = new PdfSeparationColor(cs1, 0.5f);
brush1 = new PdfSolidBrush(color1);
page.Canvas.DrawPie(brush1, , , , , , );
color2 = new PdfSeparationColor(cs2, 0.5f);
brush2 = new PdfSolidBrush(color2);
page.Canvas.DrawPie(brush2, , , , , , );
color3 = new PdfSeparationColor(cs3, 0.5f);
brush3 = new PdfSolidBrush(color3);
page.Canvas.DrawPie(brush3, , , , , , ); page.Canvas.DrawString("透明度=0.5", new PdfTrueTypeFont(new Font("Arial Unicode MS", 10f), true), brush1, new PointF(, )); //将基本色透明度设置为0.25,并绘入图形及文字
color1 = new PdfSeparationColor(cs1, 0.25f);
brush1 = new PdfSolidBrush(color1);
page.Canvas.DrawPie(brush1, , , , , , );
color2 = new PdfSeparationColor(cs2, 0.25f);
brush2 = new PdfSolidBrush(color2);
page.Canvas.DrawPie(brush2, , , , , , );
color3 = new PdfSeparationColor(cs3, 0.25f);
brush3 = new PdfSolidBrush(color3);
page.Canvas.DrawPie(brush3, , , , , , );
page.Canvas.DrawString("透明度=0.25", new PdfTrueTypeFont(new Font("Arial Unicode MS", 10f), true), brush1, new PointF(, ));
步骤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, , , , , , );
page.Canvas.DrawPie(brush2, , , , , , );
page.Canvas.DrawPie(brush3, , , , , , );
page.Canvas.DrawString("透明度=1.0", new PdfTrueTypeFont(new Font("Arial Unicode MS", 10f), true), brush1, new PointF(, )); //将基本色透明度设置为0.5,并绘入图片及文字
color1 = new PdfSeparationColor(cs1, 0.5f);
brush1 = new PdfSolidBrush(color1);
page.Canvas.DrawPie(brush1, , , , , , );
color2 = new PdfSeparationColor(cs2, 0.5f);
brush2 = new PdfSolidBrush(color2);
page.Canvas.DrawPie(brush2, , , , , , );
color3 = new PdfSeparationColor(cs3, 0.5f);
brush3 = new PdfSolidBrush(color3);
page.Canvas.DrawPie(brush3, , , , , , ); page.Canvas.DrawString("透明度=0.5", new PdfTrueTypeFont(new Font("Arial Unicode MS", 10f), true), brush1, new PointF(, )); //将基本色透明度设置为0.25,并绘入图片及文字
color1 = new PdfSeparationColor(cs1, 0.25f);
brush1 = new PdfSolidBrush(color1);
page.Canvas.DrawPie(brush1, , , , , , );
color2 = new PdfSeparationColor(cs2, 0.25f);
brush2 = new PdfSolidBrush(color2);
page.Canvas.DrawPie(brush2, , , , , , );
color3 = new PdfSeparationColor(cs3, 0.25f);
brush3 = new PdfSolidBrush(color3);
page.Canvas.DrawPie(brush3, , , , , , );
page.Canvas.DrawString("透明度=0.25", new PdfTrueTypeFont(new Font("Arial Unicode MS", 10f), true), brush1, new PointF(, )); //保存并打开文档
doc.SaveToFile("设置透明度.pdf");
System.Diagnostics.Process.Start("设置透明度.pdf");
}
}
}
效果图:

以上是关于C#绘制PDF图形的全部内容,如需转载,请注明出处!
(本文完)
C# 绘制PDF图形——基本图形、自定义图形、色彩透明度的更多相关文章
- 使用Ogre::ManualObject 绘制自定义图形
在ogre中如果需要进行自定义图形绘制可以使用ManualObject.例如绘制一个三角形的用法如下: SceneNode* pGridNode = m_pBaseNode->createChi ...
- [Xcode 实际操作]九、实用进阶-(19)重写父类的绘图方法,使用图形上下文绘制自定义图形
目录:[Swift]Xcode实际操作 本文将演示如何使用图形上下文,绘制自定义图形. 使用快捷键[Command]+[N]创建一个新的类文件. (在项目文件夹[DemoApp]上点击鼠标右键[New ...
- opencv-7-鼠标绘制自定义图形
opencv-7-鼠标绘制自定义图形 opencvc++qt 开始之前 昨天写了具体的基本的图形绘制, 然后我们使用相应的函数接口进行调用, 便能够在图像上绘制出来相应的图形, 我们以图像绘制为例, ...
- 软件项目技术点(7)——在canvas上绘制自定义图形
AxeSlide软件项目梳理 canvas绘图系列知识点整理 图形种类 目前我们软件可以绘制出来的形状有如下这几种,作为开发者我们一直想支持用户可以拖拽的类似word里面图形库,但目前还没有找到比 ...
- Linux命令之dot - 绘制DOT语言脚本描述的图形
本文链接:http://codingstandards.iteye.com/blog/840055 用途说明 Graphviz (Graph Visualization Software的缩写)是一个 ...
- Android自定义图形shape
在Android开发过程中,经常需要改变控件的默认样式, 那么通常会使用多个图片来解决.不过这种方式可能需要多个图片,比如一个按钮,需要点击时的式样图片,默认的式样图片. 这样就容易使apk变大.另一 ...
- linux启动后自动登录并运行自定义图形界面程序
在<Ubuntu CTRL+ALT+F1~F6 进入命令模式后不支持中文显示的解决办法>一文中提到linux启动在以后运行一个独占显示器的图形程序的两种办法. 1.不启动xserver,使 ...
- C# winform如何清除由Graphics类绘制出来的所有线条或图形
在C#winform应用程序中,可以用GDI绘制出线条或图形. 1.在主窗体上绘制线条或图形 using (Graphics g = this.CreateGraphics()) { ...
- Turtle绘制带颜色和字体的图形(Python3)
转载自https://blog.csdn.net/wumenglu1018/article/details/78184930 在Python中有很多编写图形程序的方法,一个简单的启动图形化程序设计的方 ...
随机推荐
- 航班座位_hihocoder
题目2 : 航班座位 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi在给一个家庭旅游团订某次航班的机票.该航班的飞机一共有N排座位,每排座位有10个座位,从左到右 ...
- 四、蛋炒饭(Egg fried rice)
蛋炒饭,是一种常见菜肴.最早的记载见于1972年湖南长沙马王堆汉墓出土的竹简上有关"卵火高"的资料.经专家考证,"卵熇"是一种用黏米饭加鸡蛋制成的食品.有人推断 ...
- json 的使用 Java对象转json
1. jsonlib:个人感觉最麻烦的一个需要导入的包也多,代码也相对多一些. 2.Gson:google的 3.FastJson:阿里巴巴的,个人觉得这个比较好,而且据说这个也是性能最好一个. 下面 ...
- VSTS 执行git pull报错问题修复
VSTS中进行双向同步配置的git pull指令如下: 运行时报错,Log如下图所示: 原因说的很清楚了,需要提前执行以下两条git config指令: git config --global use ...
- 一个开源的,跨平台的.NET机器学习框架ML.NET
微软在Build 2018大会上推出的一款面向.NET开发人员的开源,跨平台机器学习框架ML.NET. ML.NET将允许.NET开发人员开发他们自己的模型,并将自定义ML集成到他们的应用程序中,而无 ...
- JavaScript 异步编程的前世今生(下)
ES6 中的 Generator 在 ES6 出现之前,基本都是各式各样类似Promise的解决方案来处理异步操作的代码逻辑,但是 ES6 的Generator却给异步操作又提供了新的思路,马上就有人 ...
- [Swift]LeetCode91. 解码方法 | Decode Ways
A message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' - ...
- [Swift]LeetCode479. 最大回文数乘积 | Largest Palindrome Product
Find the largest palindrome made from the product of two n-digit numbers. Since the result could be ...
- IDEA 配置远程debug
1. 启动远程服务配置debug参数 远程服务启动时配置如下debug相关参数 java -Xdebug -Xrunjdwp:server=y,transport=dt_socket,address= ...
- Redis配置与安装
Redis 是一个非关系型高性能的key-value数据库.在部分场合可以对关系数据库起到很好的补充作用.它提供了Java,C/C++,C#,PHP,JavaScript,Perl,Object-C, ...