如何在 C# 中使用網路列印機列印
在各種軟體應用程式中,透過程式印刷在網路印表機上是一項常見的任務。 無論您是在建立桌面、移動應用程式,還是網路服務,能夠直接將文件發送到網路印表機可以極大地提升您的軟體的易用性和效率。 在本文中,我們將探討如何使用C#和IronPrint從
How to Print with Network Printer in C
- 使用IronPrint建立一個新的專案以進行文件印刷。
- 將IronPrint安裝到新專案中。
- 列出可用來印刷文件的網路印表機。
- 使用IronPrint印刷PDF文件。
- 使用IronPrint的對話框印刷PDF文件。
- 使用印刷設置進行高級印刷。
設定環境
在進入編碼部分之前,讓我們確保我們的環境已正確設定。 在C#中要在網路印表機上印刷,您需要以下幾點:
- 確保一台網路印表機從運行您C#應用程式的機器上可存取。
- 印表機的網路地址(IP地址或網路印表機名稱)。
- 在機器上安裝必要的印表機驅動程式/驅動程式。
- 如果需要,安裝印表機或驅動程式的管理員權限。
IronPrint
在深入了解實現細節之前,讓我們熟悉如何開始使用IronPrint:
相容性和支持: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
System.Printing
C#中的System.Printing命名空間包含類和接口,使開發人員能夠透過程式與印表機、列印隊列、列印伺服器和列印作業進行交互。 此命名空間中的一些關鍵類和接口包括:
PrintQueue:代表系統連接的印表機或印刷裝置。PrintServer:代表網路上的一個列印伺服器。PrintSystemJobInfo:提供有關列印作業的資訊,如其狀態和屬性。PrintTicket:表示列印作業的設置,包括紙張大小、方向和列印質量。
開始使用System.Printing列印的基本了解:
PrintQueue物件。PrintTicket物件以指定列印設置,如紙張大小、方向和份數。- 文件印刷:最後,將要列印的文件發送到所選的
PrintQueue。
使用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

此表提供了IronPrint和System.Printing的功能和特性的一個快速概覽,幫助開發人員根據他們的特殊需求和平台目標做出明智的決定。
第一步:使用IronPrint建立一個新的文件印刷專案
在Visual Studio中建立以下所示的控制台應用程式。

提供專案的名稱和位置。

選擇 .NET 版本。

現在專案已建立並準備進行進一步的編碼。
第二步:在新項目上安裝 IronPrint
從Visual Studio包管理器安裝IronPrint,如下所示。

IronPrint也可以使用以下命令進行安裝:
Install-Package IronPrint
第三步:列出可用來印刷文件的網路印表機
要使用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列印名稱。
輸出

第四步:使用 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
執行後,文件將按輸出所示新增到列印隊列中。
輸出

第五步:使用 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
程式碼解釋
- 使用
ShowPrintDialog從IronPrint我們可以通過對話框印刷。 - 一旦對話框打開,選擇所需的印表機以列印文件。
輸出

第六步:使用列印設置進行高級列印
文件列印中的高級設置由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方法作為該程式的進入點。
- 在Main方法中:
- 實例化一個命名為
PrintSettings物件以配置自定義列印設置。 Dpi屬性設置為150,表示150每英寸點的列印解析度。NumberOfCopies屬性設置為2,指定將列印文件的兩個相同副本。PaperOrientation.Portrait,表示文件將以縱向列印。
- 實例化一個命名為
- 最後,調用
Printer.Print方法,參數為"sample.pdf"(要列印的文件名稱)和printSettings(自定義列印設置)。 此方法使用指定的設置處理列印過程。
輸出

授權
IronPrint來自Iron Software,是一個企業級程式庫,運行需要許可證。 新增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 支援高級設置,包括 PaperSize、PaperOrientation、DPI、NumberOfCopies、PrinterName、PaperMargins 和 Grayscale。
IronPrint 的授權有哪些選擇?
您可以購買 IronPrint 授權,或在 Iron Software 網站註冊免費 30 天試用。授權金鑰存放在您的應用程式的 appSettings.json 文件中。
通過命令行安裝 IronPrint 的命令是什麼?
要通過命令行安裝 IronPrint,使用命令 Install-Package IronPrint。


