跳至頁尾內容
USING IRONWORD

C# 列印 Word 教程:逐步指南

歡迎參加本教程,我們將探索如何在C#主控台應用程式中使用Microsoft Interop列印Word文件。 這份面向初學者的指南將引導您完成程式化列印Microsoft Word文件的步驟。

先決條件

在進入程式碼之前,設定一些必要條件是至關重要的:

  • Microsoft Word安裝:確保您的系統上已安裝Microsoft Word。 如果沒有,請前往您電腦的官方Microsoft網站或應用商店安裝它。

  • Visual Studio設定:您應該已安裝Visual Studio,並具備建立Console Application的能力。 如果您是初學者,考慮下載Visual Studio Community,它是免費的,並且足夠滿足我們的需求。

  • Word文件:在您的機器上準備一個測試用途的範例Word文件。 我們將把這個文件發送到印表機。

設置環境

建立新的主控台應用程式

  1. 打開Visual Studio。
  2. 點擊"建立新專案"。
  3. 搜尋"Console App"並選擇適合的C#模板。
  4. 為您的專案命名(例如,"InteropPrintConsoleTutorial")並選擇合適的位置。

新增Interop引用

使用Interop需要引用Microsoft Office Interop庫。 以下是新增操作的方法:

  1. 在Visual Studio中,右鍵單擊Solution Explorer中的Console專案。
  2. 移至新增 > 參考。
  3. 在參考管理器窗口中,轉到COM選項卡。
  4. 在搜尋欄中輸入"Microsoft Word"以篩選列表。
  5. 從結果中選擇"Microsoft Word xx.x Object Library"(其中xx.x表示版本號)。
  6. 點擊確定按鈕以新增參考。

您也可以使用NuGet包管理器安裝它。

Microsoft.Office.Interop.Word程式庫。

驗證應用程式設置

確保應用程式的目標框架與Interop庫相容。 您可以通過右鍵單擊Solution Explorer中的專案,選擇屬性,然後在應用程式選項卡下查看目標框架來檢查這一點。 如果您在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方法而言是至關重要的,該方法預期有多個參數,其中大多數是可選的。

實施列印功能

在環境設置和了解文件物件後,是時候深入理解列印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程式庫

結論

在本教程中,我們深入探討了使用Microsoft Interop在C#主控台應用程式中程式化列印Word文件的步驟。 我們已展示如何顯示列印對話框,設置自訂列印設置,並控制各種列印方面,如選擇指定印表機或定義頁碼範圍。 雖然Interop提供了基礎功能,但值得注意的是,還有如IronWord這樣強大的替代方案。

常見問題

列印 Word 文件在 C# 中的前置需求有哪些?

要在 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 物件程式庫」。使用 IronWord,您可以管理 Word 文件而無需 COM 引用。

文件物件在 Word Interop 服務中的角色是什麼?

Interop 服務中的文件物件代表一個 Microsoft Word 文件,允許對文件進行程式化的操作。IronWord 提供了類似的功能,具有更高的性能和效率。

如何在 C# 中使用預設的列印機設置列印 Word 文件?

您可以通過在 Interop 中使用 wordDoc.PrintOut() 方法來列印帶有預設列印機設置的 Word 文件。IronWord 提供了一個更流暢的列印過程,對設置有更多的控制。

定制 C# 中列印 Word 文件的列印設置涉及哪些步驟?

要定制列印設置,例如份數或頁面範圍,使用給定參數(如 Copies: ref copiesPages: ref pages)的 PrintOut 方法。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 也支持使用者自訂列印對話框。

Curtis Chau
技術作家

Curtis Chau擁有Carleton大學的電腦科學學士學位,專精於前端開發,擁有Node.js、TypeScript、JavaScript和React的專業知識。Curtis熱衷於建立直觀且美觀的使用者介面,喜愛使用現代框架並建立結構良好、視覺吸引力的手冊。

除了開發,Curtis對物聯網(IoT)有濃厚的興趣,探索創新的方法來整合硬體和軟體。在空閒時間,他喜歡玩遊戲和建立Discord機器人,結合他對技術的熱愛與創造力。

Iron 支援團隊

我們線上24小時,每週5天。
聊天
電子郵件
給我打電話