借助 Aspose.Words,在 C# 中将图片转换为 Word

Microsoft Word 提供了多种用于生成具有增强的格式化功能的文本文档的工具。除了文本格式之外,我们还可以将各种图形元素和图像合并到Word文档中。在某些情况下,我们可能需要将图片或照片插入DOC或DOCX格式的Word文档中。在本文中,我们将学习如何使用 C# 将图片转换为 Word 文档。

**Aspose.Words**是一种高级Word文档处理API,用于执行各种文档管理和操作任务。API支持生成,修改,转换,呈现和打印文档,而无需在跨平台应用程序中直接使用Microsoft Word。

Aspose API支持流行文件格式处理,并允许将各类文档导出或转换为固定布局文件格式和最常用的图像/多媒体格式。

Aspose.words for.net下载 Aspose.words for for java下载

C# 图片到文字转换器 - 免费下载

我们将使用Aspose.Words for .NET将图片转换为Word 文档。它使开发人员能够从 .NET 应用程序中创建和操作 Word 文档。除了一系列强大的功能之外,它还提供了一个内置转换器,可以将文档和图像转换为其他格式。

请下载 API 的 DLL或使用包管理器控制台从NuGet安装它。

复制代码
PM> Install-Package Aspose.Words
如何将图片转换为Word

我们可以按照以下步骤轻松地将任何图片或照片从JPG或PNG格式转换为 Word 文档:

  1. 在您的应用程序中安装 Aspose.Words for .NET。
  2. 创建一个新的 DOCX 文档。
  3. 使用图像文件的路径将图片插入文档中。
  4. 将文档保存在您所需的位置。
在 C# 中将图片转换为 Word

现在让我们看看如何将上述步骤翻译成 C# 代码。以下是用 C# 将图片转换为 DOCX 的步骤。

  1. 创建Document类的对象。
  2. 创建DocumentBuilder类的对象。
  3. 使用DocumentBuilder.InsertImage(string)方法按路径将图片插入到文档中。
  4. 使用Document.Save(string)方法保存 DOCX 文档。

以下代码示例展示了如何使用 C# 将图片转换为 DOCX

复制代码
// This code example demonstrates how to convert a picture to a Word document!
using Aspose.Words;

// create new document
Document doc = new Document();

// create and initialize document builder
DocumentBuilder builder = new DocumentBuilder(doc);

// insert picture to the document
builder.InsertImage("C:\\Files\\tower.jpg");

// save the document
doc.Save("C:\\Files\\Output.docx");
将图片插入现有Word文档

同样,我们也可以将图片插入到现有的Word文档中,步骤如下:

  1. 使用Document类加载现有文档。
  2. 创建DocumentBuilder类的对象。
  3. **使用MoveToDocumentEnd()**方法导航到文档末尾。
  4. **使用InsertBreak(BreakType.PageBreak)**方法插入分页符。
  5. **使用DocumentBuilder.InsertImage(string)**方法按路径将图片插入到文档中。
  6. **最后,使用Document.Save(string)**方法保存 DOCX 文档。

以下代码示例展示了如何使用 C# 将图片插入到现有的 Word 文档中

复制代码
// This code example demonstrates how to insert a picture into a Word document!
using Aspose.Words;

// create new document
Document doc = new Document("C:\\Files\\Document.docx");

// create and initialize document builder
DocumentBuilder builder = new DocumentBuilder(doc);

// move to the end of the document
builder.MoveToDocumentEnd();

// insert a new page
builder.InsertBreak(BreakType.PageBreak);

// insert picture to document
builder.InsertImage("C:\\Files\\tower.jpg");

// save the document
doc.Save("C:\\Files\\Output_1.docx");
在线将图片转换为Word

您还可以使用这个免费的图片到 Word 转换器Web 应用程序在线将图片转换为 Word 文档,该应用程序是使用此 API 开发的。

在本文中,我们学习了如何使用 C# 将图片转换为 Word 文档。通过利用 Aspose.Words for .NET,您可以简化文档自动化任务。它是一个功能强大且多功能的文档处理库,为在各种 .NET 应用程序中生成、修改和呈现 Word 文件提供了强大的解决方案。

相关推荐
我是苏苏33 分钟前
C#基础:Winform桌面开发中窗体之间的数据传递
开发语言·c#
斐波娜娜1 小时前
Maven详解
java·开发语言·maven
bing_1581 小时前
Word 怎么让字变大、变粗、换颜色?
word
小码氓1 小时前
Java填充Word模板
java·开发语言·spring·word
会飞的天明1 小时前
Java 导出word 实现饼状图导出--可编辑数据
java·word
NingrLi1 小时前
Word中字号与公式字体磅值(pt)的对应关系
word
暮鹤筠2 小时前
[C语言初阶]操作符
c语言·开发语言
Boilermaker19923 小时前
【Java EE】Mybatis-Plus
java·开发语言·java-ee
aramae4 小时前
C++ -- STL -- vector
开发语言·c++·笔记·后端·visual studio
Tony小周4 小时前
实现一个点击输入框可以弹出的数字软键盘控件 qt 5.12
开发语言·数据库·qt