IRONSOFTWAREHOME
USING IRONWORD

如何在 C# 中将 Word 转换为 PDF

Curtis Chau
Curtis Chau
Updated: 2026年6月28日

创建、编辑和管理Word 文档是许多应用程序的常见需求。 虽然在 C# 中有多种方法可以创建和编辑 Word 文档,但最强大的方法之一是使用 Microsoft Interop 服务。 借助此工具,您可以非常轻松地以编程方式处理 Word 文档。

前提条件

在搭建环境和开始编写代码之前,请确保满足以下先决条件:

  1. **Visual Studio:**请确保您的计算机已安装 Visual Studio。若未安装,请从微软官方网站下载并安装。
  2. **Microsoft WORD:**由于我们使用 Microsoft Interop,您的计算机上应已安装 Microsoft WORD。 Interop 服务与您计算机上安装的 Microsoft Word 应用程序进行交互。
  3. **基础 C# 知识:**掌握基础的 C# 概念至关重要。
  4. **.NET Framework:**请确保您的 Visual Studio 支持 .NET Framework,因为我们的应用程序将基于此框架构建。

环境设置

首先打开 Visual Studio 应用程序。 打开后,您将看到欢迎屏幕。

1. 创建一个新的 .NET Framework 控制台应用程序

  1. 点击"创建新项目"。
  2. 在搜索框中输入"控制台应用程序(.NET Framework)"。
  3. 从结果中,选择"控制台应用程序 (.NET Framework)",然后单击"下一步"按钮。
  4. 为您的项目命名,然后单击"创建"按钮。

完成这些步骤后,Visual Studio 将为您生成一个新的 .NET Framework 控制台应用程序。 在Program.cs文件中,您将找到一个带有Main方法的基本模板,这是控制台应用程序的入口点。

2. 使用NuGet包管理器安装Microsoft.Office.Interop.Word

NuGet 是 .NET 的包管理器,它集成在 Visual Studio 中。 以下是如何使用它来安装Microsoft.Office.Interop.Word包:

  1. 在 Visual Studio 中,转到"工具"菜单。
  2. 选择"NuGet 程序包管理器",然后选择"管理解决方案的 NuGet 程序包..."。
  3. 在 NuGet 窗口中,单击"浏览"选项卡。
  4. 在搜索框中输入Microsoft.Office.Interop.Word并按回车键。
  5. 从搜索结果中选择Microsoft.Office.Interop.Word包。
  6. 在右侧,确保选中您的控制台应用程序项目,然后单击"安装"按钮。

C#编辑Word(代码示例开发者教程)图1

Visual Studio 现在将安装该程序包,并在您的项目中添加对该程序包的引用。 此软件包包含从 C# 应用程序与MS Word交互所需的程序集和工具。

隆重推出 IronWord:比 Interop 更优越的替代方案

虽然 Interop 为使用 Word 和 Excel 提供了强大的功能,但它也有局限性。 IronWord 是一款功能全面的库,专为 .NET 开发人员优化。 IronWord 比 Interop 提供更流畅的使用体验,尤其是在编辑 Word 文档方面。 它不仅保证了兼容性和性能,而且还通过直观的方法简化了复杂的任务。 为了便于比较,我将在MS Word之后提供每个用例的 IronWord 代码片段,使用 IronWord 版本2024.1.2

打开现有 Word 文档

很多时候,您可能需要编辑现有的 Word 文档,以下示例展示了如何在 C# 中执行此操作:

// Create an instance of the Word Application
var WordApp = new Microsoft.Office.Interop.Word.Application();

// Open a Word document with the specified file path
var WordDoc = WordApp.Documents.Open(@"path_to_your_document.docx");

在上述代码中,将path_to_your_document.docx替换为您docx文件的路径。

使用 IronWord

using IronWord 打开 Word 文档。

// Open a Word document with the specified file path using IronWord
WordDocument doc = new WordDocument(@"path_to_your_document.docx");

创建新的 Word 文档

从头开始创建 Word 文档:

// Initialize a new instance of the Word Application
var WordApp = new Microsoft.Office.Interop.Word.Application();

