使用 IRONPRINT

如何使用 C# 打印網路打印機

發佈 2024年6月6日
分享:

介紹

在各種軟體應用程式中,透過程序化方式在網路印表機上列印是一項常見的任務。無論您是在建立桌面應用程式、行動應用程式還是網路服務,能夠直接將文件傳送到網路印表機都能大大提升您的軟體的可用性和效率。在本文中,我們將探討如何使用C#來在網路印表機上列印。 IronPrintIron Software.

如何在 C# 中使用網路列印機進行列印文件

  1. 創建一個新的列印文件專案,使用 IronPrint.

  2. 安裝 IronPrint 到一個新項目。

  3. 可用於列印文件的網路打印機列表。

  4. 使用列印 PDF 文件。 IronPrint.

  5. 使用 IronPrint 列印 PDF 文件 IronPrint 使用對話框。

  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;
// 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;
// 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
' Configure printer setting
Private printSettings As New PrintSettings()
printSettings.Dpi = 220
printSettings.NumberOfCopies = 10
printSettings.PaperOrientation = PaperOrientation.Portrait
' Print the document
Printer.Print("awesomeIronPrint.pdf", printSettings)
VB   C#

System.Printing

在 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, paper size, orientation
            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, paper size, orientation
            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, paper size, orientation
			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
VB   C#

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 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 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 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
VB   C#

代碼說明

  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
VB   C#

執行完成後,文件會如輸出所示被添加到打印隊列中。

輸出

如何使用 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
VB   C#

代碼說明

  1. 使用 IronPrint 中的 ShowPrintDialog 可以顯示列印對話框
  2. 對話框打開後,選擇所需的打印機進行文件列印

輸出

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

第六步:使用打印設定進行高級列印

使用 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 setting
        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 setting
        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 setting
			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
VB   C#

代碼說明

  1. 代碼首先匯入必要的命名空間 IronPrint,這個命名空間應包含所需的列印類別和方法。

  2. IronPrintDemo 命名空間內,有一個命名為 Program 的類別被宣告為公開的。

  3. Main 方法作為程式的進入點。

  4. 在 Main 方法內:

    • 創建一個命名為 printSettingsPrintSettings 對象,用於配置自訂列印設置。

    • printSettings 的 Dpi 屬性設定為 150,表示列印解析度為 150 點每英吋。

    • printSettingsNumberOfCopies 屬性設定為 2,指定列印文件的兩份相同副本。

    • printSettingsPaperOrientation 屬性設定為 PaperOrientation.Portrait,表示文件將以直向打印。
  5. 最後,使用參數 "sample.pdf" 調用 Printer 的 Print 方法。 (列印文件的名稱) 和 printSettings (自訂列印設定)此方法大概是使用指定的設定來處理列印過程。

輸出

如何使用 C# 進行網絡打印:圖 9 - 示範輸出顯示已添加到打印隊列的 PDF 文件以進行打印

許可証

IronPrintIron Software 是一個企業庫,需要許可證才能運行。添加 IronPrint 許可證密鑰可以使項目不受限制或水印地運行。 在此購買授權 或註冊免費的30天試用金鑰 這裡一旦獲得授權金鑰,該金鑰需要放置在應用程式的 appSettings.json 檔案中。

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

這將無限制地打印文件,並且不會有浮水印。

結論

總結來說,IronPrint 和 System.Printing 各有其優勢,適用於不同的情境。IronPrint 提供了一個精簡且跨平台的解決方案,重點在於簡單和多功能性,而 System.Printing 則提供了原生整合和細緻的控制,特別是針對基於 Windows 的應用程式。開發人員在選擇這些列印庫來開發他們的 C# 專案時,應考慮其特定的需求和平台目標。

IronPrint 透過提供一個易於使用的 API 和廣泛的相容性,來簡化 .NET 應用程式中的文件列印。按照本文中的步驟,您可以無縫整合列印功能到您的 .NET 專案中,提升其可用性和生產力。探索 IronPrint 的可能性,為您的應用程式解鎖高效文件列印的世界。

下一個 >
如何在C#中列印QR Code

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

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