Spire.Doc系列教程:C# 添加形状和形状组合到 Word 文档

Spire.Doc 从版本6.0开始,支持添加多种形状(线条,矩形、基本形状,箭头,流程图,公式形状,星与旗帜及标注)等,同时各种单一的形状也可以组合在一起,成为一组形状组合。 本文主要介绍如何使用Spire.Doc在word中添加形状及形状组合。

添加单个形状
//创建一个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);复制代码

效果图:

添加组合形状
//创建一个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);复制代码

形状组合效果图:

                                                            【下载Spire.Doc最新试用版


转载于:https://juejin.im/post/5c85dcea5188257e8f616d03

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值