在Office Word中,支持在Word文档中插入类型非常丰富的形状,包括线条、矩形、基本形状(诸如圆形、多边形、星形、括号、笑脸等等图形)、箭头形状、公式形状、流程图、旗帜图形、标注图形等等,我们在编程过程中,想要在Word中绘制不同类型的图形,可以通过类库来操作。控件Spire.Doc for .NET 6.0及以上版本开始支持Office Word中的所有图形,可以通过代码操作某个单一的形状,也可以通过将单一形状进行组合来获得想要的图形或形状效果,当然,也支持自己自定义图形,通过编程绘制也是可以的。下面将介绍向Word绘制形状和组合形状的方法,方法中的代码供参考。
创新互联公司从2013年开始,先为建瓯等服务建站,建瓯等地企业,进行企业商务咨询服务。为建瓯企业网站制作PC+手机+微官网三网同步一站式服务解决您的所有建站问题。PS:
步骤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(50, 50, ShapeType.Rectangle);
shape1.FillColor = Color.Blue;
shape1.StrokeColor = Color.LightSkyBlue;
shape1.HorizontalPosition = 20;
shape1.VerticalPosition = 20;
//插入一个圆形
ShapeObject shape2 = para1.AppendShape(50, 50, ShapeType.Ellipse);
shape2.FillColor = Color.Purple;
shape2.StrokeColor = Color.LightPink;
shape2.LineStyle = ShapeLineStyle.Single;
shape2.StrokeWeight = 1;
shape2.HorizontalPosition = 80;
shape2.VerticalPosition = 20;
//插入一个公式符号 +
ShapeObject shape3 = para1.AppendShape(50, 50, ShapeType.Plus);
shape3.FillColor = Color.DarkCyan;
shape3.StrokeColor = Color.LightGreen;
shape3.LineStyle = ShapeLineStyle.Single;
shape3.StrokeWeight = 1;
shape3.HorizontalPosition = 140;
shape3.VerticalPosition = 20;
//插入一颗星形
ShapeObject shape4 = para1.AppendShape(50, 50, ShapeType.Star);
shape4.FillColor = Color.Red;
shape4.StrokeColor = Color.Gold;
shape4.LineStyle = ShapeLineStyle.Single;
shape4.HorizontalPosition = 200;
shape4.VerticalPosition = 20;
步骤4:保存文档
//保存并打开文档
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(300, 300);
步骤5:绘制一个中国国旗,这里需要组合形状矩形和五角星形,并填充相应的颜色
//添加一个矩形到形状组合
shapegr.ChildObjects.Add(new ShapeObject(doc, ShapeType.Rectangle)
{
Width = 900,
Height = 500,
LineStyle = ShapeLineStyle.Single,
FillColor = Color.Red,
StrokeColor = Color.Red,
StrokeWeight = 1,
});
//添加第一个五角星到形状组合
shapegr.ChildObjects.Add(new ShapeObject(doc, ShapeType.Star)
{
Width = 100,
Height = 100,
VerticalPosition = 90,
HorizontalPosition = 90,
LineStyle = ShapeLineStyle.Single,
FillColor = Color.Yellow,
StrokeColor = Color.Yellow,
StrokeWeight = 1,
});
//添加第二个五角星到形状组合
shapegr.ChildObjects.Add(new ShapeObject(doc, ShapeType.Star)
{
Width = 50,
Height = 50,
VerticalPosition = 40,
HorizontalPosition = 210,
LineStyle = ShapeLineStyle.Single,
FillColor = Color.Yellow,
StrokeColor = Color.Yellow,
StrokeWeight = 1,
});
//添加第三个五角星到形状组合
shapegr.ChildObjects.Add(new ShapeObject(doc, ShapeType.Star)
{
Width = 50,
Height = 50,
VerticalPosition = 80,
HorizontalPosition = 280,
LineStyle = ShapeLineStyle.Single,
FillColor = Color.Yellow,
StrokeColor = Color.Yellow,
StrokeWeight = 1,
});
//添加第四个五角星到形状组合
shapegr.ChildObjects.Add(new ShapeObject(doc, ShapeType.Star)
{
Width = 50,
Height = 50,
VerticalPosition = 160,
HorizontalPosition = 280,
LineStyle = ShapeLineStyle.Single,
FillColor = Color.Yellow,
StrokeColor = Color.Yellow,
StrokeWeight = 1,
});
//添加第五个五角星到形状组合
shapegr.ChildObjects.Add(new ShapeObject(doc, ShapeType.Star)
{
Width = 50,
Height = 50,
VerticalPosition = 220,
HorizontalPosition = 210,
LineStyle = ShapeLineStyle.Single,
FillColor = Color.Yellow,
StrokeColor = Color.Yellow,
StrokeWeight = 1,
});
步骤6:绘制一个日本国旗,需要组合形状矩形和圆形,并填充颜色
//绘制一个矩形并添加到形状组合
shapegr.ChildObjects.Add(new ShapeObject(doc, ShapeType.Rectangle)
{
Width = 900,
Height = 500,
VerticalPosition = 700,
HorizontalPosition = 600,
LineStyle = ShapeLineStyle.Single,
FillColor = Color.WhiteSmoke,
StrokeColor = Color.WhiteSmoke,
StrokeWeight = 1,
});
//绘制一个圆形并添加到形状组合
shapegr.ChildObjects.Add(new ShapeObject(doc, ShapeType.Ellipse)
{
Width = 250,
Height = 250,
VerticalPosition = 800,
HorizontalPosition = 900,
LineStyle = ShapeLineStyle.Single,
FillColor = Color.Red,
StrokeColor = Color.Red,
StrokeWeight = 1,
});
步骤7:保存文档
//保存并打开文档
doc.SaveToFile("InsertShapegroups.docx", FileFormat.Docx2010);
System.Diagnostics.Process.Start("InsertShapegroups.docx");
添加效果:
以上全部是关于Word中绘制图形形状的内容。如需转载,请注明出处!
感谢阅读!
创新互联www.cdcxhl.cn,专业提供香港、美国云服务器,动态BGP最优骨干路由自动选择,持续稳定高效的网络助力业务部署。公司持有工信部办法的idc、isp许可证, 机房独有T级流量清洗系统配攻击溯源,准确进行流量调度,确保服务器高可用性。佳节活动现已开启,新人活动云服务器买多久送多久。