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

这篇具有很好参考价值的文章主要介绍了Word控件Spire.Doc 【图像形状】教程(1) ;如何在 Word 中插入图像(C#/VB.NET)。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

Spire.Doc for .NET是一款专门对 Word 文档进行操作的 .NET 类库。在于帮助开发人员无需安装 Microsoft Word情况下,轻松快捷高效地创建、编辑、转换和打印 Microsoft Word 文档。拥有近10年专业开发经验Spire系列办公文档开发工具,专注于创建、编辑、转换和打印Word/PDF/Excel等格式文件处理,小巧便捷。下面我们将给您介绍如何在 Word 中插入图像。

Spire.Doc for.NET 最新下载https://www.evget.com/product/3368/download

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 【图像形状】教程(1) ;如何在 Word 中插入图像(C#/VB.NET)

在 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 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控件Spire.Doc 【图像形状】教程(1) ;如何在 Word 中插入图像(C#/VB.NET)

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

到了这里,关于Word控件Spire.Doc 【图像形状】教程(1) ;如何在 Word 中插入图像(C#/VB.NET)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • Word控件Spire.Doc 【超链接】教程(3):在C#中查找word文档中的超链接

    Spire.Doc for .NET是一款专门对 Word 文档进行操作的 .NET 类库。在于帮助开发人员无需安装 Microsoft Word情况下,轻松快捷高效地创建、编辑、转换和打印 Microsoft Word 文档。拥有近10年专业开发经验Spire系列办公文档开发工具,专注于创建、编辑、转换和打印Word/PDF/Excel等格式文件处

    2024年02月04日
    浏览(35)
  • Spire.doc实现对word的操作(包括文字,表格,图片)

    Spire.doc比较小众,因此需要在pom.xml中导入spire.doc的仓库,导入之后直接导包 导入仓库后进行导包 2.1.1模板 首先需要准备一个word模板,可以在里面进行文字替换,因为文字特别多,所以此处采用了对具有特殊符号的文字进行替换。此处采用 ${xxx} 模板如图所示: 2.1.2替换核心

    2023年04月12日
    浏览(41)
  • java 将word转为pdf文件的两种方式【spire.doc.free】【documents4j】

    如资产证明等场景下,一般要求同时生成word与pdf两种格式的证明文件,且两者格式需保持一致,可以各自单独生成,但那样可能需要维护两个模板文件,所以也可以仅定义一份word的模板文件,使用模板生成word文件,再将word转换为pdf,这样不仅少维护一个模板,也可以保证

    2024年02月12日
    浏览(47)
  • 如何将 Spire.Doc for C++ 集成到 C++ 程序中

    Spire.Doc for C++ 是一个专业的 Word 库,供开发人员在任何类型的 C++ 应用程序中阅读、创建、编辑、比较和转换 Word 文档。 本文演示了如何以两种不同的方式将 Spire.Doc for C++ 集成到您的 C++ 应用程序中。 通过 NuGet 安装 Spire.Doc for C++ 通过手动导入库安装 Spire.Doc for C++ 步骤1 在

    2023年04月27日
    浏览(27)
  • 借助文档控件Aspose.Words,将 Word DOC/DOCX 转换为 TXT

    在文档处理领域,经常需要将 Word 文档转换为更简单的纯文本格式。无论是出于数据提取、内容分析还是兼容性原因,将 Word(.doc、.docx)文件转换为纯文本(.txt)的能力对于开发人员来说都是一项宝贵的技能。在这篇博文中,我们将探讨如何在 C# 应用程序中将 Word 文档转换

    2024年01月19日
    浏览(41)
  • PDF控件Spire.PDF for .NET【安全】演示:使用文本或/和图像对 PDF 进行数字签名

    数字签名确保签名的文档不能被除其作者之外的任何人更改。添加签名是确保文档内容真实性的最常见方法。PDF 文档中的可视数字签名可以显示文本或图像(例如手写签名)。本文从以下三个方面介绍如何使用Spire.PDF for .NET对PDF 进行数字签名。 Spire.PDF for .NET 是一款独立

    2024年01月23日
    浏览(35)
  • Spire.Doc

    Free Spire.Doc for Java  是一款免费、专业的 Java Word 组件,开发人员使用它可以轻松地将 Word 文档创建、读取、编辑、转换和打印等功能集成到自己的 Java 应用程序中。作为一款完全独立的组件,Free Spire.Doc for Java的运行环境无需安装 Microsoft Office。 Free Spire.Doc for Java 能执行多种

    2024年02月09日
    浏览(31)
  • spire.doc.jar

    https://mvnrepository.com/ https://mvnrepository.com/search?q=spire.doc https://mvnrepository.com/artifact/e-iceblue/spire.doc ==== https://repo.e-iceblue.cn/repository/maven-public https://repo.e-iceblue.cn/#browse/browse:maven-public

    2024年02月12日
    浏览(31)
  • 如何修复损坏的DOC和DOCX格式Word文件?

    我们日常办公中,经常用到Word文档。但是有时会遇到word文件损坏、无法打开的情况。这时该怎么办?接着往下看, 小编在这里就给大家带来最简单的Word文件修复方法 ! 很多时候DOC和DOCX Word文件会无缘无故的损坏无法打开,一般来说导致word文件损坏的常见原因如下: 恶意

    2024年02月13日
    浏览(29)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包