一、序言

在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形状——基本形状、组合形状的更多相关文章

  1. Android中绘制圆角矩形图片及任意形状图片

    圆角矩形图片在苹果的产品中很流行,相比于普通的矩形,很多人都喜欢圆角矩形的图片,因为它避开了直角的生硬,带来更好的用户体验,下面是几个设计的例子: 下面在Android中实现将普通的矩形图片绘制成圆角 ...

  2. 用CSS绘制最常见的40种形状和图形

    今天在国外的网站上看到了很多看似简单却又非常强大的纯CSS绘制的图形,里面有最简单的矩形.圆形和三角形,也有各种常见的多边形,甚至是阴阳太极和网站小图标,真的非常强大,分享给大家. Square(正方 ...

  3. MFC 鼠标 移动到某控件时 修改鼠标形状为手的形状

    响应窗体的 OnSetCursor 消息响应 鼠标移动到某空间时改变 形状 BOOL CQQBulkDlg::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT m ...

  4. 纯CSS3绘制的黑色图标按钮组合

    在线演示 本地下载

  5. C# 在PPT中绘制形状(shape)

    概述 本篇文章将介绍C# 在PPT幻灯片中操作形状(shape)的方法.这里主要涉及常规形状,如箭头.矩形.圆形.三角形.多边形.不规则形状等.下面的示例中,可以通过绘制形状,并设置相应格式等.示例包 ...

  6. Java 添加Word形状或图形

    本文将介绍通过java编程在Word文档中添加形状(图形),包括添加单个图形.组合图形,以及格式化图形样式,如设置形状填充色.大小.位置.边框样式.边框颜色.边框粗细.图形旋转角度.图形文本环绕方式等 ...

  7. Android OpenGL ES 开发(四): OpenGL ES 绘制形状

    在上文中,我们使用OpenGL定义了能够被绘制出来的形状了,现在我们想绘制出来它们.使用OpenGLES 2.0来绘制形状会比你想象的需要更多的代码.因为OpenGL的API提供了大量的对渲染管线的控 ...

  8. (一)在 Blend 中绘制形状和路径

    原文:(一)在 Blend 中绘制形状和路径 https://docs.microsoft.com/zh-cn/previous-versions/jj170881(v=vs.120) 在 Blend ...

  9. C# 添加、删除、读取Word形状(基于Spire.Cloud.Word.SDK)

    本文介绍调用Spire.Cloud.Word.SDK提供的接口shapesApi来操作Word形状,包括添加形状AddShape(),添加形状时,可设置形状类型.颜色.大小.位置.倾斜.轮廓.文本环绕 ...

随机推荐

  1. json 的使用 Java对象转json

    1. jsonlib:个人感觉最麻烦的一个需要导入的包也多,代码也相对多一些. 2.Gson:google的 3.FastJson:阿里巴巴的,个人觉得这个比较好,而且据说这个也是性能最好一个. 下面 ...

  2. 简单工厂模式demo

    1. 简单工厂模式 domain的接口 public interface Color{ public void display(); } red public Class Red implements ...

  3. 【mysql】must reset your password using ALTER USER statement before executing this statement

    问题描述: must reset your password using ALTER USER statement before executing this statement 登录centos服务 ...

  4. three.js的wave特效(ivew官网首页波浪特效实现)

    查看效果请访问:https://521lbx.github.io/Web3D/index.html公司的好几个vue项目都是用ivew作为UI框架,所以ivew官网时不时就得逛一圈.每一次进首页都会被 ...

  5. mantisbt的配置与安装

    下载并安装wampserver; 安装时,提示SMTP服务器时,应正确填写邮箱的SMTP的服务器地址: 安装完成后,登录phpMyAdmin; 给原有的root用户创建密码,所有root用户: 创建一 ...

  6. 字符串----HDU-1358

    题目大意:求字符串的前缀是否为周期串,若是,打印出循环节的长度以及循环次数. 这道题考察的是KMP算法中next数组的应用,必须理解透next[]数组代表的含义才t能通过它解决这道题.思路是先构造出 ...

  7. SUSE12SP3-Mysql5.7安装

    1.将以下安装包复制到服务器 mysql-community-client-5.7.24-1.sles12.x86_64.rpm mysql-community-server-5.7.24-1.sle ...

  8. 阿里云对象存储 OSS 应用服务器搭建代码

    背景说明 最近做一个APP客户端图片直传阿里云OSS的服务,需要在后台开一个阿里云的OSSToken获取的接口. 阿里云官方文档地址:快速搭建移动应用直传服务. 略过移动端说明,直接看服务端的. 不是 ...

  9. 在vue中使用svg sprite

    概述 这几天研究了一下在vue中使用svg sprite,有些心得,记录下来,供以后开发时参考,相信对其它人也有用. 在vue中导入svg 在vue中导入svg的方法有很多种,比如在img标签的src ...

  10. [Swift]LeetCode217. 存在重复元素 | Contains Duplicate

    Given an array of integers, find if the array contains any duplicates. Your function should return t ...