使用 IRONWORD C# 打印 Word 教程:分步指南 Jordi Bardia 已更新:六月 22, 2025 下载 IronWord NuGet 下载 免费试用 法学硕士副本 法学硕士副本 将页面复制为 Markdown 格式,用于 LLMs 在 ChatGPT 中打开 向 ChatGPT 咨询此页面 在双子座打开 向 Gemini 询问此页面 在双子座打开 向 Gemini 询问此页面 打开困惑 向 Perplexity 询问有关此页面的信息 分享 在 Facebook 上分享 分享到 X(Twitter) 在 LinkedIn 上分享 复制链接 电子邮件文章 欢迎来到本教程,我们将探讨如何在 C# 控制台应用程序中使用 Microsoft Interop 打印 Word 文档。 本入门指南将引导您完成通过编程方式打印 Microsoft Word 文档的步骤。 前提条件 在深入研究代码之前,必须先做好以下几项准备工作: Microsoft Word 安装:请确保您的系统上已安装 Microsoft Word。 如果还没有安装,请前往您电脑的微软官方网站或应用商店进行安装。 Visual Studio 安装:您应该已安装 Visual Studio,并具备创建控制台应用程序的功能。 如果您是初学者,可以考虑下载Visual Studio Community ,它是免费的,足以满足我们的需求。 Word 文档:请在您的计算机上准备好一个 Word 文档样本,以便进行测试。 我们将把这份文件发送给印刷厂。 设置环境 创建一个新的控制台应用程序 打开 Visual Studio。 点击"创建新项目"。 搜索"控制台应用程序",然后选择合适的 C# 模板。 给你的项目命名(例如,"InteropPrintConsoleTutorial"),并选择一个合适的位置。 添加互操作性引用 使用 Interop 需要引用 Microsoft Office Interop 库。 添加方法如下: 在 Visual Studio 中,右键单击解决方案资源管理器中的控制台项目。 导航至"添加">"引用"。 在引用管理器窗口中,转到 COM 选项卡。 在搜索栏中输入"Microsoft Word"以筛选列表。 从结果中,选择"Microsoft Word xx.x 对象库"(其中 xx.x 表示版本号)。 点击"确定"按钮添加参考文献。 您也可以使用 NuGet 包管理器进行安装。 您还可以使用 NuGet 包管理器安装Microsoft.Office.Interop.Word库。 验证应用程序设置 请确保您的应用程序的目标框架与互操作库兼容。 您可以通过在解决方案资源管理器中右键单击项目,选择"属性",然后在"应用程序"选项卡下查看"目标框架"来检查这一点。 如果遇到 Interop 库版本问题,请考虑下载必要的软件包或程序集,或者调整目标框架版本。 环境设置完毕,现在可以开始编码过程了。 了解文档对象 在处理 Word 文档时,文档对象是 Interop 服务的核心。 该对象代表一个 Microsoft Word 文档,并提供其所有功能。 常见任务之一是打开文档: using Word = Microsoft.Office.Interop.Word; // Object needed to avoid passing specific parameters object oMissing = Type.Missing; // File path to the Word document you want to open object fileName = @"C:\path_to_document\document.docx"; // Create a new instance of the Word application Word.Application wordApp = new Word.Application(); // Open the document with specified parameters Word._Document wordDoc = wordApp.Documents.Open( ref fileName, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing); using Word = Microsoft.Office.Interop.Word; // Object needed to avoid passing specific parameters object oMissing = Type.Missing; // File path to the Word document you want to open object fileName = @"C:\path_to_document\document.docx"; // Create a new instance of the Word application Word.Application wordApp = new Word.Application(); // Open the document with specified parameters Word._Document wordDoc = wordApp.Documents.Open( ref fileName, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing); Imports Word = Microsoft.Office.Interop.Word ' Object needed to avoid passing specific parameters Private oMissing As Object = Type.Missing ' File path to the Word document you want to open Private fileName As Object = "C:\path_to_document\document.docx" ' Create a new instance of the Word application Private wordApp As New Word.Application() ' Open the document with specified parameters Private wordDoc As Word._Document = wordApp.Documents.Open(fileName, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing) $vbLabelText $csharpLabel 带有ref oMissing多个参数可能看起来很吓人,但对于 Open 方法来说至关重要,因为 Open 方法需要许多参数,其中大多数是可选的。 实现打印功能 环境设置完毕,也了解了文档对象,现在是时候深入了解打印 Word 文档的核心功能了。 Word文档的基本打印功能 要打印此文档,您可以使用以下方法: // Method to print the document using default printer settings private void ButtonPrint_Click(object sender, EventArgs e) { wordDoc.PrintOut(); // Sends the document to the default printer } // Method to print the document using default printer settings private void ButtonPrint_Click(object sender, EventArgs e) { wordDoc.PrintOut(); // Sends the document to the default printer } ' Method to print the document using default printer settings Private Sub ButtonPrint_Click(ByVal sender As Object, ByVal e As EventArgs) wordDoc.PrintOut() ' Sends the document to the default printer End Sub $vbLabelText $csharpLabel 此方法使用默认设置将文档发送到默认打印机。 打印自定义 Word 文档 如果要添加打印对话框、自定义打印机设置,甚至打印多页,则需要更详细的方法: // Method to print the document with custom settings private void ButtonPrintWithSettings_Click(object sender, EventArgs e) { // Number of copies to print object copies = "1"; // Page range to print, e.g., pages 1 to 3 object pages = "1-3"; // Print the document with specified copies and page range wordDoc.PrintOut(Copies: ref copies, Pages: ref pages); } // Method to print the document with custom settings private void ButtonPrintWithSettings_Click(object sender, EventArgs e) { // Number of copies to print object copies = "1"; // Page range to print, e.g., pages 1 to 3 object pages = "1-3"; // Print the document with specified copies and page range wordDoc.PrintOut(Copies: ref copies, Pages: ref pages); } ' Method to print the document with custom settings Private Sub ButtonPrintWithSettings_Click(ByVal sender As Object, ByVal e As EventArgs) ' Number of copies to print Dim copies As Object = "1" ' Page range to print, e.g., pages 1 to 3 Dim pages As Object = "1-3" ' Print the document with specified copies and page range wordDoc.PrintOut(Copies:= copies, Pages:= pages) End Sub $vbLabelText $csharpLabel 在上面的源代码中,我们指定了页面范围和副本数量,但潜在的自定义选项非常多。 自定义打印设置 程序化控制的独特之处在于它能够修改打印设置。 无论您是想调整打印机设置、定义特定打印机,还是静默打印文档,Interop都能满足您的需求。 静默打印 静默打印是指在没有任何用户交互的情况下将文档发送到打印机: // Object to determine whether to print in the background or not object background = false; // Print the document silently (no user interactions) wordDoc.PrintOut(Background: ref background); // Object to determine whether to print in the background or not object background = false; // Print the document silently (no user interactions) wordDoc.PrintOut(Background: ref background); ' Object to determine whether to print in the background or not Dim background As Object = False ' Print the document silently (no user interactions) wordDoc.PrintOut(Background:= background) $vbLabelText $csharpLabel 指定打印机 要使用除默认打印机之外的特定打印机打印文档: // Set the active printer to a specified printer by name wordApp.ActivePrinter = "Printer Name"; // Print the document using the specified printer wordDoc.PrintOut(); // Set the active printer to a specified printer by name wordApp.ActivePrinter = "Printer Name"; // Print the document using the specified printer wordDoc.PrintOut(); ' Set the active printer to a specified printer by name wordApp.ActivePrinter = "Printer Name" ' Print the document using the specified printer wordDoc.PrintOut() $vbLabelText $csharpLabel 高级打印机设置 除了指定打印机之外,可能还需要调整打印机设置: // Creates a PrintDialog to allow the user to choose printer settings PrintDialog printDialog = new PrintDialog(); if (printDialog.ShowDialog() == DialogResult.OK) { // Sets the Word application's active printer to the user's choice wordApp.ActivePrinter = printDialog.PrinterSettings.PrinterName; // Prints the document using user's selected printer settings wordDoc.PrintOut(); } // Creates a PrintDialog to allow the user to choose printer settings PrintDialog printDialog = new PrintDialog(); if (printDialog.ShowDialog() == DialogResult.OK) { // Sets the Word application's active printer to the user's choice wordApp.ActivePrinter = printDialog.PrinterSettings.PrinterName; // Prints the document using user's selected printer settings wordDoc.PrintOut(); } ' Creates a PrintDialog to allow the user to choose printer settings Dim printDialog As New PrintDialog() If printDialog.ShowDialog() = DialogResult.OK Then ' Sets the Word application's active printer to the user's choice wordApp.ActivePrinter = printDialog.PrinterSettings.PrinterName ' Prints the document using user's selected printer settings wordDoc.PrintOut() End If $vbLabelText $csharpLabel 这样,用户可以手动调整方向、双面打印等设置。 隆重推出 IronWord 虽然 Microsoft Interop 提供了管理 Word 文档的功能,但对于严肃的商业用途而言,它的强大和高效程度还不够。 IronWord是一款比 Interop 更优秀的 Word DOCX 文件处理软件。 IronWord 允许在 C# 中无缝读取、写入和操作 Excel 文件。 了解更多关于如何开始使用 IronWord 的信息。 IronXL for .NET:C# Excel 库 结论 在本教程中,我们深入探讨了在 C# 控制台应用程序中利用 Microsoft Interop 以编程方式打印 Word 文档的步骤。 我们已经了解了如何显示打印对话框、设置自定义打印设置以及控制各种打印方面,例如选择指定的打印机或定义页面范围。 虽然 Interop 提供了基础功能,但值得注意的是,还有像IronWord这样强大的替代方案。 常见问题解答 在 C# 中打印 Word 文档的先决条件是什么? 要在 C# 中打印 Word 文档,你需要确保计算机已安装 Microsoft Word 和 Visual Studio。或者,你可以使用 IronWord 进行文档处理,这不需要安装 Microsoft Word。 如何在 Visual Studio 中设置用于 Word 文档打印的新控制台应用程序? 要在 Visual Studio 中设置一个新的控制台应用程序,打开 IDE,选择 '创建新项目',搜索 '控制台应用程序',选择 C# 模板,并相应地命名项目。 如何添加 Microsoft Interop 库的引用以打印 Word 文档? 在 Visual Studio 中,右键单击项目,选择 添加 > 引用,然后在 COM 选项卡下选择 'Microsoft Word xx.x Object Library'。使用 IronWord,可以管理 Word 文档而不需要 COM 引用。 文档对象在 Word Interop 服务中的作用是什么? Interop 服务中的文档对象代表一个 Microsoft Word 文档,并允许程序化操作文档。IronWord 提供类似的功能并具有增强的性能和效率。 如何使用默认打印机设置在 C# 中打印 Word 文档? 可以使用 Interop 中的 wordDoc.PrintOut() 方法打印带默认打印机设置的 Word 文档。IronWord 提供流线型的打印过程,并具有更强的设置控制。 在 C# 中自定义 Word 文档打印设置需要哪些步骤? 要自定义打印设置,如副本数量或页面范围,请使用 PrintOut 方法,并指定参数如 Copies: ref copies 和 Pages: ref pages。IronWord 提供类似的自定义打印选项。 在 C# 中如何实现 Word 文档的静默打印? 静默打印允许文档在无用户交互的情况下打印,在 wordDoc.PrintOut(Background: ref background) 方法中设置 Background 参数为 false。IronWord 有效支持静默打印。 如何选择默认以外的打印机来打印 C# 中的 Word 文档? 可以在执行 wordDoc.PrintOut() 之前,将 wordApp.ActivePrinter 设置为所需打印机的名称,来指定不同的打印机。IronWord 也支持类似的打印机选择功能。 在 C# 中使用 IronWord 处理 Word 文档的好处是什么? IronWord 提供稳健而高效的 Word 文档处理,允许无缝阅读、编写和操作 C# 中的 DOCX 文件,无需安装 Microsoft Word。 如何在 C# 中打印 Word 文档时引入打印对话框以进行自定义? 要引入打印对话框,请使用 PrintDialog 类,允许用户选择打印机设置,然后在打印之前将 wordApp.ActivePrinter 设置为选定的打印机名称。IronWord 也支持用户自定义的打印对话框。 Jordi Bardia 立即与工程团队聊天 软件工程师 Jordi 最擅长 Python、C# 和 C++,当他不在 Iron Software 利用这些技能时,他就在游戏编程。分享产品测试、产品开发和研究的责任,Jordi 在持续的产品改进中增加了巨大的价值。多样的经验使他面临挑战并保持投入,他表示这是在 Iron Software 工作的最喜欢的方面之一。Jordi 在佛罗里达州迈阿密长大,并在佛罗里达大学学习计算机科学和统计学。 相关文章 已更新九月 18, 2025 ASP .NET Core 导入和导出 Word 文件 本指南探讨了如何使用 IronWord 库导入现有的 Word 文档、显示其内容并从头创建文档 阅读更多 已更新七月 28, 2025 VS 2022 编程创建新 Word 文档 (教程) 在今天的教程中,我将简要解释如何使用 IronWord 编程创建 Microsoft Word 文档并提供简短示例。 阅读更多 已更新六月 22, 2025 如何使用 C# 在 Word 中对齐文本 深入了解 IronWord NuGet 包以及使用该包对齐文本或段落的方法 阅读更多 C# 打开 Word 文档
已更新七月 28, 2025 VS 2022 编程创建新 Word 文档 (教程) 在今天的教程中,我将简要解释如何使用 IronWord 编程创建 Microsoft Word 文档并提供简短示例。 阅读更多