• Word控件Spire.Doc 【图像形状】教程(6): 如何在 C#、VB.NET 的 Word 文档中插入形状和形状组


    Spire.Doc for .NET是一款专门对 Word 文档进行操作的 .NET 类库。在于帮助开发人员无需安装 Microsoft Word情况下,轻松快捷高效地创建、编辑、转换和打印 Microsoft Word 文档。拥有近10年专业开发经验Spire系列办公文档开发工具,专注于创建、编辑、转换和打印Word/PDF/Excel等格式文件处理,小巧便捷。在 C#、VB.NET 中从 Word 中提取图像。

    Spire.Doc for.NET 最新下载(qun:767755948)icon-default.png?t=M85Bhttps://www.evget.com/product/3368/download

    MS Word 允许用户从形状菜单中选择形状,将其拖放到页面上的任何所需位置。从 Spire.Doc 版本 6.0 或更高版本开始,我们添加了一个使用代码处理形状的新功能。以下部分将介绍如何使用 Spire.Doc 在 Word 文档中的指定位置插入形状和形状组。

    代码片段:

    第 1 步:初始化 Document 类的新实例。

    Document doc = new Document();

    第 2 步:在 Word 文档中添加一个新部分,并在该部分中添加一个段落。

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

    第 3 步:通过调用 AppendShape() 方法将形状添加到段落中。为了定位形状的放置位置,您只需设置 ShapeObject 类的 HorizontalPosition 和 VerticalPosition 属性。我们还可以通过设置 FillColor、StrokeColor 和 LineStyle 属性来格式化形状。

    ShapeObject shape1 = para1.AppendShape(50, 50, ShapeType.Heart);
    
    shape1.FillColor = Color.Red;
    shape1.StrokeColor = Color.Red;
    shape1.HorizontalPosition = 200;
    shape1.VerticalPosition = 100;
    
    ShapeObject shape2 = para1.AppendShape(100, 100, ShapeType.Arrow);
    
    shape2.FillColor = Color.Purple;
    shape2.StrokeColor = Color.Black;
    shape2.LineStyle = ShapeLineStyle.Double;
    shape2.StrokeWeight = 3;
    shape2.HorizontalPosition = 200;
    shape2.VerticalPosition = 200;

    第 4 步:添加一个新段落并通过调用 AppendShapeGroup() 方法将一个形状组插入该段落。

    Paragraph para2 = sec.AddParagraph();
    ShapeGroup shapegr = para2.AppendShapeGroup(200, 400);
    
    shapegr.ChildObjects.Add(new ShapeObject(doc, ShapeType.Rectangle)
    {
    Width = 500,
    Height = 300,
    LineStyle = ShapeLineStyle.ThickThin,
    StrokeColor = System.Drawing.Color.Blue,
    
    StrokeWeight = 1.5,
    });
    shapegr.ChildObjects.Add(new ShapeObject(doc, ShapeType.RightTriangle)
    {
    Width = 500,
    Height = 300,
    VerticalPosition = 301,
    LineStyle = ShapeLineStyle.ThickThin,
    StrokeColor = System.Drawing.Color.Green,
    StrokeWeight = 1.5,
    });
    shapegr.ChildObjects.Add(new ShapeObject(doc, ShapeType.QuadArrow)
    {
    Width = 500,
    Height = 300,
    VerticalPosition = 601,
    LineStyle = ShapeLineStyle.ThickThin,
    StrokeColor = System.Drawing.Color.Blue,
    StrokeWeight = 1.5,
    });

    第 5 步:将文档保存到文件中。

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

    结果

    完整代码

    [C#]

    using Spire.Doc;
    using Spire.Doc.Documents;
    using Spire.Doc.Fields;
    using System.Drawing;
    namespace InsertShape
    {
    class Program
    {
    static void Main(string[] args)
    {
    Document doc = new Document();
    Section sec = doc.AddSection();
    Paragraph para1 = sec.AddParagraph();
    
    ShapeObject shape1 = para1.AppendShape(50, 50, ShapeType.Heart);
    
    shape1.FillColor = Color.Red;
    shape1.StrokeColor = Color.Red;
    shape1.HorizontalPosition = 200;
    shape1.VerticalPosition = 100;
    
    ShapeObject shape2 = para1.AppendShape(100, 100, ShapeType.Arrow);
    
    shape2.FillColor = Color.Purple;
    shape2.StrokeColor = Color.Black;
    shape2.LineStyle = ShapeLineStyle.Double;
    shape2.StrokeWeight = 3;
    shape2.HorizontalPosition = 200;
    shape2.VerticalPosition = 200;
    
    Paragraph para2 = sec.AddParagraph();
    ShapeGroup shapegr = para2.AppendShapeGroup(200, 400);
    
    shapegr.ChildObjects.Add(new ShapeObject(doc, ShapeType.Rectangle)
    {
    Width = 500,
    Height = 300,
    LineStyle = ShapeLineStyle.ThickThin,
    StrokeColor = System.Drawing.Color.Blue,
    
    StrokeWeight = 1.5,
    });
    shapegr.ChildObjects.Add(new ShapeObject(doc, ShapeType.RightTriangle)
    {
    Width = 500,
    Height = 300,
    VerticalPosition = 301,
    LineStyle = ShapeLineStyle.ThickThin,
    StrokeColor = System.Drawing.Color.Green,
    StrokeWeight = 1.5,
    });
    shapegr.ChildObjects.Add(new ShapeObject(doc, ShapeType.QuadArrow)
    {
    Width = 500,
    Height = 300,
    VerticalPosition = 601,
    LineStyle = ShapeLineStyle.ThickThin,
    StrokeColor = System.Drawing.Color.Blue,
    StrokeWeight = 1.5,
    });
    
    doc.SaveToFile("InsertShapes.docx", FileFormat.Docx2010);
    }
    }
    }

    [VB.NET]

    Imports Spire.Doc
    Imports Spire.Doc.Documents
    Imports Spire.Doc.Fields
    Imports System.Drawing
    Namespace InsertShape
    Class Program
    Private Shared Sub Main(args As String())
    Dim doc As New Document()
    Dim sec As Section = doc.AddSection()
    Dim para1 As Paragraph = sec.AddParagraph()
    
    Dim shape1 As ShapeObject = para1.AppendShape(50, 50, ShapeType.Heart)
    
    shape1.FillColor = Color.Red
    shape1.StrokeColor = Color.Red
    shape1.HorizontalPosition = 200
    shape1.VerticalPosition = 100
    
    Dim shape2 As ShapeObject = para1.AppendShape(100, 100, ShapeType.Arrow)
    
    shape2.FillColor = Color.Purple
    shape2.StrokeColor = Color.Black
    shape2.LineStyle = ShapeLineStyle.[Double]
    shape2.StrokeWeight = 3
    shape2.HorizontalPosition = 200
    shape2.VerticalPosition = 200
    
    Dim para2 As Paragraph = sec.AddParagraph()
    Dim shapegr As ShapeGroup = para2.AppendShapeGroup(200, 400)
    
    shapegr.ChildObjects.Add(New ShapeObject(doc, ShapeType.Rectangle) With { _
    Key .Width = 500, _
    Key .Height = 300, _
    Key .LineStyle = ShapeLineStyle.ThickThin, _
    Key .StrokeColor = System.Drawing.Color.Blue, _
    Key .StrokeWeight = 1.5 _
    })
    shapegr.ChildObjects.Add(New ShapeObject(doc, ShapeType.RightTriangle) With { _
    Key .Width = 500, _
    Key .Height = 300, _
    Key .VerticalPosition = 301, _
    Key .LineStyle = ShapeLineStyle.ThickThin, _
    Key .StrokeColor = System.Drawing.Color.Green, _
    Key .StrokeWeight = 1.5 _
    })
    shapegr.ChildObjects.Add(New ShapeObject(doc, ShapeType.QuadArrow) With { _
    Key .Width = 500, _
    Key .Height = 300, _
    Key .VerticalPosition = 601, _
    Key .LineStyle = ShapeLineStyle.ThickThin, _
    Key .StrokeColor = System.Drawing.Color.Blue, _
    Key .StrokeWeight = 1.5 _
    })
    
    doc.SaveToFile("InsertShapes.docx", FileFormat.Docx2010)
    End Sub
    End Class
    End Namespace

    以上便是如何在 C# 中将文本环绕在图像周围,如果您有其他问题也可以继续浏览本系列文章,获取相关教程,你还可以给我留言或者加入我们的官方技术交流群。

     

  • 相关阅读:
    图像处理与计算机视觉--第五章-图像分割-霍夫变换
    NFT Insider#110:The Sandbox与T&B Media Global合作,YGG Web3游戏峰会阵容揭晓
    http升级为https
    Azkaban环境配置-尚硅谷大数据培训
    从0到1 手把手搭建spring cloud alibaba 微服务大型应用框架(十一)spring-boot-admin 监控篇(1) 原理与介绍
    如何扩大电脑c盘分区,c盘空间不足怎么扩容
    计算机考研 | 2020年 | 计算机组成原理真题
    皮革店铺怎么实施IT程序快速实施 部署
    适用于遥感图像处理的神经网络
    spring boot日常开发中的小技巧
  • 原文地址:https://blog.csdn.net/m0_67129275/article/details/128076406