溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

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

發(fā)布時(shí)間:2020-07-05 11:17:17 來源:網(wǎng)絡(luò) 閱讀:1147 作者:E_iceblue 欄目:編程語言

一、序言

在Office Word中,支持在Word文檔中插入類型非常豐富的形狀,包括線條、矩形、基本形狀(諸如圓形、多邊形、星形、括號(hào)、笑臉等等圖形)、箭頭形狀、公式形狀、流程圖、旗幟圖形、標(biāo)注圖形等等,我們?cè)诰幊踢^程中,想要在Word中繪制不同類型的圖形,可以通過類庫來操作??丶pire.Doc for .NET 6.0及以上版本開始支持Office Word中的所有圖形,可以通過代碼操作某個(gè)單一的形狀,也可以通過將單一形狀進(jìn)行組合來獲得想要的圖形或形狀效果,當(dāng)然,也支持自己自定義圖形,通過編程繪制也是可以的。下面將介紹向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:創(chuàng)建示例,添加section、paragraph

//創(chuàng)建一個(gè)Document實(shí)例
Document doc = new Document();
//添加一個(gè)section paragraph
 Section sec = doc.AddSection();
 Paragraph para1 = sec.AddParagraph();

步驟3:在文檔指定位置插入形狀,并設(shè)置形狀類型、大小、填充顏色、線條樣式等
(這里簡單列舉幾個(gè)形狀的添加方法,方法比較簡單,不做贅述,效果圖中列舉了部分形狀樣式,需要其他樣式的形狀可自行設(shè)置添加)

            //插入一個(gè)矩形
            ShapeObject shape1 = para1.AppendShape(50, 50, ShapeType.Rectangle);
            shape1.FillColor = Color.Blue;
            shape1.StrokeColor = Color.LightSkyBlue;
            shape1.HorizontalPosition = 20;
            shape1.VerticalPosition = 20;

            //插入一個(gè)圓形
            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;

            //插入一個(gè)公式符號(hào) +
            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:創(chuàng)建文檔,添加section、paragraph

Document doc = new Document();
Section sec = doc.AddSection();
Paragraph para1 = sec.AddParagraph();

步驟3:添加文字,并應(yīng)用格式到文字

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:實(shí)例化段落2,并創(chuàng)建一個(gè)形狀組合,并設(shè)置大小

//實(shí)例化段落2
Paragraph para2 = sec.AddParagraph();
//創(chuàng)建一個(gè)形狀組合并設(shè)置大小
ShapeGroup shapegr = para2.AppendShapeGroup(300, 300);

步驟5:繪制一個(gè)中國國旗,這里需要組合形狀矩形和五角星形,并填充相應(yīng)的顏色

            //添加一個(gè)矩形到形狀組合
            shapegr.ChildObjects.Add(new ShapeObject(doc, ShapeType.Rectangle)
            {
                Width = 900,
                Height = 500,
                LineStyle = ShapeLineStyle.Single,
                FillColor = Color.Red,
                StrokeColor = Color.Red,                
                StrokeWeight = 1,
            });

            //添加第一個(gè)五角星到形狀組合
            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,
            });
            //添加第二個(gè)五角星到形狀組合
            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,
            });
            //添加第三個(gè)五角星到形狀組合
            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,
            });
            //添加第四個(gè)五角星到形狀組合
            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,
            });
            //添加第五個(gè)五角星到形狀組合
            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:繪制一個(gè)日本國旗,需要組合形狀矩形和圓形,并填充顏色

          //繪制一個(gè)矩形并添加到形狀組合
            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,
            });
            //繪制一個(gè)圓形并添加到形狀組合
            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圖形、組合圖形

以上全部是關(guān)于Word中繪制圖形形狀的內(nèi)容。如需轉(zhuǎn)載,請(qǐng)注明出處!
感謝閱讀!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI