使用 IRONWORD C# 列印 Word 教學:分步指南 Jordi Bardia 更新:6月 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 文件樣本,以便進行測試。 我們將把這份文件寄給印刷廠。 設定環境 建立一個新的控制台應用程式 1.開啟 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 中建立新的 Console Application,請開啟 IDE,選擇「建立新專案」,搜尋「Console App」,選擇 C# 模板,並依此命名您的專案。 如何為 Word 文件列印加入 Microsoft Interop 函式庫的參考? 在 Visual Studio 中,在專案上按一下滑鼠右鍵,選擇「新增」>「參照」,然後在 COM 索引標籤下選擇「Microsoft Word xx.x 物件庫」。使用 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# 中無縫讀取、寫入和處理 DOCX 檔案,而無需安裝 Microsoft Word。 在 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 檔案 本指南探討如何匯入現有的 Word 文件、顯示其內容,以及使用 IronWord 函式庫從頭建立文件 閱讀更多 更新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 文件,並提供簡單的範例。 閱讀更多