翻譯|使用教程|編輯:胡濤|2022-09-22 10:17:02.957|閱讀 182 次
概述:在本文中我們將給您介紹如何在 Word 中插入圖像,歡迎查閱~
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關鏈接:
Spire.Doc for .NET是一款專門對 Word 文檔進行操作的 .NET 類庫。在于幫助開發人員無需安裝 Microsoft Word情況下,輕松快捷高效地創建、編輯、轉換和打印 Microsoft Word 文檔。擁有近10年專業開發經驗Spire系列辦公文檔開發工具,專注于創建、編輯、轉換和打印Word/PDF/Excel等格式文件處理,小巧便捷。下面我們將給您介紹如何在 Word 中插入圖像。
Word 文檔中的圖像通常與文本內容密切相關。與充滿文字的文檔相比,帶有圖像的文檔更具說明性和吸引力。在本文中,您將學習如何使用Spire.Doc for .NET以編程方式在 Word 文檔中插入圖像。通過這個專業的 Word 庫,您還可以設置圖像大小、位置以及環繞樣式。
首先,您需要添加 Spire.Doc for .NET 包中包含的 DLL 文件作為 .NET 項目中的引用。DLL 文件可以從此鏈接下載或通過NuGet安裝。
PM> Install-Package Spire.Doc
Spire.Doc for .NET 支持常見的環繞樣式,例如 In Line with Text、Square、Tight、Through、Top 和 Bottom、Behind the Text 以及 In Front of Text。以下是插入圖像然后設置其環繞樣式的詳細步驟。
【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屬性允許您在指定位置插入圖像。詳細步驟如下。
【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),如果您有其他問題也可以繼續瀏覽本系列文章,獲取相關教程,你還可以給我留言或者加入我們的官方技術交流群。
歡迎下載|體驗更多E-iceblue產品
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@ke049m.cn