Word控件Spire.Doc 【图像形状】教程(1) ;如何在 Word 中插入图像(C#/VB.NET)

为 .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 instanceDocument document = new Document();//Load a sample Word documentdocument.LoadFromFile("input.docx");//Get the first sectionSection section = document.Sections[0];//Get two specified paragraphsParagraph para1 = section.Paragraphs[5];Paragraph para2 = section.Paragraphs[9];//Insert images in the specified paragraphsDocPicture Pic1 = para1.AppendPicture(Image.FromFile(@"C:UsersAdministratorDesktoppic1.jpg"));DocPicture Pic2 = para2.AppendPicture(Image.FromFile(@"C:UsersAdministratorDesktoppic2.png"));//Set wrapping styles to Square and Inline respectivelyPic1.TextWrappingStyle = TextWrappingStyle.Square;Pic2.TextWrappingStyle = TextWrappingStyle.Inline;//Save the document to filedocument.SaveToFile("InsertImage.docx", FileFormat.Docx);}}}

【VB.NET】

Imports System.DrawingImports Spire.DocImports Spire.Doc.DocumentsImports Spire.Doc.FieldsNamespace WordImageClass ImageinWordPrivate Shared Sub Main(ByVal args As String())'Create a Document instanceDim document As Document = New Document()'Load a sample Word documentdocument.LoadFromFile("sample.docx")'Get the first sectionDim section As Section = document.Sections(0)'Get two specified paragraphsDim para1 As Paragraph = section.Paragraphs(5)Dim para2 As Paragraph = section.Paragraphs(9)'Insert images in the specified paragraphsDim Pic1 As DocPicture = para1.AppendPicture(Image.FromFile("C:UsersAdministratorDesktoppic1.jpg"))Dim Pic2 As DocPicture = para2.AppendPicture(Image.FromFile("C:UsersAdministratorDesktoppic2.png"))‘Set wrapping styles to Square and Inline respectivelyPic1.TextWrappingStyle = TextWrappingStyle.SquarePic2.TextWrappingStyle = TextWrappingStyle.Inline'Save the document to filedocument.SaveToFile("InsertImage.docx", FileFormat.Docx)End SubEnd ClassEnd Namespace

C#/VB.NET:在 Word 中插入图像
在 Word 文档的指定位置插入图像

Spire.Doc for .NET 提供的DocPicture.Horizo ntalPositionDocPicture.VerticalPosition属性允许您在指定位置插入图像。详细步骤如下。

  • 创建一个文档实例。
  • 使用Document.LoadFromFile()方法加载示例 Word 文档。
  • 使用Document.Sections[]属性获取 Word 文档的第一部分。
  • 使用Section.Paragraphs[]属性获取该部分的指定段落。
  • 使用Paragraph.AppendPicture()方法加载图像并将图像插入到文档中。
  • 使用DocPicture.HorizontalPositionDocPicture.VerticalPosition属性设置图像的水平和垂直位置。
  • 使用DocPicture.WidthDocPicture.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 instanceDocument document = new Document();//Load a sample Word documentdocument.LoadFromFile("input.docx");//Get the first sectionSection section = document.Sections[0];//Load an image and insert it to the documentDocPicture picture = section.Paragraphs[0].AppendPicture(Image.FromFile(@"C:UsersAdministratorDesktoppic.jpg"));//Set the position of the imagepicture.HorizontalPosition = 90.0F;picture.VerticalPosition = 50.0F;//Set the size of the imagepicture.Width = 150;picture.Height = 150;//Set the wrapping style to Behindpicture.TextWrappingStyle = TextWrappingStyle.Behind;// Save the document to filedocument.SaveToFile("Insert.docx", FileFormat.Docx);}}}

【VB.NET】

Imports Spire.DocImports Spire.Doc.DocumentsImports Spire.Doc.FieldsNamespace InsertImageClass ProgramPrivate Shared Sub Main(ByVal args As String())'Create a Document instanceDim document As Document = New Document()'Load a sample Word documentdocument.LoadFromFile("input.docx")'Get the first sectionDim section As Section = document.Sections(0)'Load an image and insert it to the documentDim picture As DocPicture = section.Paragraphs(0).AppendPicture(Image.FromFile("C:UsersAdministratorDesktoppic.jpg"))'Set the position of the imagepicture.HorizontalPosition = 90.0Fpicture.VerticalPosition = 50.0F'Set the size of the imagepicture.Width = 150picture.Height = 150' Set the wrapping style to Behindpicture.TextWrappingStyle = TextWrappingStyle.Behind' Save the document to filedocument.SaveToFile("Insert.docx", FileFormat.Docx)End SubEnd ClassEnd Namespace

C#/VB.NET:在 Word 中插入图像

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


欢迎下载|体验更多E-iceblue产品

获取更多信息请咨询在线客服  


标签:

声明:本站部分文章及图片源自用户投稿,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!

上一篇 2022年8月19日
下一篇 2022年8月19日

相关推荐

发表回复

登录后才能评论