使用 IRONPRINT

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

查克尼思·賓
查克尼思·賓
2024年6月6日
分享:

介紹

在各種軟體應用中,以程式設計方式在網路印表機上進行列印是一項常見的任務。 無論您是在構建桌面、移動應用程式還是網路服務,直接將文件發送到網路打印機的能力都可以大大提高您的軟體的可用性和效率。 在本文中,我們將探討如何使用 C# 和 IronPrintIron Software 在網路印表機上列印。

如何在 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;
// 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)
$vbLabelText   $csharpLabel

System.Printing

C# 中的 System.Printing 命名空間包含一系列類別和介面,讓開發者能夠以程式方式與印表機、列印佇列、列印伺服器及列印作業進行互動。 此命名空間中的一些關鍵類別和介面包括:

  1. PrintQueue:表示連接到系統的打印機或打印設備。

  2. PrintServer:表示網路上的列印伺服器。

  3. PrintSystemJobInfo:提供有關列印工作的資訊,例如其狀態和屬性。

  4. PrintTicket:表示列印作業的設定,包括紙張尺寸、方向和列印品質。

    快速入門System.Printing:在我們深入瞭解代碼之前,先確保我們對使用System.Printing列印的運作機制有基本的理解:

  5. PrintQueue 選擇:您需要識別代表您想要用於列印的印表機的 PrintQueue 對象。

  6. PrintTicket 配置:您可以選擇性地配置 PrintTicket 物件,以指定列印設定,例如紙張大小、方向和副本數量。

  7. 文件列印:最後,您將文件發送到選定的 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
$vbLabelText   $csharpLabel

IronPrint Vs System.Printing

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

此表格提供了 IronPrint 和 System.Printing 特性和特徵的快速概覽,幫助開發人員根據他們的特定需求和平台目標做出明智的決策。

步驟 1:使用 IronPrint 創建一個新的打印文件專案

在 Visual Studio 中創建如下所示的控制台應用程式。

如何使用網路印表機進行列印:圖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
$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. 使用ShowPrintDialog從IronPrint中,我們可以通過對話框列印

  2. 儘量開啟對話框並選擇所需的印表機來列印文件。

輸出

如何使用網路印表機進行列印:圖8 - 選擇正確的印表機

步驟 6:使用列印設定進階列印

IronPrint 支援具有進階設定的文件列印。 它支持以下屬性:

  1. PaperSize:指定印表機使用的紙張尺寸。

  2. PaperOrientation:定義紙張的方向,例如自動、直式或橫式。

  3. DPI:設定所需的每英吋點數列印解析度。 預設值為 300,通常用於商業印刷。 實際的 DPI 可能受限於列印機的能力。

  4. NumberOfCopies: 表示要列印文件的相同副本數量。

  5. PrinterName:指定用於打印任務的打印機名稱。

  6. PaperMargins:確定列印的邊距,單位為毫米。

  7. Grayscale:布林值,用於確定是否以灰階列印。 預設為假。

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

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
$vbLabelText   $csharpLabel

程式碼說明

  1. 程式碼開始於匯入必要的命名空間 IronPrint,該命名空間大概包含了列印所需的類別和方法。

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

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

  4. 在 Main 方法內:

    • 一個名為printSettingsPrintSettings對象被實例化,以配置自訂列印設定。

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

    • printSettingsNumberOfCopies 屬性設置為 2,這表示將列印兩份相同的文件。

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

輸出

如何使用網路印表機列印 C#:圖 9 - 顯示已加入列印列隊中的 PDF 文件的範例輸出

許可證

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 的可能性,解鎖應用程式中高效文件列印的世界。

查克尼思·賓
軟體工程師
Chaknith 致力於 IronXL 和 IronBarcode。他在 C# 和 .NET 方面擁有豐富的專業知識,協助改進軟體並支持客戶。他從用戶互動中獲得的洞察力有助於提高產品、文檔和整體體驗。
下一個 >
如何在C#中列印QR Code

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

查看許可證 >