引言

在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(, ), 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图形——基本图形、自定义图形、色彩透明度的更多相关文章

  1. 使用Ogre::ManualObject 绘制自定义图形

    在ogre中如果需要进行自定义图形绘制可以使用ManualObject.例如绘制一个三角形的用法如下: SceneNode* pGridNode = m_pBaseNode->createChi ...

  2. [Xcode 实际操作]九、实用进阶-(19)重写父类的绘图方法,使用图形上下文绘制自定义图形

    目录:[Swift]Xcode实际操作 本文将演示如何使用图形上下文,绘制自定义图形. 使用快捷键[Command]+[N]创建一个新的类文件. (在项目文件夹[DemoApp]上点击鼠标右键[New ...

  3. opencv-7-鼠标绘制自定义图形

    opencv-7-鼠标绘制自定义图形 opencvc++qt 开始之前 昨天写了具体的基本的图形绘制, 然后我们使用相应的函数接口进行调用, 便能够在图像上绘制出来相应的图形, 我们以图像绘制为例, ...

  4. 软件项目技术点(7)——在canvas上绘制自定义图形

    AxeSlide软件项目梳理   canvas绘图系列知识点整理 图形种类 目前我们软件可以绘制出来的形状有如下这几种,作为开发者我们一直想支持用户可以拖拽的类似word里面图形库,但目前还没有找到比 ...

  5. Linux命令之dot - 绘制DOT语言脚本描述的图形

    本文链接:http://codingstandards.iteye.com/blog/840055 用途说明 Graphviz (Graph Visualization Software的缩写)是一个 ...

  6. Android自定义图形shape

    在Android开发过程中,经常需要改变控件的默认样式, 那么通常会使用多个图片来解决.不过这种方式可能需要多个图片,比如一个按钮,需要点击时的式样图片,默认的式样图片. 这样就容易使apk变大.另一 ...

  7. linux启动后自动登录并运行自定义图形界面程序

    在<Ubuntu CTRL+ALT+F1~F6 进入命令模式后不支持中文显示的解决办法>一文中提到linux启动在以后运行一个独占显示器的图形程序的两种办法. 1.不启动xserver,使 ...

  8. C# winform如何清除由Graphics类绘制出来的所有线条或图形

    在C#winform应用程序中,可以用GDI绘制出线条或图形. 1.在主窗体上绘制线条或图形 using (Graphics g = this.CreateGraphics())      {    ...

  9. Turtle绘制带颜色和字体的图形(Python3)

    转载自https://blog.csdn.net/wumenglu1018/article/details/78184930 在Python中有很多编写图形程序的方法,一个简单的启动图形化程序设计的方 ...

随机推荐

  1. 19南昌网络赛L

    校赛打杂没施展开. 题意:一开始给你一颗 (0,0)到(0,l)的树. 这棵树每一年会长出来三个幼芽(雾),长度均为l/4,方向分别是左转60,右转60,和不变. 年份<=14 考虑3^14直接 ...

  2. Spark 异步Action

    异步不保序,但大作业执行时间后移. .set("spark.scheduler.mode", "FAIR") 公平调度,充分使用集群资源. Spark Doc ...

  3. c# ef 排序字段动态,构建动态Lambda和扩展方法OrderBy

    1.动态构建排序 Lambda /// <summary> /// 获取排序Lambda(如果动态排序,类型不同会导致转换失败) /// </summary> /// < ...

  4. Dora.Interception,为.NET Core度身打造的AOP框架 [2]:以约定的方式定义拦截器

    上一篇<更加简练的编程体验>提供了最新版本的Dora.Interception代码的AOP编程体验,接下来我们会这AOP框架的编程模式进行详细介绍,本篇文章着重关注的是拦截器的定义.采用“ ...

  5. [Swift]LeetCode887. 鸡蛋掉落 | Super Egg Drop

    You are given K eggs, and you have access to a building with N floors from 1 to N. Each egg is ident ...

  6. [Swift]LeetCode934. 最短的桥 | Shortest Bridge

    In a given 2D binary array A, there are two islands.  (An island is a 4-directionally connected grou ...

  7. CentOS7 Linux中通过加密grub防止黑客通过单用户系统破解root密码

    如何防止别人恶意通过单用户系统破解root密码,进入系统窃取数据? 给grub加密,不让别人通过grub进入单用户. 17.3.1  基于centos6进行grub加密 [root@63 ~]# gr ...

  8. cassandra vs mongo (1)存储引擎

    摘要 在MongoDB 初识篇中谈到过Mongo 与 Cassandra的区别,这边再谈谈Mongo与Cassandra的存储引擎差别 概括 存储引擎: 类型 功能 应用 hash 增删改.随机读.顺 ...

  9. .NET Core 使用 HttpClient SSL 请求出错的解决办法

    问题 使用 HTTP Client 请求 HTTPS 的 API 时出现 The certificate cannot be verified up to a trusted certificatio ...

  10. Python 字典和集合基于哈希表实现

    哈希表作为基础数据结构我不多说,有兴趣的可以百度,或者等我出一篇博客来细谈哈希表.我这里就简单讲讲:哈希表不过就是一个定长数组,元素找位置,遇到哈希冲突则利用 hash 算法解决找另一个位置,如果数组 ...