// Add a new Word document
var WordDoc = WordApp.Documents.Add();

这段代码片段会创建一个新的 Word 文档,您可以使用 C# 对其进行编写和编辑。

使用 IronWord

// Create a new, empty Word document using IronWord
WordDocument doc = new WordDocument();

向 Word 文档添加文本

要添加新的文本段落:

// Add a new paragraph to the document
WordDoc.Paragraphs.Add();
// Assign text to the newly added paragraph
WordDoc.Paragraphs[1].Range.Text = "This is the first paragraph.";

Range.Text属性为其分配新的文本。

使用 IronWord

// Add a new text to the document using IronWord
doc.AddText("Add text using IronWord");

编辑现有文本

在本教程中,让我们修改第一段:

// Edit the text of the first paragraph
WordDoc.Paragraphs[1].Range.Text = "This is the edited first paragraph.";

您还可以使用类似的方法在 Word 文档中添加和编辑其他元素。

使用 IronWord

// Edit the text of the first paragraph using IronWord
doc.Paragraphs[0].TextRuns[0].Text = "This is the edited first paragraph.";

保存并关闭文档

完成所需的修改后:

// Save the document to a specified path
WordDoc.SaveAs(@"path_where_you_want_to_save.docx");
// Close the document and quit the application
WordDoc.Close();
WordApp.Quit();

用您想要的路径替换path_where_you_want_to_save.docx

使用 IronWord

// Save the document to the desired path using IronWord
doc.SaveAs(@"path_where_you_want_to_save.docx");

完整代码和示例

让我们把所有内容整合起来。 以下是一个完整的代码示例,演示如何打开现有的 Word 文档、对其进行编辑,然后保存更改:

var WordApp = new Microsoft.Office.Interop.Word.Application();

// Create a new Word document
var WordDoc = WordApp.Documents.Add();

// Add new text
WordDoc.Paragraphs.Add();
WordDoc.Paragraphs[1].Range.Text = "This is the first paragraph.";

// Edit the first paragraph
WordDoc.Paragraphs[1].Range.Text = "This is the edited first paragraph.";

// Save and close
WordDoc.SaveAs(@"path_where_you_want_to_save.docx");
WordDoc.Close();
WordApp.Quit();

使用 IronWord

using IronWord 的完整代码示例非常简洁。IronWord 利用简洁的代码片段来编辑 DOCX 文件。

// Create an empty Word document
WordDocument doc = new WordDocument();

// Add new text
doc.AddText("This is the first paragraph.");

// Edit the text
doc.Paragraphs[0].TextRuns[0].Text = "This is the edited first paragraph.";

// Export DOCX
doc.SaveAs(@"path_where_you_want_to_save.docx");

结论

在 .NET 应用程序中操作 Word 和 Excel 文档方面,选择非常多。 虽然微软的 Interop 服务一直是许多人的首选,但 IronWord 等解决方案的出现标志着人们正在转向更高效、更人性化的工具。

Curtis Chau
技术作家

Curtis Chau 拥有卡尔顿大学的计算机科学学士学位,专注于前端开发,精通 Node.js、TypeScript、JavaScript 和 React。他热衷于打造直观且美观的用户界面,喜欢使用现代框架并创建结构良好、视觉吸引力强的手册。

...
阅读更多

相关文章

Key in blue circle

立即获取免费的 30 天试用版密钥

Your trial license will be sent to your email address

无任何限制。100% 解锁。无需信用卡。

bullet_checked无需信用卡或创建账户无任何限制。100% 解锁。无需信用卡。
  • Logo Aetna
  • Logo NASA
  • Logo GE
  • Logo Porsche
  • Logo USDA
  • Logo Qatar
Join Millions of Engineers who’ve tried IronPDF
预约您的免费现场演示
Booking Badge

深受全球数百万工程师信赖

Iron Software 的客户徽标
获取您的无义务咨询
填写下面的表格或通过sales@ironsoftware.com
您的资料将始终保密。
深受全球数百万工程师信赖
Iron Software 的客户徽标
立即获取您的免费30 天试用密钥
无需信用卡或创建账户