在 Word 中插入藝術(shù)字
Spire.Doc for .NET是一款專(zhuān)門(mén)對(duì) Word 文檔進(jìn)行操作的 .NET 類(lèi)庫(kù)。在于幫助開(kāi)發(fā)人員無(wú)需安裝 Microsoft Word情況下,輕松快捷高效地創(chuàng)建、編輯、轉(zhuǎn)換和打印 Microsoft Word 文檔。擁有近10年專(zhuān)業(yè)開(kāi)發(fā)經(jīng)驗(yàn)Spire系列辦公文檔開(kāi)發(fā)工具,專(zhuān)注于創(chuàng)建、編輯、轉(zhuǎn)換和打印Word/PDF/Excel等格式文件處理,小巧便捷。在 C#、VB.NET 中從 Word 中提取圖像。
藝術(shù)字是 MS Word 中的一項(xiàng)功能,可讓您在文檔中插入色彩豐富且時(shí)尚的文本。除此之外,它還可以彎曲、拉伸或傾斜文本的形狀,這是一種通過(guò)特殊效果使文本脫穎而出的快速方法。在本文中,您將學(xué)習(xí)如何使用Spire.Doc for .NET以編程方式將藝術(shù)字插入 Word 文檔。
為 .NET 安裝 Spire.Doc
首先,您需要添加 Spire.Doc for .NET 包中包含的 DLL 文件作為 .NET 項(xiàng)目中的引用。DLL 文件可以從此鏈接下載或通過(guò)NuGet安裝。
PM> Install-Package Spire.Doc
在 Word 中插入藝術(shù)字
Spire.Doc 為 .NET 提供的ShapeType枚舉定義了各種名稱(chēng)以“Text”開(kāi)頭的藝術(shù)字形狀類(lèi)型。為了在 Word 中創(chuàng)建藝術(shù)字,您需要初始化一個(gè)ShapeObject實(shí)例并指定藝術(shù)字類(lèi)型和文本內(nèi)容。詳細(xì)步驟如下:
- 創(chuàng)建一個(gè)文檔實(shí)例。
- 使用Document.AddSection()方法向文檔添加一個(gè)部分,然后使用Section.AddParagraph()方法向該部分添加一個(gè)段落。
- 將形狀附加到段落并使用Paragraph.AppendShape(float width , float height , ShapeType shapeType )方法指定形狀大小和類(lèi)型。
- 使用ShapeObject.VerticalPosition和ShapeObject.HorizontalPosition屬性設(shè)置形狀的位置。
- 使用WordArt.Text屬性設(shè)置藝術(shù)字的文本。
- 使用ShapeObject.FillColor和ShapeObject.StrokeColor屬性設(shè)置藝術(shù)字的填充顏色和描邊顏色。
- 使用Document.SaveToFile()方法將文檔保存到另一個(gè)文件。
【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 中替換圖像,如果您有其他問(wèn)題也可以繼續(xù)瀏覽本系列文章,獲取相關(guān)教程,你還可以給我留言或者加入我們的官方技術(shù)交流群(767755948)