资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

C#绘制Word图形、组合图形

一、序言

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

创新互联公司专注于企业成都营销网站建设、网站重做改版、磴口网站定制设计、自适应品牌网站建设、html5商城网站建设、集团公司官网建设、外贸网站建设、高端网站制作、响应式网页设计等建站业务,价格优惠性价比高,为磴口等各大城市提供网站开发制作服务。

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中绘制图形形状的内容。如需转载,请注明出处!
感谢阅读!


网站栏目:C#绘制Word图形、组合图形
地址分享:http://cdkjz.cn/article/jegogp.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220