微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

C# 绘制Word形状——基本形状、组合形状

一、序言

在Office Word中,支持在Word文档中插入类型非常丰富的形状,包括线条、矩形、基本形状(诸如圆形、多边形、星形、括号、笑脸等等图形)、箭头形状、公式形状、流程图、旗帜图形、标注图形等等,我们在编程过程中,想要在Word中绘制不同类型的图形,可以通过类库来操作。控件Spire.Doc for .NET 6.0及以上版本开始支持Office Word中的所有图形,可以通过代码操作某个单一的形状,也可以通过将单一形状进行组合来获得想要的图形或形状效果,当然,也支持自己自定义图形,通过编程绘制也是可以的。下面将介绍向Word绘制形状和组合形状的方法方法中的代码供参考。

PS:

Dll引用

二、代码示例

(一)绘制单一形状

步骤1:添加如下using指定

using Spire.Doc;
 Spire.Doc.Documents;
 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 = ;

            插入一个圆形
            ShapeObject shape2 = para1.AppendShape( Color.Purple;
            shape2.strokeColor = Color.LightPink;
            shape2.Linestyle = ShapeLinestyle.Single;
            shape2.strokeWeight = 1;
            shape2.HorizontalPosition = 80;
            shape2.VerticalPosition = 插入一个公式符号 +
            ShapeObject shape3 = para1.AppendShape( Color.DarkCyan;
            shape3.strokeColor = Color.LightGreen;
            shape3.Linestyle = ShapeLinestyle.Single;
            shape3.strokeWeight = ;
            shape3.HorizontalPosition = 140;
            shape3.VerticalPosition = 插入一颗星形
            ShapeObject shape4 = para1.AppendShape( Color.Red;
            shape4.strokeColor = Color.Gold;
            shape4.Linestyle = ShapeLinestyle.Single;
            shape4.HorizontalPosition = 200;
            shape4.VerticalPosition = 20;

 

步骤4:保存文档

保存并打开文档
doc.SavetoFile("InsertShapes.docx"stem.Diagnostics.Process.Start(");

 

形状添加效果

全部代码

  1   2   3   4  System.Drawing;
  5 
  6 namespace AddShapes_Doc
  7 {
  8     class Program
  9     {
 10         static void Main(string[] args)
 11         {
 12             创建一个Document实例
 13             Document doc =  14 
 15             添加一个section paragraph
 16             Section sec = doc.AddSection();
 17             Paragraph para1 = sec.AddParagraph();
 18 
 19             插入一个矩形
 20             ShapeObject shape1 = para1.AppendShape( 21             shape1.FillColor = Color.Blue;
 22             shape1.strokeColor = Color.LightSkyBlue;
 23             shape1.HorizontalPosition = ;
 24             shape1.VerticalPosition =  25 
 26             插入一个圆形
 27             ShapeObject shape2 = para1.AppendShape( 28             shape2.FillColor = Color.Purple;
 29             shape2.strokeColor = Color.LightPink;
 30             shape2.Linestyle = ShapeLinestyle.Single;
 31             shape2.strokeWeight =  32             shape2.HorizontalPosition =  33             shape2.VerticalPosition =  34 
 35             插入一个公式符号 +
 36             ShapeObject shape3 = para1.AppendShape( 37             shape3.FillColor = Color.DarkCyan;
 38             shape3.strokeColor = Color.LightGreen;
 39             shape3.Linestyle = 40             shape3.strokeWeight =  41             shape3.HorizontalPosition =  42             shape3.VerticalPosition =  43 
 44             插入一颗星形
 45             ShapeObject shape4 = para1.AppendShape( 46             shape4.FillColor = Color.Red;
 47             shape4.strokeColor = Color.Gold;
 48             shape4.Linestyle = 49             shape4.HorizontalPosition =  50             shape4.VerticalPosition =  51 
 52             插入一个立方体
 53             ShapeObject shape5 = para1.AppendShape( 54             shape5.FillColor = Color.OrangeRed;
 55             shape5.strokeColor = Color.Orange;
 56             shape5.Linestyle = 57             shape5.HorizontalPosition = 260 58             shape5.VerticalPosition =  59 
 60             插入一个圆柱体
 61             ShapeObject shape6 = para1.AppendShape( 62             shape6.FillColor = Color.Goldenrod;
 63             shape6.strokeColor = 64             shape6.Linestyle = 65             shape6.HorizontalPosition = 320 66             shape6.VerticalPosition =  67 
 68             插入一个箭头
 69             ShapeObject shape7 = para1.AppendShape( 70             shape7.FillColor = Color.Yellow;
 71             shape7.strokeColor = 72             shape7.Linestyle = 73             shape7.HorizontalPosition =  74             shape7.VerticalPosition =  75 
 76             插入一个v形臂章图形
 77             ShapeObject shape8 = para1.AppendShape( 78             shape8.FillColor = Color.YellowGreen;
 79             shape8.strokeColor = 80             shape8.Linestyle = 81             shape8.HorizontalPosition =  82             shape8.VerticalPosition =  83 
 84             插入一个循环箭头图形
 85             ShapeObject shape9 = para1.AppendShape(cularArrow);
 86             shape9.FillColor = Color.Green;
 87             shape9.strokeColor = 88             shape9.Linestyle = 89             shape9.HorizontalPosition =  90             shape9.VerticalPosition =  91 
 92             插入一个云图形
 93             ShapeObject shape10 = para1.AppendShape( 94             shape10.FillColor = 95             shape10.strokeColor = Color.White;
 96             shape10.Linestyle = 97             shape10.HorizontalPosition =  98             shape10.VerticalPosition =  99 
100             插入一个环形图
101             ShapeObject shape11 = para1.AppendShape(102             shape11.FillColor = Color.Pink;
103             shape11.strokeColor =104             shape11.Linestyle =105             shape11.HorizontalPosition = 106             shape11.VerticalPosition = 107 
108             插入一个波浪形状图
109             ShapeObject shape12 = para1.AppendShape(110             shape12.FillColor = Color.Plum;
111             shape12.strokeColor =112             shape12.Linestyle =113             shape12.HorizontalPosition = 114             shape12.VerticalPosition = 115 
116             插入一个礼结状图形
117             ShapeObject shape13 = para1.AppendShape(118             shape13.FillColor = Color.Rosybrown;
119             shape13.strokeColor =120             shape13.Linestyle =121             shape13.HorizontalPosition = 122             shape13.VerticalPosition = 123 
124             插入一个心形图
125             ShapeObject shape14 = para1.AppendShape(126             shape14.FillColor =127             shape14.strokeColor =128             shape14.Linestyle =129             shape14.HorizontalPosition = 130             shape14.VerticalPosition = 131 
132             插入一个六边形图形
133             ShapeObject shape15 = para1.AppendShape(134             shape15.FillColor =135             shape15.strokeColor =136             shape15.Linestyle =137             shape15.HorizontalPosition = 138             shape15.VerticalPosition = 139 
140             插入一个不规则图形
141             ShapeObject shape16 = para1.AppendShape(larseal1);
142             shape16.FillColor = Color.DeepPink;
143             shape16.strokeColor =144             shape16.Linestyle =145             shape16.HorizontalPosition = 146             shape16.VerticalPosition = 147 
148             插入一个月亮形状
149             ShapeObject shape17 = para1.AppendShape(150             shape17.FillColor = Color.Violet;
151             shape17.strokeColor =152             shape17.Linestyle =153             shape17.HorizontalPosition = 154             shape17.VerticalPosition = 155 
156             插入一个"禁止"形状
157             ShapeObject shape18 = para1.AppendShape(158             shape18.FillColor =159             shape18.strokeColor =160             shape18.Linestyle =161             shape18.HorizontalPosition = 162             shape18.VerticalPosition = 163 
164             保存并打开文档
165             doc.SavetoFile(166             System.Diagnostics.Process.Start();
167         }
168     }
169 }
View Code

 

(二)添加组合形状

步骤1:添加如下using指令

using System.Drawing;

 

步骤2:创建文档,添加section、paragraph

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

 

步骤3:添加文字,并应用格式到文字

para1.AppendText(中日文化交流);
ParagraphStyle style1 =  ParagraphStyle(doc);
style1.Name = titleStyle;
style1.CharacterFormat.Bold = true;
style1.CharacterFormat.FontName = 隶书;
style1.CharacterFormat.FontSize = 30f;
doc.Styles.Add(style1);
para1.ApplyStyle();
para1.Format.HorizontalAlignment = HorizontalAlignment.Center;

 

步骤4:实例化段落2,并创建一个形状组合,并设置大小

实例化段落2
Paragraph para2 =创建一个形状组合并设置大小
ShapeGroup shapegr = para2.AppendShapeGroup(300,1)">300);

 

步骤5:绘制一个中国国旗,这里需要组合形状矩形和五角星形,并填充相应的颜色

 添加一个矩形到形状组合
            shapegr.Childobjects.Add( ShapeObject(doc,ShapeType.Rectangle)
            {
                Width = 900500nestyle = ShapeLinestyle.Single,FillColor = Color.Red,strokeColor =strokeWeight = 添加一个五角星到形状组合
            shapegr.Childobjects.Add(10090 Color.Yellow,});
            添加第二个五角星到形状组合
            shapegr.Childobjects.Add(40210添加第三个五角星到形状组合
            shapegr.Childobjects.Add(280添加第四个五角星到形状组合
            shapegr.Childobjects.Add(160添加第五个五角星到形状组合
            shapegr.Childobjects.Add(220一个日本国旗,需要组合形状矩形和圆形,并填充颜色

绘制一个矩形并添加到形状组合
            shapegr.Childobjects.Add(700600 Color.WhiteSmoke,1)">绘制一个圆形并添加到形状组合
            shapegr.Childobjects.Add(250800
InsertShapegroups.docx");

 

添加效果

(此时的图形是组合后的效果,任意拖动图形不会出现各个形状分离、错位的情况。)

全部代码

 InsertShapesGroup_Doc
创建一个Document实例并添加section及paragraph
 14             Section sec = 15             Paragraph para1 = 16             添加文字,并应用格式到文字
 17             para1.AppendText( 18             ParagraphStyle style1 =  ParagraphStyle(doc);
 19             style1.Name =  20             style1.CharacterFormat.Bold =  21             style1.CharacterFormat.FontName =  22             style1.CharacterFormat.FontSize = 30f;
 23             doc.Styles.Add(style1);
 24             para1.ApplyStyle( 25             para1.Format.HorizontalAlignment = HorizontalAlignment.Center;
 26 
 27             实例化段落2
 28             Paragraph para2 = 29             创建一个形状组合并设置大小
 30             ShapeGroup shapegr = para2.AppendShapeGroup(300 31 
 32             添加一个矩形到形状组合
 33             shapegr.Childobjects.Add( 34             {
 35                 Width =  36                 Height =  37                 Linestyle = 38                 FillColor = 39                 strokeColor = 40                 strokeWeight =  41             });
 42 
 43             添加一个五角星到形状组合
 44             shapegr.Childobjects.Add( 45  46                 Width =  47                 Height =  48                 VerticalPosition =  49                 HorizontalPosition =  50                 Linestyle = 51                 FillColor = 52                 strokeColor = 53                 strokeWeight =  54  55             添加第二个五角星到形状组合
 56             shapegr.Childobjects.Add( 57  58                 Width =  59                 Height =  60                 VerticalPosition =  61                 HorizontalPosition =  62                 Linestyle = 63                 FillColor = 64                 strokeColor = 65                 strokeWeight =  66  67             添加第三个五角星到形状组合
 68             shapegr.Childobjects.Add( 69  70                 Width =  71                 Height =  72                 VerticalPosition =  73                 HorizontalPosition =  74                 Linestyle = 75                 FillColor = 76                 strokeColor = 77                 strokeWeight =  78  79             添加第四个五角星到形状组合
 80             shapegr.Childobjects.Add( 81  82                 Width =  83                 Height =  84                 VerticalPosition =  85                 HorizontalPosition =  86                 Linestyle = 87                 FillColor = 88                 strokeColor = 89                 strokeWeight =  90  91             添加第五个五角星到形状组合
 92             shapegr.Childobjects.Add( 93  94                 Width =  95                 Height =  96                 VerticalPosition =  97                 HorizontalPosition =  98                 Linestyle = 99                 FillColor =100                 strokeColor =101                 strokeWeight = 102 103 
104             绘制一个矩形并添加到形状组合
105             shapegr.Childobjects.Add(106 107                 Width = 108                 Height = 109                 VerticalPosition = 110                 HorizontalPosition = 111                 Linestyle =112                 FillColor =113                 strokeColor = Color.Wheat,1)">114                 strokeWeight = 115 绘制一个圆形并添加到形状组合
117             shapegr.Childobjects.Add(118 119                 Width = 120                 Height = 121                 VerticalPosition = 122                 HorizontalPosition = 123                 Linestyle =124                 FillColor =125                 strokeColor =126                 strokeWeight = 127             });    
128 
129             130             doc.SavetoFile(131             System.Diagnostics.Process.Start(132 133 134 }
View Code

 

以上全部是关于Word中绘制图形形状的内容。如需转载,请注明出处!

感谢阅读!

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