C# 列印 Word 教學:逐步指南
歡迎來到本教學課程,我們將探討如何在 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 套件管理器進行安裝。
驗證應用程式設定
請確保您的應用程式的目標框架與互通庫相容。 您可以透過在解決方案資源管理器中右鍵單擊項目,選擇"屬性",然後在"應用程式"標籤下查看"目標框架"來檢查這一點。 如果遇到 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);具有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
}此方法使用預設設定將文件傳送到預設印表機。
列印自訂 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);
}在上面的原始程式碼中,我們指定了頁面範圍和副本數量,但潛在的自訂選項非常多。
自訂列印設定
程序化控制的獨特之處在於它能夠修改列印設定。 無論您是想調整印表機設定、定義特定印表機,還是想靜默列印文檔,使用 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);指定印表機
若要使用預設印表機以外的特定印表機列印文件:
// 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();進階印表機設定
除了指定印表機之外,可能還需要調整印表機設定:
// 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();
}這樣,使用者可以手動調整方向、雙面列印等設定。
隆重介紹 IronWord
雖然 Microsoft Interop 提供了管理 Word 文件的功能,但對於嚴肅的商業用途而言,它的強大和高效程度還不夠。 IronWord是一款比 Interop 更優秀的 Word DOCX 檔案處理軟體。 IronWord 允許在 C# 中無縫讀取、寫入和操作 Excel 檔案。 了解更多關於如何開始使用 IronWord 的資訊。
結論
在本教學中,我們深入探討了在 C# 控制台應用程式中利用 Microsoft Interop 以程式設計方式列印 Word 文件的步驟。 我們已經了解如何顯示列印對話方塊、設定自訂列印設定以及控制各種列印方面,例如選擇指定的印表機或定義頁面範圍。 雖然 Interop 提供了基礎功能,但值得注意的是,還有像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 物件庫”。使用 IronWord,您可以管理 Word 文件而無需 COM 引用。
文件物件在 Word 互通服務中扮演什麼角色?
Interop 服務中的文檔物件代表 Microsoft Word 文檔,並允許以程式設計方式操作該文檔。 IronWord 提供類似的功能,但效能和效率更高。
如何在C#中使用預設印表機設定列印Word文檔?
您可以使用 Interop 中的wordDoc.PrintOut()方法,以預設印表機設定列印 Word 文件。 IronWord 提供了一種簡化的列印流程,並讓您更精細地控制設定。
在 C# 中自訂 Word 文件的列印設定需要哪些步驟?
若要自訂列印設置,例如份數或頁數範圍,請使用指定參數的PrintOut方法,例如Copies: ref copies和Pages: ref pages也提供了類似的自訂列印選項。
C#中Word文檔的靜默列印是如何實現的?
靜默列印允許透過在wordDoc.PrintOut(Background: ref background)方法中將Background參數設為 false 來列印文檔,而無需使用者互動。 IronWord 對靜默列印提供了高效率的支援。
在 C# 中,如何選擇預設印表機以外的其他印表機來列印 Word 文件?
您可以透過在執行wordDoc.PrintOut()之前將wordApp.ActivePrinter設定為所需印表機的名稱來指定不同的印表機。 IronWord 也提供了類似的印表機選擇功能。
使用 IronWord 在 C# 中處理 Word 文件有哪些好處?
IronWord 提供強大且高效的 Word 文件處理功能,無需安裝 Microsoft Word 即可在 C# 中無縫讀取、寫入和操作 DOCX 檔案。
如何在C#中為列印Word文件新增自訂列印對話方塊?
若要新增列印對話框,請使用PrintDialog類別讓使用者選擇印表機設置,然後在列印前將wordApp.ActivePrinter設定為所選印表機名稱。 IronWord 也支援使用者自訂列印對話框。







