翻譯|使用教程|編輯:胡濤|2022-09-22 10:17:02.957|閱讀 171 次
概述:在本文中我們將給您介紹如何在 Word 中插入圖像,歡迎查閱~
# 界面/圖表報(bào)表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關(guān)鏈接:
Spire.Doc for .NET是一款專門對(duì) Word 文檔進(jìn)行操作的 .NET 類庫(kù)。在于幫助開(kāi)發(fā)人員無(wú)需安裝 Microsoft Word情況下,輕松快捷高效地創(chuàng)建、編輯、轉(zhuǎn)換和打印 Microsoft Word 文檔。擁有近10年專業(yè)開(kāi)發(fā)經(jīng)驗(yàn)Spire系列辦公文檔開(kāi)發(fā)工具,專注于創(chuàng)建、編輯、轉(zhuǎn)換和打印Word/PDF/Excel等格式文件處理,小巧便捷。下面我們將給您介紹如何在 Word 中插入圖像。
Word 文檔中的圖像通常與文本內(nèi)容密切相關(guān)。與充滿文字的文檔相比,帶有圖像的文檔更具說(shuō)明性和吸引力。在本文中,您將學(xué)習(xí)如何使用Spire.Doc for .NET以編程方式在 Word 文檔中插入圖像。通過(guò)這個(gè)專業(yè)的 Word 庫(kù),您還可以設(shè)置圖像大小、位置以及環(huán)繞樣式。
首先,您需要添加 Spire.Doc for .NET 包中包含的 DLL 文件作為 .NET 項(xiàng)目中的引用。DLL 文件可以從此鏈接下載或通過(guò)NuGet安裝。
PM> Install-Package Spire.Doc
Spire.Doc for .NET 支持常見(jiàn)的環(huán)繞樣式,例如 In Line with Text、Square、Tight、Through、Top 和 Bottom、Behind the Text 以及 In Front of Text。以下是插入圖像然后設(shè)置其環(huán)繞樣式的詳細(xì)步驟。
【C#】
using System.Drawing; using Spire.Doc; using Spire.Doc.Documents; using Spire.Doc.Fields; namespace WordImage { class ImageinWord { static void Main(string[] args) { //Create a Document instance Document document = new Document(); //Load a sample Word document document.LoadFromFile("input.docx"); //Get the first section Section section = document.Sections[0]; //Get two specified paragraphs Paragraph para1 = section.Paragraphs[5]; Paragraph para2 = section.Paragraphs[9]; //Insert images in the specified paragraphs DocPicture Pic1 = para1.AppendPicture(Image.FromFile(@"C:\Users\Administrator\Desktop\pic1.jpg")); DocPicture Pic2 = para2.AppendPicture(Image.FromFile(@"C:\Users\Administrator\Desktop\pic2.png")); //Set wrapping styles to Square and Inline respectively Pic1.TextWrappingStyle = TextWrappingStyle.Square; Pic2.TextWrappingStyle = TextWrappingStyle.Inline; //Save the document to file document.SaveToFile("InsertImage.docx", FileFormat.Docx); } } }
【VB.NET】
Imports System.Drawing Imports Spire.Doc Imports Spire.Doc.Documents Imports Spire.Doc.Fields Namespace WordImage Class ImageinWord Private Shared Sub Main(ByVal args As String()) 'Create a Document instance Dim document As Document = New Document() 'Load a sample Word document document.LoadFromFile("sample.docx") 'Get the first section Dim section As Section = document.Sections(0) 'Get two specified paragraphs Dim para1 As Paragraph = section.Paragraphs(5) Dim para2 As Paragraph = section.Paragraphs(9) 'Insert images in the specified paragraphs Dim Pic1 As DocPicture = para1.AppendPicture(Image.FromFile("C:\Users\Administrator\Desktop\pic1.jpg")) Dim Pic2 As DocPicture = para2.AppendPicture(Image.FromFile("C:\Users\Administrator\Desktop\pic2.png")) ‘Set wrapping styles to Square and Inline respectively Pic1.TextWrappingStyle = TextWrappingStyle.Square Pic2.TextWrappingStyle = TextWrappingStyle.Inline 'Save the document to file document.SaveToFile("InsertImage.docx", FileFormat.Docx) End Sub End Class End Namespace
Spire.Doc for .NET 提供的DocPicture.Horizo ntalPosition和DocPicture.VerticalPosition屬性允許您在指定位置插入圖像。詳細(xì)步驟如下。
【C#】
using Spire.Doc; using Spire.Doc.Documents; using Spire.Doc.Fields; using System.Drawing; namespace InsertImage { class Program { static void Main(string[] args) { //Create a Document instance Document document = new Document(); //Load a sample Word document document.LoadFromFile("input.docx"); //Get the first section Section section = document.Sections[0]; //Load an image and insert it to the document DocPicture picture = section.Paragraphs[0].AppendPicture(Image.FromFile(@"C:\Users\Administrator\Desktop\pic.jpg")); //Set the position of the image picture.HorizontalPosition = 90.0F; picture.VerticalPosition = 50.0F; //Set the size of the image picture.Width = 150; picture.Height = 150; //Set the wrapping style to Behind picture.TextWrappingStyle = TextWrappingStyle.Behind; // Save the document to file document.SaveToFile("Insert.docx", FileFormat.Docx); } } }
【VB.NET】
Imports Spire.Doc Imports Spire.Doc.Documents Imports Spire.Doc.Fields Namespace InsertImage Class Program Private Shared Sub Main(ByVal args As String()) 'Create a Document instance Dim document As Document = New Document() 'Load a sample Word document document.LoadFromFile("input.docx") 'Get the first section Dim section As Section = document.Sections(0) 'Load an image and insert it to the document Dim picture As DocPicture = section.Paragraphs(0).AppendPicture(Image.FromFile("C:\Users\Administrator\Desktop\pic.jpg")) 'Set the position of the image picture.HorizontalPosition = 90.0F picture.VerticalPosition = 50.0F 'Set the size of the image picture.Width = 150 picture.Height = 150 ' Set the wrapping style to Behind picture.TextWrappingStyle = TextWrappingStyle.Behind ' Save the document to file document.SaveToFile("Insert.docx", FileFormat.Docx) End Sub End Class End Namespace
以上便是如何在 Word 中插入圖像(C#/VB.NET),如果您有其他問(wèn)題也可以繼續(xù)瀏覽本系列文章,獲取相關(guān)教程,你還可以給我留言或者加入我們的官方技術(shù)交流群。
歡迎下載|體驗(yàn)更多E-iceblue產(chǎn)品
獲取更多信息請(qǐng)咨詢 ;技術(shù)交流Q群(767755948)
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請(qǐng)務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請(qǐng)郵件反饋至chenjj@fc6vip.cn