借助 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 文件提供了强大的解决方案。

相关推荐
十八朵郁金香4 分钟前
通俗易懂的DOM1级标准介绍
开发语言·前端·javascript
阿尔法波7 分钟前
python与pycharm如何设置文件夹为源代码根目录
开发语言·python·pycharm
xing251616 分钟前
pytest下allure
开发语言·python·pytest
眸笑丶20 分钟前
使用 Python 调用 Ollama API 并调用 deepseek-r1:8b 模型
开发语言·python
enyp8041 分钟前
Qt QStackedWidget 总结
开发语言·qt
gu201 小时前
c#编程:学习Linq,重几个简单示例开始
开发语言·学习·c#·linq
lly2024061 小时前
SQLite 删除表
开发语言
wjs20241 小时前
HTML 字符实体
开发语言
二十雨辰1 小时前
[Java基础]网络编程
java·开发语言
AC使者1 小时前
介绍 TensorFlow 的基本概念和使用场景。
开发语言·自然语言处理·sqlite·github