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

2025-10-19 14:44:46

1、安装后注意在程序中引用Spire.Doc.dll,dll文件可以在安装路径下的Bin文件夹中获取。

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

1、步骤1:添加如下using指定

using Spire.Doc;

using Spire.Doc.Documents;

using Spire.Doc.Fields;

using System.Drawing;

2、步骤2:创建示例,添加section、paragraph

//创建一个Document实例

Document doc = new Document();

//添加一个section paragraph

 Section sec = doc.AddSection();

 Paragraph para1 = sec.AddParagraph();

3、步骤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、步骤4:保存文档

//保存并打开文档

doc.SaveToFile("InsertShapes.docx", FileFormat.Docx2010);

System.Diagnostics.Process.Start("InsertShapes.docx");

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

1、步骤1:添加如下using指令

using Spire.Doc;

using Spire.Doc.Documents;

using Spire.Doc.Fields;

using System.Drawing;

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

Document doc = new Document();

Section sec = doc.AddSection();

Paragraph para1 = sec.AddParagraph();

3、步骤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、步骤4:实例化段落2,并创建一个形状组合,并设置大小

//实例化段落2

Paragraph para2 = sec.AddParagraph();

//创建一个形状组合并设置大小

ShapeGroup shapegr = para2.AppendShapeGroup(300, 300);

5、步骤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、步骤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、步骤7:保存文档

//保存并打开文档

doc.SaveToFile("InsertShapegroups.docx", FileFormat.Docx2010);

System.Diagnostics.Process.Start("InsertShapegroups.docx");

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

声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
猜你喜欢