zl程序教程

您现在的位置是:首页 >  后端

当前栏目

Word控件Spire.Doc 【图像形状】教程(8): 如何借助C#/VB.NET在 Word 中插入艺术字

c#Net教程 如何 控件 图像 插入 word
2023-09-11 14:14:49 时间

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 for .NET以编程方式将艺术字插入 Word 文档。

为 .NET 安装 Spire.Doc

首先,您需要添加 Spire.Doc for .NET 包中包含的 DLL 文件作为 .NET 项目中的引用。DLL 文件可以从此链接下载或通过NuGet安装。

PM> Install-Package Spire.Doc

在 Word 中插入艺术字

Spire.Doc 为 .NET 提供的ShapeType枚举定义了各种名称以“Text”开头的艺术字形状类型。为了在 Word 中创建艺术字,您需要初始化一个ShapeObject实例并指定艺术字类型和文本内容。详细步骤如下:

  • 创建一个文档实例。
  • 使用Document.AddSection()方法向文档添加一个部分,然后使用Section.AddParagraph()方法向该部分添加一个段落。
  • 将形状附加到段落并使用Paragraph.AppendShape(float width , float height , ShapeType shapeType )方法指定形状大小和类型。
  • 使用ShapeObject.VerticalPosition和ShapeObject.HorizontalPosition属性设置形状的位置。
  • 使用WordArt.Text属性设置艺术字的文本。
  • 使用ShapeObject.FillColor和ShapeObject.StrokeColor属性设置艺术字的填充颜色和描边颜色。
  • 使用Document.SaveToFile()方法将文档保存到另一个文件。

【C#】

using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
namespace CreatWordArt
{
class Program
{
static void Main(string[] args)
{
//Create a Document instance
Document doc = new Document();

//Add a section
Section section = doc.AddSection();

//Add a paragraph
Paragraph paragraph = section.AddParagraph();

//Append a shape to the paragraph and specify the shape size and type
ShapeObject shape = paragraph.AppendShape(400, 150, ShapeType.TextDeflateBottom);

//Set the position of the shape
shape.VerticalPosition = 60;
shape.HorizontalPosition = 60;

//Set the text of WordArt
shape.WordArt.Text = "Create WordArt in Word";

//Set the fill color and stroke color of WordArt
shape.FillColor = System.Drawing.Color.Cyan;
shape.StrokeColor = System.Drawing.Color.DarkBlue;

//Save the document
doc.SaveToFile("CreateWordArt.docx", FileFormat.Docx2013);
}
}
}

【VB.NET】

Imports Spire.Doc
Imports Spire.Doc.Documents
Imports Spire.Doc.Fields

Namespace CreatWordArt
Class Program
Private Shared Sub Main(ByVal args As String())

'Create a Document instance
Dim doc As Document = New Document()

'Add a section
Dim section As Section = doc.AddSection()

'Add a paragraph
Dim paragraph As Paragraph = section.AddParagraph()

'Append a shape to the paragraph and specify the shape size and type
Dim shape As ShapeObject = paragraph.AppendShape(400, 150, ShapeType.TextDeflateBottom)

'Set the position of the shape
shape.VerticalPosition = 60
shape.HorizontalPosition = 60

'Set the text of WordArt
shape.WordArt.Text = "Create WordArt in Word"

'Set the fill color and stroke color of WordArt
shape.FillColor = System.Drawing.Color.Cyan
shape.StrokeColor = System.Drawing.Color.DarkBlue

'Save the document
doc.SaveToFile("CreateWordArt.docx", FileFormat.Docx2013)
End Sub
End Class
End Namespace

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