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)
形状组合效果图: