跳過到頁腳內容
使用 IRONPRINT

如何在 C# 中使用網絡打印機打印

在各種軟體應用程式中,透過網路印表機進行程式化列印是一項常見任務。 無論您是開發桌面應用程式、行動應用程式或 Web 服務,將文件直接傳送到網路印表機的功能都可以大大提高軟體的可用性和效率。 在本文中,我們將探討如何使用 C# 和Iron SoftwareIronPrint在網路印表機上進行列印。

如何在 C# 中使用網路印表機列印

  1. 使用IronPrint建立一個用於列印文件的新項目。
  2. IronPrint安裝到新專案中。
  3. 列出可用於列印文件的網路印表機。
  4. 使用IronPrint列印 PDF 文件。
  5. 使用IronPrint和對話方塊列印 PDF 文件。
  6. 進階列印設定。

營造環境

在深入編寫程式碼之前,讓我們先確保環境設定正確。 若要在 C# 中使用網路印表機列印,您需要具備以下條件:

  1. 確保從執行 C# 應用程式的電腦可以存取網路印表機。
  2. 印表機的網路位址(IP 位址或網路印表機名稱)。
  3. 機器上已安裝必要的印表機驅動程式。
  4. 如有需要,擁有安裝印表機或驅動程式的管理權限。

IronPrint

在深入探討實作細節之前,讓我們先熟悉一下如何開始使用 IronPrint:

1.安裝:IronPrint 可以透過 NuGet 套件管理器輕鬆安裝。 只需訪問NuGet 頁面並按照安裝說明操作即可。 2.文件:請參閱官方文件以取得快速入門指南和全面的 API 參考。

相容性和支援:IronPrint 提供廣泛的相容性和支持,涵蓋各種環境和專案類型:

  • .NET 版本支援:IronPrint 支援 C#、VB.NET 和 F#,包括 .NET 8、7、6、5 和 Core 3.1+。 *作業系統與環境*:IronPrint 與 Windows (7+)、macOS (10+)、iOS (11+) 和 Android API 21+ (v5"Lollipop") 相容。 專案類型**:無論您是開發行動應用程式(Xamarin、MAUI、Avalonia)、桌面應用程式(WPF、MAUI、Windows Avalonia)還是控制台應用程序,IronPrint 都能滿足您的需求。

現在讓我們透過以下範例了解如何使用 IronPrint 進行列印。

using IronPrint;

namespace IronPrintDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            // Configure printer setting
            PrintSettings printSettings = new PrintSettings();
            printSettings.Dpi = 220;
            printSettings.NumberOfCopies = 10;
            printSettings.PaperOrientation = PaperOrientation.Portrait;

            // Print the document
            Printer.Print("awesomeIronPrint.pdf", printSettings);
        }
    }
}
using IronPrint;

namespace IronPrintDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            // Configure printer setting
            PrintSettings printSettings = new PrintSettings();
            printSettings.Dpi = 220;
            printSettings.NumberOfCopies = 10;
            printSettings.PaperOrientation = PaperOrientation.Portrait;

            // Print the document
            Printer.Print("awesomeIronPrint.pdf", printSettings);
        }
    }
}
Imports IronPrint

Namespace IronPrintDemo
	Friend Class Program
		Shared Sub Main(ByVal args() As String)
			' Configure printer setting
			Dim printSettings As New PrintSettings()
			printSettings.Dpi = 220
			printSettings.NumberOfCopies = 10
			printSettings.PaperOrientation = PaperOrientation.Portrait

			' Print the document
			Printer.Print("awesomeIronPrint.pdf", printSettings)
		End Sub
	End Class
End Namespace
$vbLabelText   $csharpLabel

系統.列印

C# 中的System.Printing命名空間包含類別和接口,使開發人員能夠以程式設計方式與印表機、列印佇列、列印伺服器和列印作業進行互動。 此命名空間中的一些關鍵類別和介麵包括:

  1. PrintQueue :表示連接到系統的印表機或列印設備。
  2. PrintServer :表示網路上的列印伺服器。
  3. PrintSystemJobInfo :提供有關列印作業的信息,例如其狀態和屬性。
  4. PrintTicket :表示列印作業的設置,包括紙張尺寸、方向和列印品質。

System.Printing入門:在深入程式碼之前,讓我們先確保對System.Printing列印工作原理有基本的了解:

  1. PrintQueue選擇:您需要確定代表您要用於列印的印表機的PrintQueue佇列物件。
  2. PrintTicket配置:您可以選擇配置PrintTicket對象,以指定列印設置,例如紙張尺寸、方向和份數。 3.文檔列印:最後,將要列印的文檔傳送到選定的PrintQueue

使用System.Printing實現文件列印:現在,讓我們透過以下範例來了解使用System.Printing列印文件的過程:

以下是原始碼:

using System;
using System.IO;
using System.Printing;

class Program
{
    static void Main(string[] args)
    {
        // Specify the path to the document to be printed on the local printer
        string documentPath = "path/to/your/document.pdf";

        // Instantiate a Printer Server object representing the local print server
        using (PrintServer printServer = new PrintServer())
        {
            // Get the default PrintQueue from the PrintServer default 
            PrintQueue defaultPrintQueue = printServer.DefaultPrintQueue;

            // Create a PrintTicket object to specify print settings (optional)
            PrintTicket printTicket = new PrintTicket();

            // Configure print settings, e.g., number of copies
            printTicket.CopyCount = 1;

            // Print the document to the default PrintQueue with the specified PrintTicket
            defaultPrintQueue.AddJob("MyPrintJob", documentPath, false, printTicket);
        }

        Console.WriteLine("Document sent to print queue.");
    }
}
using System;
using System.IO;
using System.Printing;

class Program
{
    static void Main(string[] args)
    {
        // Specify the path to the document to be printed on the local printer
        string documentPath = "path/to/your/document.pdf";

        // Instantiate a Printer Server object representing the local print server
        using (PrintServer printServer = new PrintServer())
        {
            // Get the default PrintQueue from the PrintServer default 
            PrintQueue defaultPrintQueue = printServer.DefaultPrintQueue;

            // Create a PrintTicket object to specify print settings (optional)
            PrintTicket printTicket = new PrintTicket();

            // Configure print settings, e.g., number of copies
            printTicket.CopyCount = 1;

            // Print the document to the default PrintQueue with the specified PrintTicket
            defaultPrintQueue.AddJob("MyPrintJob", documentPath, false, printTicket);
        }

        Console.WriteLine("Document sent to print queue.");
    }
}
Imports System
Imports System.IO
Imports System.Printing

Friend Class Program
	Shared Sub Main(ByVal args() As String)
		' Specify the path to the document to be printed on the local printer
		Dim documentPath As String = "path/to/your/document.pdf"

		' Instantiate a Printer Server object representing the local print server
		Using printServer As New PrintServer()
			' Get the default PrintQueue from the PrintServer default 
			Dim defaultPrintQueue As PrintQueue = printServer.DefaultPrintQueue

			' Create a PrintTicket object to specify print settings (optional)
			Dim printTicket As New PrintTicket()

			' Configure print settings, e.g., number of copies
			printTicket.CopyCount = 1

			' Print the document to the default PrintQueue with the specified PrintTicket
			defaultPrintQueue.AddJob("MyPrintJob", documentPath, False, printTicket)
		End Using

		Console.WriteLine("Document sent to print queue.")
	End Sub
End Class
$vbLabelText   $csharpLabel

IronPrint 與System.Printing對比

如何在 C# 中使用網路印表機列印:圖 1 - IronPrint 與 System.Printing 的快速比較表

此表簡要概述了 IronPrint 和System.Printing的功能和特性,協助開發人員根據其具體需求和平台目標做出明智的決策。

步驟 1:使用 IronPrint 建立一個用於列印文件的新項目

在 Visual Studio 中建立一個控制台應用程序,如下所示。

如何在 C# 中使用網路印表機列印:圖 2 - 建立控制台應用程式

提供專案名稱和地點。

如何在 C# 中使用網路印表機列印:圖 3 - 提供項目名稱

選擇 .NET 版本。

如何在 C# 中使用網路印表機列印:圖 4 - 選擇適當的 .NET 版本

現在專案已經建立完成,可以進行進一步的編碼了。

步驟 2:在新項目上安裝 IronPrint

按照以下所示,從 Visual Studio 套件管理器安裝 IronPrint。

如何在 C# 中使用網路印表機列印:圖 5 - 透過 Visual Studio 套件管理器搜尋 IronPrint

IronPrint 也可以使用以下指令安裝:

Install-Package IronPrint

步驟 3:列出可用於列印文件的網路印表機

若要使用 IronPrint 列出印表機名稱,請使用下列程式碼:

using System;
using System.Collections.Generic;
using IronPrint;

namespace IronPrintDemo
{
    public class Program
    {
        public static void Main()
        {
            // Get printer names using printer drivers
            List<string> printersName = Printer.GetPrinterNames();
            foreach (var printer in printersName)
            {
                Console.WriteLine(printer);
            }
        }
    }
}
using System;
using System.Collections.Generic;
using IronPrint;

namespace IronPrintDemo
{
    public class Program
    {
        public static void Main()
        {
            // Get printer names using printer drivers
            List<string> printersName = Printer.GetPrinterNames();
            foreach (var printer in printersName)
            {
                Console.WriteLine(printer);
            }
        }
    }
}
Imports System
Imports System.Collections.Generic
Imports IronPrint

Namespace IronPrintDemo
	Public Class Program
		Public Shared Sub Main()
			' Get printer names using printer drivers
			Dim printersName As List(Of String) = Printer.GetPrinterNames()
			For Each printer In printersName
				Console.WriteLine(printer)
			Next printer
		End Sub
	End Class
End Namespace
$vbLabelText   $csharpLabel

程式碼解釋

  1. 使用Printer.GetPrinterNames取得所有可用的印表機驅動程式。
  2. 使用Console.WriteLine列印名稱。

輸出

如何在 C# 中使用網路印表機列印:圖 6 - 可用印表機的範例輸出

步驟 4:使用 IronPrint 列印 PDF 文件

使用以下程式碼可以靜默列印文件:

using IronPrint;

namespace IronPrintDemo
{
    public class Program
    {
        public static void Main()
        {
            // Print the document silently
            Printer.Print("sample.pdf");
        }
    }
}
using IronPrint;

namespace IronPrintDemo
{
    public class Program
    {
        public static void Main()
        {
            // Print the document silently
            Printer.Print("sample.pdf");
        }
    }
}
Imports IronPrint

Namespace IronPrintDemo
	Public Class Program
		Public Shared Sub Main()
			' Print the document silently
			Printer.Print("sample.pdf")
		End Sub
	End Class
End Namespace
$vbLabelText   $csharpLabel

執行完畢後,文件將被加入到列印佇列中,如輸出所示。

輸出

如何在 C# 中使用網路印表機列印:圖 7 - 範例輸出,展示了文件如何新增至列印佇列

步驟 5:使用 IronPrint 對話方塊列印 PDF 文件

使用以下程式碼列印帶有對話框的文檔:

using IronPrint;

namespace IronPrintDemo
{
    public class Program
    {
        public static void Main()
        {
            // Print with Dialog
            Printer.ShowPrintDialog("sample.pdf");
        }
    }
}
using IronPrint;

namespace IronPrintDemo
{
    public class Program
    {
        public static void Main()
        {
            // Print with Dialog
            Printer.ShowPrintDialog("sample.pdf");
        }
    }
}
Imports IronPrint

Namespace IronPrintDemo
	Public Class Program
		Public Shared Sub Main()
			' Print with Dialog
			Printer.ShowPrintDialog("sample.pdf")
		End Sub
	End Class
End Namespace
$vbLabelText   $csharpLabel

程式碼解釋

  1. 使用 IronPrint 中的ShowPrintDialog ,我們可以用對話框列印。
  2. 對話方塊開啟後,選擇要列印文件的印表機。

輸出

如何在 C# 中使用網路印表機列印:圖 8 - 選擇正確的印表機

步驟 6:進階列印設定

IronPrint 支援使用進階設定列印文件。 它支援以下屬性:

  1. PaperSize :指定印表機使用的紙張尺寸。
  2. PaperOrientation :定義紙張的方向,例如自動、縱向或橫向。
  3. DPI :設定所需的列印分辨率,單位為每英吋點數。 預設值為 300,常用於商業印刷。 實際DPI可能受限於印表機的效能。
  4. NumberOfCopies :表示要列印的文件的相同副本數量。
  5. PrinterName :指定用於列印任務的印表機的名稱。
  6. PaperMargins :確定列印邊距,以毫米為單位。
  7. Grayscale :一個布林值,用於確定是否以灰度列印。 預設值為 false。

現在我們來看一個程式碼範例:

using IronPrint;

namespace IronPrintDemo
{
    public class Program
    {
        public static void Main()
        {
            // Configure custom print settings
            PrintSettings printSettings = new PrintSettings();
            printSettings.Dpi = 150;
            printSettings.NumberOfCopies = 2;
            printSettings.PaperOrientation = PaperOrientation.Portrait;

            // Print the required document
            Printer.Print("sample.pdf", printSettings);
        }
    }
}
using IronPrint;

namespace IronPrintDemo
{
    public class Program
    {
        public static void Main()
        {
            // Configure custom print settings
            PrintSettings printSettings = new PrintSettings();
            printSettings.Dpi = 150;
            printSettings.NumberOfCopies = 2;
            printSettings.PaperOrientation = PaperOrientation.Portrait;

            // Print the required document
            Printer.Print("sample.pdf", printSettings);
        }
    }
}
Imports IronPrint

Namespace IronPrintDemo
	Public Class Program
		Public Shared Sub Main()
			' Configure custom print settings
			Dim printSettings As New PrintSettings()
			printSettings.Dpi = 150
			printSettings.NumberOfCopies = 2
			printSettings.PaperOrientation = PaperOrientation.Portrait

			' Print the required document
			Printer.Print("sample.pdf", printSettings)
		End Sub
	End Class
End Namespace
$vbLabelText   $csharpLabel

程式碼解釋

  1. 程式碼首先匯入必要的命名空間 IronPrint,其中包含列印所需的類別和方法。
  2. IronPrintDemo命名空間中,有一個名為 Program 的類,聲明為公用類別。
  3. Main 方法作為程序的入口點。
  4. 在主方法中:
    • 實例化一個名為printSettingsPrintSettings對象,用於配置自訂列印設定。
    • printSettingsDpi屬性設定為 150,表示列印解析度為每吋 150 點。 printSettingsNumberOfCopies屬性設定為 2,指定要列印文件的兩個相同副本。
    • printSettingsPaperOrientation屬性設定為PaperOrientation.Portrait ,表示文件將以縱向列印。
  5. 最後,呼叫Printer.Print方法,參數為"sample.pdf"(要列印的文件名稱)和printSettings (自訂列印設定)。 此方法使用指定的設定來處理列印過程。

輸出

如何在 C# 中使用網路印表機列印:圖 9 - 範例輸出,展示了新增至列印佇列中的 PDF 文檔

執照

Iron SoftwareIronPrint是一個企業級庫,需要許可證才能運作。 新增 IronPrint 許可證金鑰後,項目即可無限製或無浮水印地上線運作。 您可以在這裡購買許可證,或在這裡註冊以取得30天免費試用金鑰。

取得許可證金鑰後,需要將該金鑰放入應用程式的 appSettings.json 檔案中。

{
  "IronPrint.License.LicenseKey": "IRONPRINT.KEY"
}

這將列印出無限制、無浮水印的文件。

結論

總而言之,IronPrint 和System.Printing各有優勢,適用於不同的場景。 IronPrint 提供精簡且跨平台的解決方案,注重簡潔性和多功能性,而System.Printing提供原生整合和精細控制,尤其適用於基於 Windows 的應用程式。 開發人員在為 C# 專案選擇列印庫時,應考慮其具體需求和平台目標。

IronPrint 透過提供使用者友善的 API 和廣泛的跨平台及專案類型相容性,簡化了 .NET 應用程式中的文件列印。按照本文概述的步驟,您可以將列印功能無縫整合到 .NET 專案中,從而提高專案的可用性和效率。 探索 IronPrint 的各種可能性,在您的應用程式中開啟高效能文件列印的新世界。

常見問題解答

如何使用 C# 在網路印表機上列印?

您可以使用 IronPrint 以 C# 在網路印表機上列印文件。它允許您使用程式化處理列印任務的方法,直接將文件傳送至網路印表機。

在 C# 中設定網路印表機列印的步驟為何?

首先,確保您可以存取網路印表機及其位址。透過 NuGet 套件管理員安裝必要的驅動程式和 IronPrint。然後,使用 IronPrint 的方法來管理和傳送列印工作。

如何在我的 C# 專案中安裝 IronPrint?

您可以使用 Visual Studio 中的 NuGet Package Manager 或在 Package Manager Console 中執行 Install-Package IronPrint 指令來安裝 IronPrint。

IronPrint 兼容哪些 .NET 版本?

IronPrint 支援 C#、VB.NET 和 F#,並與 .NET 8、7、6、5 和 Core 3.1+ 相容。

IronPrint 可以在不同的作業系統上使用嗎?

是的,IronPrint 與 Windows (7+)、macOS (10+)、iOS (11+) 和 Android API 21+ (v5 'Lollipop')相容。

IronPrint 與 C# 中的 System.Printing 相比如何?

IronPrint 提供注重簡便性的跨平台解決方案,而 System.Printing 則為基於 Windows 的應用程式提供本機整合與控制。

如何使用 IronPrint 列出可用的網路印表機?

您可以使用 IronPrint 中的方法 Printer.GetPrinterNames() 返回可用的印表機驅動程式清單。

IronPrint 有哪些進階列印選項?

IronPrint 支援進階設定,包括 PaperSize、PaperOrientation、DPI、NumberOfCopies、PrinterName、PaperMargins 和 Grayscale。

IronPrint 有哪些授權選項?

您可以購買 IronPrint 授權,也可以在 Iron Software 網站註冊免費試用 30 天。許可證金鑰會放在您應用程式的 appSettings.json 檔案中。

通過命令列安裝 IronPrint 使用什麼命令?

若要透過命令列安裝 IronPrint,請使用 Install-Package IronPrint 指令。

Curtis Chau
技術作家

Curtis Chau 擁有卡爾頓大學計算機科學學士學位,專注於前端開發,擅長於 Node.js、TypeScript、JavaScript 和 React。Curtis 熱衷於創建直觀且美觀的用戶界面,喜歡使用現代框架並打造結構良好、視覺吸引人的手冊。

除了開發之外,Curtis 對物聯網 (IoT) 有著濃厚的興趣,探索將硬體和軟體結合的創新方式。在閒暇時間,他喜愛遊戲並構建 Discord 機器人,結合科技與創意的樂趣。