C# 绘制Word形状——基本形状、组合形状
一、序言
在Office Word中,支持在Word文档中插入类型非常丰富的形状,包括线条、矩形、基本形状(诸如圆形、多边形、星形、括号、笑脸等等图形)、箭头形状、公式形状、流程图、旗帜图形、标注图形等等,我们在编程过程中,想要在Word中绘制不同类型的图形,可以通过类库来操作。控件Spire.Doc for .NET 6.0及以上版本开始支持Office Word中的所有图形,可以通过代码操作某个单一的形状,也可以通过将单一形状进行组合来获得想要的图形或形状效果,当然,也支持自己自定义图形,通过编程绘制也是可以的。下面将介绍向Word绘制形状和组合形状的方法,方法中的代码供参考。
PS:
- Spire.Doc for .NET获取地址
- 安装后,dll文件可在安装路径下的Bin文件夹中获取
Dll引用

二、代码示例
(一)绘制单一形状
步骤1:添加如下using指定
using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
using System.Drawing;
步骤2:创建示例,添加section、paragraph
//创建一个Document实例
Document doc = new Document();
//添加一个section paragraph
Section sec = doc.AddSection();
Paragraph para1 = sec.AddParagraph();
步骤3:在文档指定位置插入形状,并设置形状类型、大小、填充颜色、线条样式等
(这里简单列举几个形状的添加方法,方法比较简单,不做赘述,效果图中列举了部分形状样式,需要其他样式的形状可自行设置添加)
//插入一个矩形
ShapeObject shape1 = para1.AppendShape(, , ShapeType.Rectangle);
shape1.FillColor = Color.Blue;
shape1.StrokeColor = Color.LightSkyBlue;
shape1.HorizontalPosition = ;
shape1.VerticalPosition = ; //插入一个圆形
ShapeObject shape2 = para1.AppendShape(, , ShapeType.Ellipse);
shape2.FillColor = Color.Purple;
shape2.StrokeColor = Color.LightPink;
shape2.LineStyle = ShapeLineStyle.Single;
shape2.StrokeWeight = ;
shape2.HorizontalPosition = ;
shape2.VerticalPosition = ; //插入一个公式符号 +
ShapeObject shape3 = para1.AppendShape(, , ShapeType.Plus);
shape3.FillColor = Color.DarkCyan;
shape3.StrokeColor = Color.LightGreen;
shape3.LineStyle = ShapeLineStyle.Single;
shape3.StrokeWeight = ;
shape3.HorizontalPosition = ;
shape3.VerticalPosition = ; //插入一颗星形
ShapeObject shape4 = para1.AppendShape(, , ShapeType.Star);
shape4.FillColor = Color.Red;
shape4.StrokeColor = Color.Gold;
shape4.LineStyle = ShapeLineStyle.Single;
shape4.HorizontalPosition = ;
shape4.VerticalPosition = ;
步骤4:保存文档
//保存并打开文档
doc.SaveToFile("InsertShapes.docx", FileFormat.Docx2010);
System.Diagnostics.Process.Start("InsertShapes.docx");
形状添加效果:

