使用 IRONWORD C# 列印 Word 教程:逐步指南 Jordi Bardia 更新日期:6月 22, 2025 Download IronWord NuGet 下載 Start Free Trial Copy for LLMs Copy for LLMs Copy page as Markdown for LLMs Open in ChatGPT Ask ChatGPT about this page Open in Gemini Ask Gemini about this page Open in Grok Ask Grok about this page Open in Perplexity Ask Perplexity about this page Share Share on Facebook Share on X (Twitter) Share on LinkedIn Copy URL Email article Welcome to this tutorial, where we will explore how to print Word documents using the Microsoft Interop in a C# Console Application. This beginner-friendly guide will walk you through the steps to programmatically print Microsoft Word documents. Prerequisites Before diving into the code, it's essential to have a few things set up: Microsoft Word Installation: Ensure you have Microsoft Word installed on your system. If not, head to your computer's official Microsoft website or the app store to install it. Visual Studio Setup: You should have Visual Studio installed with the capability to create a Console Application. If you're a beginner, consider downloading Visual Studio Community, which is free and sufficient for our needs. A Word Document: Have a sample Word document ready on your machine for testing purposes. This will be the document we'll be sending to the printer. Setting Up the Environment Create a New Console Application Open Visual Studio. Click on "Create a new project." Search for "Console App" and select the appropriate C# template. Name your project (e.g., "InteropPrintConsoleTutorial") and choose a suitable location. Adding Interop Reference Using Interop requires a reference to the Microsoft Office Interop library. Here's how to add it: In Visual Studio, right-click on your Console project in the Solution Explorer. Navigate to Add > Reference. In the Reference Manager window, go to the COM tab. Type "Microsoft Word" in the search bar to filter the list. From the results, select "Microsoft Word xx.x Object Library" (where xx.x denotes the version number). Click on the OK button to add the reference. You can also install it using the NuGet Package Manager. Verify Application Settings Ensure that your application's target framework is compatible with the Interop library. You can check this by right-clicking on your project in Solution Explorer, selecting Properties, and then viewing the Target framework under the Application tab. If you face issues with the Interop library's version, consider downloading the necessary package or assembly or adjusting the target framework version. With the environment set up, you can now proceed with the coding process. Understanding the Document Object The document object is at the heart of the Interop services when dealing with Word documents. This object represents a Microsoft Word document and provides all its functionalities. A common task is opening a document: 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 The multiple parameters with ref oMissing might seem daunting, but it's essential for the Open method, which expects numerous arguments, most of which are optional. Implementing the Print Functionality With our environment set up and understanding the document object, it's time to dive into the core functionality of printing Word documents. Basic Printing of Word Document To print the document, you can use the following method: // 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 This method sends the document to the default printer using the default settings. Print Word Documents with Customizations If you want to introduce a print dialog, customize printer settings, or even print multiple pages, you'd require a more detailed approach: // 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 In the above source code, we specify the page range and number of copies, but the potential customizations are vast. Customizing Print Settings The ability to modify print settings is what sets programmatic control apart. Whether you want to adjust the printer settings, define a specific printer, or even silently print the document, it's all within reach with Interop. Silent Printing Silent printing is all about sending the document to the printer without any user interactions: // 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 Specifying a Printer To print a document on a specific printer other than the default: // 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 Advanced Printer Settings Beyond just specifying the printer, one might need to adjust the printer settings: // 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 This way, the user can manually adjust settings like orientation, duplex printing, and more. Introducing IronWord While Microsoft Interop provides functionalities to manage Word documents, it's not as robust and efficient as it should be for serious commercial use. Enter IronWord—a superior alternative to Interop for Word DOCX file processing. IronWord allows for seamless reading, writing, and manipulation of Excel files in C#. Learn more about how to get started with IronWord. Conclusion In this tutorial, we've delved into the steps involved in leveraging Microsoft Interop to print Word documents programmatically in a C# Console Application. We've seen how to show the print dialog, set custom print settings, and control various printing aspects like choosing a specified printer or defining a page range. While Interop offers foundational capabilities, it's worth noting that there are potent alternatives like IronWord. 常見問題解答 在 C# 中列印 Word 文件的先決條件是什麼? 要在 C# 中列印 Word 文件,您需要在電腦上安裝 Microsoft Word 和 Visual Studio。或者,您可以使用 IronWord 處理文檔,這不需要安裝 Microsoft Word。 如何在 Visual Studio 中設置新的控制台應用程式以進行 Word 文檔列印? 在 Visual Studio 中設置新的控制台應用程式,打開 IDE,選擇“建立新專案”,搜尋“控制台應用”,選擇 C# 模板,並根據需要命名您的專案。 如何為 Word 文檔列印添加 Microsoft Interop 程式庫的參考? 在 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 為印表機選擇提供類似的功能。 使用 IronWord 以 C# 處理 Word 文件有哪些好處? IronWord 提供了穩健且高效的 Word 文檔處理功能,允許在 C# 中無需安裝 Microsoft Word 即可無縫地讀取、寫入和操作 DOCX 文件。 如何在 C# 中引入列印對話框以進行自定義列印 Word 文檔? 要引入列印對話框,使用 PrintDialog 類讓用戶選擇印表機設置,然後在列印之前設置 wordApp.ActivePrinter 為選擇的印表機名稱。IronWord 也支持用戶自定義列印對話框。 Jordi Bardia 立即與工程團隊聊天 軟體工程師 Jordi 在 Python、C# 和 C++ 上最得心應手,當他不在 Iron Software 展現技術時,便在做遊戲編程。在分担产品测测试,产品开发和研究的责任时,Jordi 为持续的产品改进增值。他说这种多样化的经验使他受到挑战并保持参与, 而这也是他与 Iron Software 中工作一大乐趣。Jordi 在佛罗里达州迈阿密长大,曾在佛罗里达大学学习计算机科学和统计学。 相關文章 更新日期 9月 18, 2025 ASP .NET Core 導入和導出 Word 文件 本指南探討如何使用 IronWord 庫導入現有的 Word 文件,顯示其內容,並從頭開始創建文件 閱讀更多 更新日期 7月 28, 2025 VS 2022 程式化創建新 Word 文件(教程) 在今天的教程中,我將簡單解釋如何使用 IronWord 程式化創建 Microsoft Word 文檔,並提供簡單範例。 閱讀更多 更新日期 6月 22, 2025 如何使用 C# 對齊 Word 中的文本 讓我們深入了解 IronWord NuGet 包,了解如何使用此包對齊文本或段落 閱讀更多 C# 開啟 Word 文檔
更新日期 7月 28, 2025 VS 2022 程式化創建新 Word 文件(教程) 在今天的教程中,我將簡單解釋如何使用 IronWord 程式化創建 Microsoft Word 文檔,並提供簡單範例。 閱讀更多