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(),添加形状时,可设置形状类型.颜色.大小.位置.倾斜.轮廓.文本环绕 ...
随机推荐
- R语言grid包just参数如何just图形位置
思路 grid的画图函数都含有just,但是just参数的是怎么调节图形位置的总是让人非常费解,于是便写了代码来一探究竟. 思路非常简单:放一个2*2的布局viewport,每个布局里面放一个 ...
- JavaScript经典作用域问题(转载)
题目 var a = 10; function test(){ a = 100; console.log(a); console.log(this.a); var a; console.log(a); ...
- Spring 复习第一天
一.Sping的优点 1.方便解耦,简化开发 ----Spring就是一个大工厂,可以将所有对象创建和依赖关系维护,交给Spring管理. 2.AOP编程的支持 ----Spring提供面向切面编程, ...
- Super Jumping! Jumping! Jumping! ---HDU - 1087
Nowadays, a kind of chess game called “Super Jumping! Jumping! Jumping!” is very popular in HDU. May ...
- Android Studio 真机调试 连接手机
前提:adb环境已经配置 手机端: 1.打开手机开发者权限,”设置“ 中找到 “版本号”,连续多次点击,会提示打开“开发者”.我的是 “设置” --> "关于手机" --&g ...
- WARN [wxpay java sdk] - report fail. reason: report.mch.weixin.qq.com:80 failed to respond
最近小程序接口 tomcat日志经常出现此类警告 WARN [wxpay java sdk] - report fail. reason: report.mch.weixin.qq.com:80 fa ...
- 算法与数据结构(四) 图的物理存储结构与深搜、广搜(Swift版)
开门见山,本篇博客就介绍图相关的东西.图其实就是树结构的升级版.上篇博客我们聊了树的一种,在后边的博客中我们还会介绍其他类型的树,比如红黑树,B树等等,以及这些树结构的应用.本篇博客我们就讲图的存储结 ...
- 【RL-TCPnet网络教程】第13章 RL-TCPnet之TCP服务器
第13章 RL-TCPnet之TCP服务器 本章节为大家讲解RL-TCPnet的TCP服务器实现,学习本章节前,务必要优先学习第12章TCP传输控制协议基础知识.有了这些基础知识之后,再搞本 ...
- linux入门--Linux系统的优缺点
1) 大量的可用软件及免费软件 Linux 系统上有着大量的可用软件,且绝大多数是免费的,比如声名赫赫的 Apache.Samba.PHP.MySQL 等,构建成本低廉,是 Linux 被众多企业青睐 ...
- js 里面的键盘事件对应的键码
js 里面的键盘事件经常用到,所以收集了键盘事件对应的键码来分享下:keyCode 8 = BackSpace BackSpacekeyCode 9 = Tab TabkeyCode 12 = Cle ...