全部代码:
using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
using System.Drawing; namespace AddShapes_Doc
{
class Program
{
static void Main(string[] args)
{
//创建一个Document实例
Document doc = new Document(); //添加一个section paragraph
Section sec = doc.AddSection();
Paragraph para1 = sec.AddParagraph(); //插入一个矩形
ShapeObject shape1 = para1.AppendShape(, , ShapeType.Rectangle);
shape1.FillColor = Color.Blue;
shape1.StrokeColor = Color.LightSkyBlue;
shape1.HorizontalPosition = ;
shape1.VerticalPosition = ; //插入一个圆形
ShapeObject shape2 = para1.AppendShape(, , ShapeType.Ellipse);
shape2.FillColor = Color.Purple;
shape2.StrokeColor = Color.LightPink;
shape2.LineStyle = ShapeLineStyle.Single;
shape2.StrokeWeight = ;
shape2.HorizontalPosition = ;
shape2.VerticalPosition = ; //插入一个公式符号 +
ShapeObject shape3 = para1.AppendShape(, , ShapeType.Plus);
shape3.FillColor = Color.DarkCyan;
shape3.StrokeColor = Color.LightGreen;
shape3.LineStyle = ShapeLineStyle.Single;
shape3.StrokeWeight = ;
shape3.HorizontalPosition = ;
shape3.VerticalPosition = ; //插入一颗星形
ShapeObject shape4 = para1.AppendShape(, , ShapeType.Star);
shape4.FillColor = Color.Red;
shape4.StrokeColor = Color.Gold;
shape4.LineStyle = ShapeLineStyle.Single;
shape4.HorizontalPosition = ;
shape4.VerticalPosition = ; //插入一个立方体
ShapeObject shape5 = para1.AppendShape(, , ShapeType.Cube);
shape5.FillColor = Color.OrangeRed;
shape5.StrokeColor = Color.Orange;
shape5.LineStyle = ShapeLineStyle.Single;
shape5.HorizontalPosition = ;
shape5.VerticalPosition = ; //插入一个圆柱体
ShapeObject shape6 = para1.AppendShape(, , ShapeType.Can);
shape6.FillColor = Color.Goldenrod;
shape6.StrokeColor = Color.Gold;
shape6.LineStyle = ShapeLineStyle.Single;
shape6.HorizontalPosition = ;
shape6.VerticalPosition = ; //插入一个箭头
ShapeObject shape7 = para1.AppendShape(, , ShapeType.Arrow);
shape7.FillColor = Color.Yellow;
shape7.StrokeColor = Color.Yellow;
shape7.LineStyle = ShapeLineStyle.Single;
shape7.HorizontalPosition = ;
shape7.VerticalPosition = ; //插入一个v形臂章图形
ShapeObject shape8 = para1.AppendShape(, , ShapeType.Chevron);
shape8.FillColor = Color.YellowGreen;
shape8.StrokeColor = Color.Yellow;
shape8.LineStyle = ShapeLineStyle.Single;
shape8.HorizontalPosition = ;
shape8.VerticalPosition = ; //插入一个循环箭头图形
ShapeObject shape9 = para1.AppendShape(, , ShapeType.CircularArrow);
shape9.FillColor = Color.Green;
shape9.StrokeColor = Color.Yellow;
shape9.LineStyle = ShapeLineStyle.Single;
shape9.HorizontalPosition = ;
shape9.VerticalPosition = ; //插入一个云图形
ShapeObject shape10 = para1.AppendShape(, , ShapeType.CloudCallout);
shape10.FillColor = Color.LightSkyBlue;
shape10.StrokeColor = Color.White;
shape10.LineStyle = ShapeLineStyle.Single;
shape10.HorizontalPosition = ;
shape10.VerticalPosition = ; //插入一个环形图
ShapeObject shape11 = para1.AppendShape(, , ShapeType.Donut);
shape11.FillColor = Color.Pink;
shape11.StrokeColor = Color.White;
shape11.LineStyle = ShapeLineStyle.Single;
shape11.HorizontalPosition = ;
shape11.VerticalPosition = ; //插入一个波浪形状图
ShapeObject shape12 = para1.AppendShape(, , ShapeType.DoubleWave);
shape12.FillColor = Color.Plum;
shape12.StrokeColor = Color.White;
shape12.LineStyle = ShapeLineStyle.Single;
shape12.HorizontalPosition = ;
shape12.VerticalPosition = ; //插入一个礼结状图形
ShapeObject shape13 = para1.AppendShape(, , ShapeType.EllipseRibbon);
shape13.FillColor = Color.RosyBrown;
shape13.StrokeColor = Color.White;
shape13.LineStyle = ShapeLineStyle.Single;
shape13.HorizontalPosition = ;
shape13.VerticalPosition = ; //插入一个心形图
ShapeObject shape14 = para1.AppendShape(, , ShapeType.Heart);
shape14.FillColor = Color.Red;
shape14.StrokeColor = Color.White;
shape14.LineStyle = ShapeLineStyle.Single;
shape14.HorizontalPosition = ;
shape14.VerticalPosition = ; //插入一个六边形图形
ShapeObject shape15 = para1.AppendShape(, , ShapeType.Hexagon);
shape15.FillColor = Color.DarkCyan;
shape15.StrokeColor = Color.White;
shape15.LineStyle = ShapeLineStyle.Single;
shape15.HorizontalPosition = ;
shape15.VerticalPosition = ; //插入一个不规则图形
ShapeObject shape16 = para1.AppendShape(, , ShapeType.IrregularSeal1);
shape16.FillColor = Color.DeepPink;
shape16.StrokeColor = Color.White;
shape16.LineStyle = ShapeLineStyle.Single;
shape16.HorizontalPosition = ;
shape16.VerticalPosition = ; //插入一个月亮形状
ShapeObject shape17 = para1.AppendShape(, , ShapeType.Moon);
shape17.FillColor = Color.Violet;
shape17.StrokeColor = Color.White;
shape17.LineStyle = ShapeLineStyle.Single;
shape17.HorizontalPosition = ;
shape17.VerticalPosition = ; //插入一个"禁止"形状
ShapeObject shape18 = para1.AppendShape(, , ShapeType.NoSmoking);
shape18.FillColor = Color.Yellow;
shape18.StrokeColor = Color.Goldenrod;
shape18.LineStyle = ShapeLineStyle.Single;
shape18.HorizontalPosition = ;
shape18.VerticalPosition = ; //保存并打开文档
doc.SaveToFile("InsertShapes.docx", FileFormat.Docx2010);
System.Diagnostics.Process.Start("InsertShapes.docx");
}
}
}
(二)添加组合形状
步骤1:添加如下using指令
using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
using System.Drawing;
步骤2:创建文档,添加section、paragraph
Document doc = new Document();
Section sec = doc.AddSection();
Paragraph para1 = sec.AddParagraph();
步骤3:添加文字,并应用格式到文字
para1.AppendText("中日文化交流");
ParagraphStyle style1 = new ParagraphStyle(doc);
style1.Name = "titleStyle";
style1.CharacterFormat.Bold = true;
style1.CharacterFormat.FontName = "隶书";
style1.CharacterFormat.FontSize = 30f;
doc.Styles.Add(style1);
para1.ApplyStyle("titleStyle");
para1.Format.HorizontalAlignment = HorizontalAlignment.Center;
步骤4:实例化段落2,并创建一个形状组合,并设置大小
//实例化段落2
Paragraph para2 = sec.AddParagraph();
//创建一个形状组合并设置大小
ShapeGroup shapegr = para2.AppendShapeGroup(, );
步骤5:绘制一个中国国旗,这里需要组合形状矩形和五角星形,并填充相应的颜色
//添加一个矩形到形状组合
shapegr.ChildObjects.Add(new ShapeObject(doc, ShapeType.Rectangle)
{
Width = ,
Height = ,
LineStyle = ShapeLineStyle.Single,
FillColor = Color.Red,
StrokeColor = Color.Red,
StrokeWeight = ,
}); //添加第一个五角星到形状组合
shapegr.ChildObjects.Add(new ShapeObject(doc, ShapeType.Star)
{
Width = ,
Height = ,
VerticalPosition = ,
HorizontalPosition = ,
LineStyle = ShapeLineStyle.Single,
FillColor = Color.Yellow,
StrokeColor = Color.Yellow,
StrokeWeight = ,
});
//添加第二个五角星到形状组合
shapegr.ChildObjects.Add(new ShapeObject(doc, ShapeType.Star)
{
Width = ,
Height = ,
VerticalPosition = ,
HorizontalPosition = ,
LineStyle = ShapeLineStyle.Single,
FillColor = Color.Yellow,
StrokeColor = Color.Yellow,
StrokeWeight = ,
});
//添加第三个五角星到形状组合
shapegr.ChildObjects.Add(new ShapeObject(doc, ShapeType.Star)
{
Width = ,
Height = ,
VerticalPosition = ,
HorizontalPosition = ,
LineStyle = ShapeLineStyle.Single,
FillColor = Color.Yellow,
StrokeColor = Color.Yellow,
StrokeWeight = ,
});
//添加第四个五角星到形状组合
shapegr.ChildObjects.Add(new ShapeObject(doc, ShapeType.Star)
{
Width = ,
Height = ,
VerticalPosition = ,
HorizontalPosition = ,
LineStyle = ShapeLineStyle.Single,
FillColor = Color.Yellow,
StrokeColor = Color.Yellow,
StrokeWeight = ,
});
//添加第五个五角星到形状组合
shapegr.ChildObjects.Add(new ShapeObject(doc, ShapeType.Star)
{
Width = ,
Height = ,
VerticalPosition = ,
HorizontalPosition = ,
LineStyle = ShapeLineStyle.Single,
FillColor = Color.Yellow,
StrokeColor = Color.Yellow,
StrokeWeight = ,
});
步骤6:绘制一个日本国旗,需要组合形状矩形和圆形,并填充颜色
//绘制一个矩形并添加到形状组合
shapegr.ChildObjects.Add(new ShapeObject(doc, ShapeType.Rectangle)
{
Width = ,
Height = ,
VerticalPosition = ,
HorizontalPosition = ,
LineStyle = ShapeLineStyle.Single,
FillColor = Color.WhiteSmoke,
StrokeColor = Color.WhiteSmoke,
StrokeWeight = ,
});
//绘制一个圆形并添加到形状组合
shapegr.ChildObjects.Add(new ShapeObject(doc, ShapeType.Ellipse)
{
Width = ,
Height = ,
VerticalPosition = ,
HorizontalPosition = ,
LineStyle = ShapeLineStyle.Single,
FillColor = Color.Red,
StrokeColor = Color.Red,
StrokeWeight = ,
});
步骤7:保存文档
//保存并打开文档
doc.SaveToFile("InsertShapegroups.docx", FileFormat.Docx2010);
System.Diagnostics.Process.Start("InsertShapegroups.docx");
添加效果:

