Spire.Doc 从版本6.0开始,支持添加多种形状(线条,矩形、基本形状,箭头,流程图,公式形状,星与旗帜及标注)等,同时各种单一的形状也可以组合在一起,成为一组形状组合。本文主要介绍如何使用Spire.Doc在word中添加形状及形状组合。
添加单个形状
C#
//创建一个Document实例
Document doc = new Document();
//添加一个section
Section sec = doc.AddSection();
//添加一个paragraph
Paragraph para1 = sec.AddParagraph();
//插入一个心形
ShapeObject shape1 = para1.AppendShape(50, 50, ShapeType.Heart);
shape1.FillColor = Color.Red;
shape1.StrokeColor = Color.Red;
shape1.HorizontalPosition = 200;
shape1.VerticalPosition = 20;
//插入一个箭头
ShapeObject shape2 = para1.AppendShape(100, 100, ShapeType.Arrow);
shape2.FillColor = Color.Purple;
shape2.StrokeColor = Color.Black;
shape2.LineStyle = ShapeLineStyle.Double;
shape2.StrokeWeight = 3;
shape2.HorizontalPosition = 200;
shape2.VerticalPosition = 100;
//插入一个公式符号 +
ShapeObject shape3 = para1.AppendShape(50, 50, ShapeType.Plus);
shape3.FillColor = Color.Red;
shape3.StrokeColor = Color.Red;
shape3.LineStyle = ShapeLineStyle.Single;
shape3.StrokeWeight = 3;
shape3.HorizontalPosition = 200;
shape3.VerticalPosition = 200;
//插入一颗star
ShapeObject shape4 = para1.AppendShape(50, 50, ShapeType.Star);
shape4.FillColor = Color.Gold;
shape4.StrokeColor = Color.Gold;
shape4.LineStyle = ShapeLineStyle.Single;
shape4.HorizontalPosition = 200;
shape4.VerticalPosition = 300;
//保存文档
doc.SaveToFile("InsertShapes.docx", FileFormat.Docx2010);
VB.NET
'创建一个Document实例 Dim doc As Document = New Document '添加一个section
Dim sec As Section = doc.AddSection
'添加一个paragraph Dim para1 As Paragraph = sec.AddParagraph '插入一个心形
Dim shape1 As ShapeObject = para1.AppendShape(50, 50, ShapeType.Heart)
shape1.FillColor = Color.Red
shape1.StrokeColor = Color.Red
shape1.HorizontalPosition = 200
shape1.VerticalPosition = 20
'插入一个箭头 Dim shape2 As ShapeObject = para1.AppendShape(100,100,ShapeType.Arrow) shape2.FillColor = Color.Purple shape2.StrokeColor = Color.Black shape2.LineStyle = ShapeLineStyle.Double shape2.StrokeWeight = 3 shape2.HorizontalPosition = 200 shape2.VerticalPosition = 100 '插入一个公式符号 +
Dim shape3 As ShapeObject = para1.AppendShape(50, 50, ShapeType.Plus)
shape3.FillColor = Color.Red
shape3.StrokeColor = Color.Red
shape3.LineStyle = ShapeLineStyle.Single
shape3.StrokeWeight = 3
shape3.HorizontalPosition = 200
shape3.VerticalPosition = 200
'插入一颗star Dim shape4 As ShapeObject = para1.AppendShape(50,50,ShapeType.Star) shape4.FillColor = Color.Gold shape4.StrokeColor = Color.Gold shape4.LineStyle = ShapeLineStyle.Single shape4.HorizontalPosition = 200 shape4.VerticalPosition = 300 '保存文档
doc.SaveToFile("InsertShapes.docx", FileFormat.Docx2010)
效果图:
添加形状组合
C#
//创建一个Document实例并添加section及paragraph
Document doc = new Document();
Section sec = doc.AddSection();
Paragraph para = sec.AddParagraph();
//创建一个形状组合并设置大小
ShapeGroup shapegr = para.AppendShapeGroup(200, 400);
//添加一个矩形到形状组合
shapegr.ChildObjects.Add(new ShapeObject(doc, ShapeType.Rectangle)
{
Width = 500,
Height = 300,
LineStyle = ShapeLineStyle.ThickThin,
StrokeColor = System.Drawing.Color.Blue,
StrokeWeight = 1.5,
});
//添加一个三角形到形状组合
shapegr.ChildObjects.Add(new ShapeObject(doc, ShapeType.RightTriangle)
{
Width = 500,
Height = 300,
VerticalPosition = 301,
LineStyle = ShapeLineStyle.ThickThin,
StrokeColor = System.Drawing.Color.Green,
StrokeWeight = 1.5,
});
//添加一个十字箭头到形状组合
shapegr.ChildObjects.Add(new ShapeObject(doc, ShapeType.QuadArrow)
{
Width = 500,
Height = 300,
VerticalPosition = 601,
LineStyle = ShapeLineStyle.ThickThin,
StrokeColor = System.Drawing.Color.Blue,
StrokeWeight = 1.5,
});
//保存文档
doc.SaveToFile("InsertShapegroups.docx", FileFormat.Docx2010);
VB.NET
'创建一个Document实例并添加section及paragraph Dim doc As Document = New Document Dim sec As Section = doc.AddSection Dim para As Paragraph = sec.AddParagraph '创建一个形状组合并设置大小
Dim shapegr As ShapeGroup = para.AppendShapeGroup(200, 400)
'添加一个矩形到形状组合 shapegr.ChildObjects.Add(New ShapeObject(doc,ShapeType.Rectangle) With{_ Key .Width = 500,_ Key .Height = 300,_ Key .LineStyle = ShapeLineStyle.ThickThin,_ Key .StrokeColor = System.Drawing.Color.Blue,_ Key .StrokeWeight = 1.5 _}) '添加一个三角形到形状组合
shapegr.ChildObjects.Add(New ShapeObject(doc, ShapeType.RightTriangle) With { _
Key .Width = 500, _
Key .Height = 300, _
Key .VerticalPosition = 301, _
Key .LineStyle = ShapeLineStyle.ThickThin, _
Key .StrokeColor = System.Drawing.Color.Green, _
Key .StrokeWeight = 1.5 _
})
'添加一个十字箭头到形状组合 shapegr.ChildObjects.Add(New ShapeObject(doc,ShapeType.QuadArrow) With{_ Key .Width = 500,_ Key .Height = 300,_ Key .VerticalPosition = 601,_ Key .LineStyle = ShapeLineStyle.ThickThin,_ Key .StrokeColor = System.Drawing.Color.Blue,_ Key .StrokeWeight = 1.5 _}) '保存文档
doc.SaveToFile("InsertShapegroups.docx", FileFormat.Docx2010)
形状组合效果图: