使用IRONWORD

C# 列印 Word(開發者教學)

發佈 2023年10月30日
分享:

歡迎來到本教程,我們將探討如何在 C# 控制台應用程序中使用 Microsoft Interop 打印 Word 文件。本入門指南將引導您逐步實現以程式化方式列印 Microsoft Word 文件。

先決條件

在進入程式碼之前,必須先設定幾個重要事項:

Microsoft Word 安裝:確保你的系統已安裝 Microsoft Word。如果未安裝,請前往電腦的官方 Microsoft 網站或應用程式商店進行安裝。

Visual Studio 設定:你應該已安裝可建立主控台應用程式的 Visual Studio。如果你是初學者,考慮下載 Visual Studio 社群版, 這是免費的,並且滿足我們的需求。

一份Word文檔:在您的機器上準備一個示範Word文檔用於測試,這將是我們要發送到打印機的文檔。

設定環境

建立一個新的控制台應用程式

  1. 打開 Visual Studio。

  2. 點擊建立新專案。

  3. 搜索「Console App」,並選擇適當的 C# 範本。

  4. 為您的專案命名 (InteropPrintConsoleTutorial) 並選擇合適的位置。

新增 Interop 引用

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

  1. 在 Visual Studio 中,右鍵點擊 Solution Explorer 中的 Console 專案。
  2. 瀏覽到 Add > Reference。
  3. 在 Reference Manager 視窗中,轉到 COM 標籤。
  4. 在搜索欄輸入 "Microsoft Word" 以過濾列表。
  5. 從結果中選擇 Microsoft Word xx.x Object Library。 (其中 xx.x 代表版本號碼).

  6. 點擊確定按鈕以添加參考資料。

您也可以使用NuGet套件管理器進行安裝。

您也可以使用 NuGet 套件管理器安裝 `Microsoft.Office.Interop.Word` 庫。

驗證應用程式設定

確保您的應用程式目標框架與Interop庫相容。您可以在Solution Explorer中右鍵點擊您的專案,選擇屬性,然後在應用程式標籤下查看目標框架。如果遇到Interop庫版本的問題,請考慮下載必要的包或程序集,或者調整目標框架版本。

在設定好環境後,您現在可以進行程式編碼過程。

了解文件物件

文件物件是在處理 Word 文件時 Interop 服務的核心。這個物件代表一個 Microsoft Word 文件並提供其所有功能。

一個常見的任務是打開文件:

object oMissing = Type.Missing;
object fileName = @"C:\path_to_document\document.docx";
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);
object oMissing = Type.Missing;
object fileName = @"C:\path_to_document\document.docx";
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);
Dim oMissing As Object = Type.Missing
Dim fileName As Object = "C:\path_to_document\document.docx"
Dim wordDoc As Word._Document = wordApp.Documents.Open(fileName, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing)
VB   C#

用 ref oMissing 传递的多个参数看起來可能令人卻步,但這對於 Open 方法來說是必需的,因為該方法需要多個參數,其中大多數是可選的。

實現列印功能

在設定好環境並瞭解文件物件後,是時候深入瞭解列印Word文件的核心功能了。

基本列印 Word 文件

要列印文件,您可以使用以下方法:

private void ButtonPrint_Click(object sender, EventArgs e)
{
    wordDoc.PrintOut();
}
private void ButtonPrint_Click(object sender, EventArgs e)
{
    wordDoc.PrintOut();
}
Private Sub ButtonPrint_Click(ByVal sender As Object, ByVal e As EventArgs)
	wordDoc.PrintOut()
End Sub
VB   C#

此方法使用預設設定將文件傳送至預設印表機。

3.2 為 Word 文件打印設置自定義選項

如果要引入打印對話框、自定義打印機設置,甚至打印多頁,您需要採取更詳細的方法:

private void ButtonPrintWithSettings_Click(object sender, EventArgs e)
{
    object copies = "1";
    object pages = "1-3"; // To print multiple pages, e.g., 1 to 3.

    wordDoc.PrintOut(Copies: ref copies, Pages: ref pages);
}
private void ButtonPrintWithSettings_Click(object sender, EventArgs e)
{
    object copies = "1";
    object pages = "1-3"; // To print multiple pages, e.g., 1 to 3.

    wordDoc.PrintOut(Copies: ref copies, Pages: ref pages);
}
Private Sub ButtonPrintWithSettings_Click(ByVal sender As Object, ByVal e As EventArgs)
	Dim copies As Object = "1"
	Dim pages As Object = "1-3" ' To print multiple pages, e.g., 1 to 3.

	wordDoc.PrintOut(Copies:= copies, Pages:= pages)
End Sub
VB   C#

在上述源代碼中,我們指定了頁面範圍和副本數量,但潛在的自定義選項是巨大的。

自訂列印設定

調整列印設定的能力是程式控制的特點所在。無論您想調整印表機設定,指定特定的印表機,甚至是靜默列印文件,這一切都可以通過Interop實現。

靜默列印

靜默列印就是在無需用戶互動的情況下,將文件發送到打印機:

object background = false;
wordDoc.PrintOut(Background: ref background);
object background = false;
wordDoc.PrintOut(Background: ref background);
Dim background As Object = False
wordDoc.PrintOut(Background:= background)
VB   C#

指定打印機

要在非預設的特定打印機上打印文件:

wordApp.ActivePrinter = "Printer Name";
wordDoc.PrintOut();
wordApp.ActivePrinter = "Printer Name";
wordDoc.PrintOut();
wordApp.ActivePrinter = "Printer Name"
wordDoc.PrintOut()
VB   C#

進階列印機設定

除了指定列印機外,有時還需要調整列印機設定:

PrintDialog printDialog = new PrintDialog();
if (printDialog.ShowDialog() == DialogResult.OK)
{
    wordApp.ActivePrinter = printDialog.PrinterSettings.PrinterName;
    wordDoc.PrintOut();
}
PrintDialog printDialog = new PrintDialog();
if (printDialog.ShowDialog() == DialogResult.OK)
{
    wordApp.ActivePrinter = printDialog.PrinterSettings.PrinterName;
    wordDoc.PrintOut();
}
Dim printDialog As New PrintDialog()
If printDialog.ShowDialog() = DialogResult.OK Then
	wordApp.ActivePrinter = printDialog.PrinterSettings.PrinterName
	wordDoc.PrintOut()
End If
VB   C#

這樣,用戶可以手動調整設定,如頁面方向、雙面列印等。

引薦 IronWord

雖然 Microsoft Interop 提供了管理 Word 文件的功能,但對於嚴肅的商業用途來說,它的健全性和效率並不如預期。引進 IronWord—a superior alternative to Interop for Word DOCX file processing. IronWord 允許在 C# 中無縫讀取、寫入和操作 Word 文件。了解更多เกี่ยวกับ 如何開始使用IronWord.

IronXL for .NET:C# Excel 庫

結論

在本教程中,我們深入探討了如何在 C# 控制台應用程式中使用 Microsoft Interop 程式化地列印 Word 文件。我們已經了解了如何顯示列印對話框、設置自訂列印設定以及控制多種列印方面,例如選擇指定的印表機或設定頁面範圍。雖然 Interop 提供了基本功能,但值得注意的是,有像 IronWord.

< 上一頁
C# 打開Word文件

準備開始了嗎? 版本: 2024.10 剛剛發布

免費 NuGet 下載 總下載次數: 5,614 查看許可證 >