引言

在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. JavaScript经典作用域问题(转载)

    题目 var a = 10; function test(){ a = 100; console.log(a); console.log(this.a); var a; console.log(a); ...

  2. mybatis3源码阅读之SqlSessionFactoryBuilder

    /** 构造器,根据配置或者代码生成SqlSessionFactory,采用分布构建的Builder模式 /* public class SqlSessionFactoryBuilder { /** ...

  3. framework7 入门(基础布局)

    个人认为framework7是个很好的移动端框架,自带路由.下拉刷新.无限加载.时间线.各种dialog.环形图等,基本能满足大部分app的需求,可以和vue,webpack 等各种搭配. 这里讲一下 ...

  4. Ubuntu ARM更改为国内源

    关键词:ubuntu  arm  ubuntu-ports  国内源  镜像  阿里源  apt apt-get install update  0%working  速度慢  rk3399  开发板 ...

  5. Ajax实现聊天室

    Ajax实现聊天室 运行效果如下: 代码显示: var net=new Object();//编写构造函数net.AjaxRequest=function(url,onload,onerror,met ...

  6. NodeJS NPM 镜像使用方法

    每次npm的时候,走国外的镜像,非常的慢,可以配置一下 通过改变默认npm镜像代理服务,以下三种办法任意一种都能解决问题,建议使用第三种,将配置写死,下次用的时候不用重新配置. 通过config命令 ...

  7. Edge-assisted Traffic Engineering and applications in the IoT

    物联网中边缘辅助的流量工程和应用 本文为SIGCOMM 2018 Workshop (Mobile Edge Communications, MECOMM)论文. 笔者翻译了该论文.由于时间仓促,且笔 ...

  8. .NET Core 源码导航(按程序集链接)

    System.*.dll/dotnetfx mscorlib.dll/dotnetclr Microsoft.AspNetCore.dll Microsoft.EntityFrameworkCore. ...

  9. Java面试大纲-java面试该做哪些准备,java开发达到这样的水平可以涨工资

    Java培训结束,面临的就是毕业找工作.在找工作时,就要针对性地做充分的面试准备.准备不充分的面试,完全是浪费时间,更是对自己的不负责. 上海尚学堂Java培训整理出Java面试大纲,其中大部分都是面 ...

  10. [Swift]LeetCode190. 颠倒二进制位 | Reverse Bits

    Reverse bits of a given 32 bits unsigned integer. Example 1: Input: 00000010100101000001111010011100 ...