【教程】Spire.Doc系列教程(6):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最新試用版


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章