(此时的图形是组合后的效果,任意拖动图形不会出现各个形状分离、错位的情况。)
全部代码:
using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
using System.Drawing; namespace InsertShapesGroup_Doc
{
class Program
{
static void Main(string[] args)
{
//创建一个Document实例并添加section及paragraph
Document doc = new Document();
Section sec = doc.AddSection();
Paragraph para1 = sec.AddParagraph();
//添加文字,并应用格式到文字
para1.AppendText("中日文化交流");
ParagraphStyle style1 = new ParagraphStyle(doc);
style1.Name = "titleStyle";
style1.CharacterFormat.Bold = true;
style1.CharacterFormat.FontName = "隶书";
style1.CharacterFormat.FontSize = 30f;
doc.Styles.Add(style1);
para1.ApplyStyle("titleStyle");
para1.Format.HorizontalAlignment = HorizontalAlignment.Center; //实例化段落2
Paragraph para2 = sec.AddParagraph();
//创建一个形状组合并设置大小
ShapeGroup shapegr = para2.AppendShapeGroup(, ); //添加一个矩形到形状组合
shapegr.ChildObjects.Add(new ShapeObject(doc, ShapeType.Rectangle)
{
Width = ,
Height = ,
LineStyle = ShapeLineStyle.Single,
FillColor = Color.Red,
StrokeColor = Color.Red,
StrokeWeight = ,
}); //添加第一个五角星到形状组合
shapegr.ChildObjects.Add(new ShapeObject(doc, ShapeType.Star)
{
Width = ,
Height = ,
VerticalPosition = ,
HorizontalPosition = ,
LineStyle = ShapeLineStyle.Single,
FillColor = Color.Yellow,
StrokeColor = Color.Yellow,
StrokeWeight = ,
});
//添加第二个五角星到形状组合
shapegr.ChildObjects.Add(new ShapeObject(doc, ShapeType.Star)
{
Width = ,
Height = ,
VerticalPosition = ,
HorizontalPosition = ,
LineStyle = ShapeLineStyle.Single,
FillColor = Color.Yellow,
StrokeColor = Color.Yellow,
StrokeWeight = ,
});
//添加第三个五角星到形状组合
shapegr.ChildObjects.Add(new ShapeObject(doc, ShapeType.Star)
{
Width = ,
Height = ,
VerticalPosition = ,
HorizontalPosition = ,
LineStyle = ShapeLineStyle.Single,
FillColor = Color.Yellow,
StrokeColor = Color.Yellow,
StrokeWeight = ,
});
//添加第四个五角星到形状组合
shapegr.ChildObjects.Add(new ShapeObject(doc, ShapeType.Star)
{
Width = ,
Height = ,
VerticalPosition = ,
HorizontalPosition = ,
LineStyle = ShapeLineStyle.Single,
FillColor = Color.Yellow,
StrokeColor = Color.Yellow,
StrokeWeight = ,
});
//添加第五个五角星到形状组合
shapegr.ChildObjects.Add(new ShapeObject(doc, ShapeType.Star)
{
Width = ,
Height = ,
VerticalPosition = ,
HorizontalPosition = ,
LineStyle = ShapeLineStyle.Single,
FillColor = Color.Yellow,
StrokeColor = Color.Yellow,
StrokeWeight = ,
}); //绘制一个矩形并添加到形状组合
shapegr.ChildObjects.Add(new ShapeObject(doc, ShapeType.Rectangle)
{
Width = ,
Height = ,
VerticalPosition = ,
HorizontalPosition = ,
LineStyle = ShapeLineStyle.Single,
FillColor = Color.WhiteSmoke,
StrokeColor = Color.Wheat,
StrokeWeight = ,
});
//绘制一个圆形并添加到形状组合
shapegr.ChildObjects.Add(new ShapeObject(doc, ShapeType.Ellipse)
{
Width = ,
Height = ,
VerticalPosition = ,
HorizontalPosition = ,
LineStyle = ShapeLineStyle.Single,
FillColor = Color.Red,
StrokeColor = Color.Red,
StrokeWeight = ,
}); //保存并打开文档
doc.SaveToFile("InsertShapegroups.docx", FileFormat.Docx2010);
System.Diagnostics.Process.Start("InsertShapegroups.docx");
}
}
}
以上全部是关于Word中绘制图形形状的内容。如需转载,请注明出处!
感谢阅读!
C# 绘制Word形状——基本形状、组合形状的更多相关文章
- Android中绘制圆角矩形图片及任意形状图片
圆角矩形图片在苹果的产品中很流行,相比于普通的矩形,很多人都喜欢圆角矩形的图片,因为它避开了直角的生硬,带来更好的用户体验,下面是几个设计的例子: 下面在Android中实现将普通的矩形图片绘制成圆角 ...
- 用CSS绘制最常见的40种形状和图形
今天在国外的网站上看到了很多看似简单却又非常强大的纯CSS绘制的图形,里面有最简单的矩形.圆形和三角形,也有各种常见的多边形,甚至是阴阳太极和网站小图标,真的非常强大,分享给大家. Square(正方 ...
- MFC 鼠标 移动到某控件时 修改鼠标形状为手的形状
响应窗体的 OnSetCursor 消息响应 鼠标移动到某空间时改变 形状 BOOL CQQBulkDlg::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT m ...
- 纯CSS3绘制的黑色图标按钮组合
在线演示 本地下载
- C# 在PPT中绘制形状(shape)
概述 本篇文章将介绍C# 在PPT幻灯片中操作形状(shape)的方法.这里主要涉及常规形状,如箭头.矩形.圆形.三角形.多边形.不规则形状等.下面的示例中,可以通过绘制形状,并设置相应格式等.示例包 ...
- Java 添加Word形状或图形
本文将介绍通过java编程在Word文档中添加形状(图形),包括添加单个图形.组合图形,以及格式化图形样式,如设置形状填充色.大小.位置.边框样式.边框颜色.边框粗细.图形旋转角度.图形文本环绕方式等 ...
- Android OpenGL ES 开发(四): OpenGL ES 绘制形状
在上文中,我们使用OpenGL定义了能够被绘制出来的形状了,现在我们想绘制出来它们.使用OpenGLES 2.0来绘制形状会比你想象的需要更多的代码.因为OpenGL的API提供了大量的对渲染管线的控 ...
- (一)在 Blend 中绘制形状和路径
原文:(一)在 Blend 中绘制形状和路径 https://docs.microsoft.com/zh-cn/previous-versions/jj170881(v=vs.120) 在 Blend ...
- C# 添加、删除、读取Word形状(基于Spire.Cloud.Word.SDK)
本文介绍调用Spire.Cloud.Word.SDK提供的接口shapesApi来操作Word形状,包括添加形状AddShape(),添加形状时,可设置形状类型.颜色.大小.位置.倾斜.轮廓.文本环绕 ...
随机推荐
- 菜鸡谈OO 第二单元总结
“欢迎来到(玄学)多线程的新世界” Homework1 单部傻瓜电梯调度 Part1 多线程设计策略 第一次学到了线程这个概念,与之前的编程体验大有不同.最大的区别在于从原本的线性发生程序变成了多个行 ...
- Reactjs项目性能优化
在construct中绑定函数this shouldComponentUpdate React.PureComponent 无状态组件 chrome浏览器性能优化工具 setTimeout,setIn ...
- 基于js的数据结构与算法-数组
Given an array of integers, return indices of the two numbers such that they add up to a specific ta ...
- RabbitMQ in Action (1): Understanding messaging
1. Consumers and producers Producers create messages and publish (send) them to a broker server (Rab ...
- RabbitMQ 集群原理和完善
一.RabbitMQ集群方案的原理 RabbitMQ这款消息队列中间件产品本身是基于Erlang编写,Erlang语言天生具备分布式特性(通过同步Erlang集群各节点的magic cookie来实现 ...
- 算法与数据结构(十) 二叉排序树的查找、插入与删除(Swift版)
在上一篇博客中,我们主要介绍了四种查找的方法,包括顺序查找.折半查找.插入查找以及Fibonacci查找.上面这几种查找方式都是基于线性表的查找方式,今天博客中我们来介绍一下基于二叉树结构的查找,也就 ...
- vue项目实践-添加axios封装api请求
安装 axios npm install axios --save 创建实例 (utils/fetch.js) axios 默认提交格式为:application/json 可使用 qs 模块(需要安 ...
- [Swift]LeetCode311. 稀疏矩阵相乘 $ Sparse Matrix Multiplication
Given two sparse matrices A and B, return the result of AB. You may assume that A's column number is ...
- [Swift]LeetCode620. 有趣的电影 | Not Boring Movies
SQL架构 Create table If Not Exists cinema (id ), description varchar(), rating , )) Truncate table cin ...
- [Swift]LeetCode640. 求解方程 | Solve the Equation
Solve a given equation and return the value of x in the form of string "x=#value". The equ ...