引言

在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. 自己封装element-ui树组件的过滤

    前言:vue开发项目时用到了element-ui的树组件,但是发现一执行过滤事件,树就全部都展开了,为了解决这个问题,只能自己先过滤数剧,再赋值给树组件的data,就避免了一上来全部展开的尴尬. 一. ...

  2. 基于jQuery的控件:弹框

    ★页面展示 ★属性 属性 值 说明 默认值 div Object jQuery对象 $('body') width Number 控件的宽度 auto height Number 控件的高度 auto ...

  3. python从入门到实践-5章if语句

    #!/user/bin/env python cars = ['audi','bmw','subaru','toyota']for car in cars: if car == 'bmw': prin ...

  4. react基础篇 整理(一)

    备注不知道为啥不能到出图片,详细知识自己百度一下就可以了,很简单的.画这个是为了更好的梳理知识,公司有个App项目,项目可控,所以尝试一下用React-native去做一下试试.

  5. ndk编译libx264生成库

    编译脚本如下: TOOLCHAIN=$NDK/toolchains/arm-linux-androideabi-4.6/prebuilt/linux-x86_64 function build_x26 ...

  6. C#单元测试,带你快速入门

    注:本文示例环境 VS2017 XUnit 2.2.0 单元测试框架 xunit.runner.visualstudio 2.2.0 测试运行工具 Moq 4.7.10 模拟框架 为什么要编写单元测试 ...

  7. C++或C#调用外部exe的分析

    假如有个外部程序名为A.exe,放在目录E:\temp\下,然后我们用C++或者C#写一个程序调用这个A.exe的话(假设这个调用者所在的路径在D:\invoke),通常会采用下面的代码: // C# ...

  8. 消息队列RabbitMq、ActiveMq、ZeroMq、kafka之间的比较

    MQ框架非常之多,比较流行的有RabbitMq.ActiveMq.ZeroMq.kafka.这几种MQ到底应该选择哪个?要根据自己项目的业务场景和需求.下面我列出这些MQ之间的对比数据和资料. 第一部 ...

  9. [WEB]绕过安全狗与360PHP一句话的编写

    00x01安全狗的确是让人很头痛,尤其是在上传一句话或者写入一句话的时候,会被安全狗拦截从而拿不下shell.当然,安全狗是最简单的一款waf,很容易就进行一个绕过.00x02对于绕过安全狗跟360, ...

  10. [SQL]LeetCode626. 换座位 | Exchange Seats

    SQL架构 Create table If Not Exists seat(id )) Truncate table seat insert into seat (id, student) value ...