在 Word 中插入圖像
Spire.Doc for .NET是一款專門對 Word 文檔進行操作的 .NET 類庫。在于幫助開發人員無需安裝 Microsoft Word情況下,輕松快捷高效地創建、編輯、轉換和打印 Microsoft Word 文檔。擁有近10年專業開發經驗Spire系列辦公文檔開發工具,專注于創建、編輯、轉換和打印Word/PDF/Excel等格式文件處理,小巧便捷。下面我們將給您介紹如何在 Word 中插入圖像。
Word 文檔中的圖像通常與文本內容密切相關。與充滿文字的文檔相比,帶有圖像的文檔更具說明性和吸引力。在本文中,您將學習如何使用Spire.Doc for .NET以編程方式在 Word 文檔中插入圖像。通過這個專業的 Word 庫,您還可以設置圖像大小、位置以及環繞樣式。
為 .NET 安裝 Spire.Doc
首先,您需要添加 Spire.Doc for .NET 包中包含的 DLL 文件作為 .NET 項目中的引用。DLL 文件可以從此鏈接下載或通過NuGet安裝。
PM> Install-Package Spire.Doc
在 Word 文檔中插入圖像并設置其環繞樣式
Spire.Doc for .NET 支持常見的環繞樣式,例如 In Line with Text、Square、Tight、Through、Top 和 Bottom、Behind the Text 以及 In Front of Text。以下是插入圖像然后設置其環繞樣式的詳細步驟。
- 創建一個文檔實例。
- 使用Document.LoadFromFile()方法加載示例 Word 文檔。
- 使用Document.Sections[]屬性獲取 Word 文檔的第一部分。
- 使用Section.Paragraphs[]屬性獲取該部分的指定段落。
- 使用Paragraph.AppendPicture()方法加載圖像并在指定段落中插入圖像。
- 使用DocPicture.TextWrappingType屬性設置圖像的環繞樣式。
- 使用Document.SaveToFile()方法將文檔保存到另一個文件。
【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
在 Word 文檔的指定位置插入圖像
Spire.Doc for .NET 提供的DocPicture.Horizo ntalPosition和DocPicture.VerticalPosition屬性允許您在指定位置插入圖像。詳細步驟如下。
- 創建一個文檔實例。
- 使用Document.LoadFromFile()方法加載示例 Word 文檔。
- 使用Document.Sections[]屬性獲取 Word 文檔的第一部分。
- 使用Section.Paragraphs[]屬性獲取該部分的指定段落。
- 使用Paragraph.AppendPicture()方法加載圖像并將圖像插入到文檔中。
- 使用DocPicture.HorizontalPosition和DocPicture.VerticalPosition屬性設置圖像的水平和垂直位置。
- 使用DocPicture.Width和DocPicture.Height屬性設置圖像的高度和寬度。
- 使用DocPicture.TextWrappingType屬性設置圖像的環繞樣式。
- 使用Document.SaveToFile()方法將文檔保存到另一個文件。
【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),如果您有其他問題也可以繼續瀏覽本系列文章,獲取相關教程,你還可以給我留言或者加入我們的官方技術交流群。