C# 繪製Word圖形、組合圖形

一、序言

在Office Word中,支持在Word文檔中插入類型非常豐富的形狀,包括線條、矩形、基本形狀(諸如圓形、多邊形、星形、括號、笑臉等等圖形)、箭頭形狀、公式形狀、流程圖、旗幟圖形、標註圖形等等,我們在編程過程中,想要在Word中繪製不同類型的圖形,可以通過類庫來操作。控件Spire.Doc for .NET 6.0及以上版本開始支持Office Word中的所有圖形,可以通過代碼操作某個單一的形狀,也可以通過將單一形狀進行組合來獲得想要的圖形或形狀效果,當然,也支持自己自定義圖形,通過編程繪製也是可以的。下面將介紹向Word繪製形狀和組合形狀的方法,方法中的代碼供參考。

PS:

  • Spire.Doc for .NET獲取地址
  • 安裝後,dll文件可在安裝路徑下的Bin文件夾中獲取
    Dll引用
    C# 繪製Word圖形、組合圖形

    二、代碼示例

    (一)、繪製單一形狀

    步驟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");

形狀添加效果:
C# 繪製Word圖形、組合圖形

(二)、添加組合形狀

步驟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");

添加效果:
C# 繪製Word圖形、組合圖形

以上全部是關於Word中繪製圖形形狀的內容。如需轉載,請註明出處!
感謝閱讀!

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