如何在 C# 中使用網路打印機打印
在各種軟體應用程式中,透過網路印表機進行程式化列印是一項常見任務。 無論您是開發桌面應用程式、行動應用程式或 Web 服務,將文件直接傳送到網路印表機的功能都可以大大提高軟體的可用性和效率。 在本文中,我們將探討如何使用 C# 和Iron Software的IronPrint在網路印表機上進行列印。
How to Print with Network Printer in C#
- 使用IronPrint建立一個用於列印文件的新項目。
- 將IronPrint安裝到新專案中。
- 列出可用於列印文件的網路印表機。
- 使用IronPrint列印 PDF 文件。
- 使用IronPrint和對話方塊列印 PDF 文件。
- 進階列印設定。
營造環境
在深入編寫程式碼之前,讓我們先確保環境設定正確。 若要在 C# 中使用網路印表機列印,您需要具備以下條件:
- 確保從執行 C# 應用程式的電腦可以存取網路印表機。
- 印表機的網路位址(IP 位址或網路印表機名稱)。
- 機器上已安裝必要的印表機驅動程式。
- 如有需要,擁有安裝印表機或驅動程式的管理權限。
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
系統.列印
C# 中的 System.Printing 命名空間包含類別和接口,使開發人員能夠以程式設計方式與印表機、列印佇列、列印伺服器和列印作業進行互動。 此命名空間中的一些關鍵類別和介麵包括:
PrintQueue: 表示連接到系統的印表機或列印設備。PrintServer: 表示網路上的印表機伺服器。PrintSystemJobInfo: 提供有關列印作業的信息,例如其狀態和屬性。PrintTicket: 表示列印作業的設置,包括紙張尺寸、方向和列印品質。
使用 System.Printing 開始:在深入程式碼之前,讓我們確保對 System.Printing 的列印工作原理有一個基本的了解:
1.選擇:您需要識別代表您要用於列印的印表機的 @@--CODE-109195 --@@ 物件。
2.配置:您可以選擇配置 @@--CODE-109197 --@@ 對象,以指定列印設置,例如紙張尺寸、方向和份數。
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
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
程式碼解釋
- 使用
Printer.GetPrinterNames取得所有可用的印表機驅動程式。 - 使用
Console.WriteLine列印名稱。
輸出
步驟 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
執行完畢後,文件將被加入到列印佇列中,如輸出所示。
輸出
步驟 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
程式碼解釋
- 使用 IronPrint 中的
ShowPrintDialog,我們可以透過對話方塊列印。 - 對話方塊開啟後,選擇要列印文件的印表機。
輸出
步驟 6:進階列印設定
IronPrint 支援使用進階設定列印文件。 它支援以下屬性:
PaperSize:指定印表機使用的紙張尺寸。PaperOrientation:定義紙張方向,例如自動、縱向或橫向。DPI:設定所需的列印解析度(以每英吋點數為單位)。 預設值為 300,常用於商業印刷。 實際DPI可能受限於印表機的效能。NumberOfCopies:指示要列印的文件的相同副本數量。PrinterName:指定用於列印任務的印表機的名稱。PaperMargins:確定列印邊距,以毫米為單位。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
程式碼解釋
- 程式碼首先匯入必要的命名空間 IronPrint,其中包含列印所需的類別和方法。
- 在
IronPrintDemo命名空間中,有一個名為 Program 的類,宣告為公用類別。 - Main 方法作為程序的入口點。
- 在主方法中:
- 實例化一個名為
printSettings的PrintSettings對象,用於配置自訂列印設定。 printSettings的Dpi屬性設定為 150,表示列印解析度為每英吋 150 點。printSettings的NumberOfCopies屬性設為 2,指定將列印該文件的兩個相同副本。printSettings的PaperOrientation.Portrait屬性設定為PaperOrientation.Portrait,表示該文件將以縱向列印。
- 實例化一個名為
- 最後,使用參數"sample.pdf"(要列印的文件名稱)和"
printSettings"(自訂列印設定)呼叫Printer.Print方法。 此方法使用指定的設定來處理列印過程。
輸出
執照
Iron Software的IronPrint是一個企業級庫,需要許可證才能運作。 新增 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 套件管理器安裝 IronPrint,或在套件管理器控制台中運行命令 Install-Package 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 支援包括紙張大小、紙張方向、DPI、輸出複製數、印表機名稱、紙張邊距和灰階在內的高級設定。
IronPrint 提供哪些許可選項?
IronPrint 許可可以購買,也可以在 Iron Software 網站上註冊免費 30 天試用。許可金鑰放置在應用程式的 appSettings.json 文件中。
用於通過命令行安裝 IronPrint 的命令是什麼?
要通過命令行安裝 IronPrint,請使用命令 Install-Package IronPrint。


