在生產環境中測試,無水印。
在任何需要的地方都能運行。
獲得 30 天的全功能產品。
在幾分鐘內上手運行。
試用產品期間完全訪問我們的支援工程團隊
在各種軟體應用中,以程式設計方式在網路印表機上進行列印是一項常見的任務。 無論您是在構建桌面、移動應用程式還是網路服務,直接將文件發送到網路打印機的能力都可以大大提高您的軟體的可用性和效率。 在本文中,我們將探討如何使用 C# 和 IronPrint 從 Iron Software 在網路印表機上列印。
使用IronPrint創建一個新的文件列印專案。
將IronPrint 安裝到一個新專案。
可用於列印文件的網路印表機列表。
使用 IronPrint列印 PDF 文件。
使用具有對話框的IronPrint列印 PDF 文件。
在深入編碼之前,我們先確保環境已正確設置。 若要在 C# 中使用網路印表機打印,您需要具備以下條件:
確保網路印表機可從運行您的 C# 應用程式的機器上訪問。
印表機的網路位址(IP 位址或網路印表機名稱)。
必要的印表機驅動程式已安裝在機器上。
在深入了解實施細節之前,讓我們先熟悉如何開始使用IronPrint:
安裝:IronPrint 可以通過 NuGet 套件管理器輕鬆安裝。 只需造訪NuGet 頁面並按照安裝指示進行操作。
文件:請參閱官方文件以獲取快速入門指南和全面的 API 參考。
相容性和支援: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)
C# 中的 System.Printing
命名空間包含一系列類別和介面,讓開發者能夠以程式方式與印表機、列印佇列、列印伺服器及列印作業進行互動。 此命名空間中的一些關鍵類別和介面包括:
PrintQueue
:表示連接到系統的打印機或打印設備。
PrintServer
:表示網路上的列印伺服器。
PrintSystemJobInfo
:提供有關列印工作的資訊,例如其狀態和屬性。
PrintTicket
:表示列印作業的設定,包括紙張尺寸、方向和列印品質。
快速入門System.Printing
:在我們深入瞭解代碼之前,先確保我們對使用System.Printing
列印的運作機制有基本的理解:
PrintQueue
選擇:您需要識別代表您想要用於列印的印表機的 PrintQueue
對象。
PrintTicket
配置:您可以選擇性地配置 PrintTicket
物件,以指定列印設定,例如紙張大小、方向和副本數量。
文件列印:最後,您將文件發送到選定的 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
System.Printing
此表格提供了 IronPrint 和 System.Printing
特性和特徵的快速概覽,幫助開發人員根據他們的特定需求和平台目標做出明智的決策。
在 Visual Studio 中創建如下所示的控制台應用程式。
提供專案名稱和位置。
選擇 .NET 版本。
現在專案已建立並準備進一步編碼。
從 Visual Studio 封裝管理器中安裝 IronPrint,如下所示。
可以使用以下命令安裝IronPrint:
Install-Package IronPrint
要使用 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
使用Printer.GetPrinterNames
來獲取所有可用的印表機驅動程式。
Console.WriteLine
列印名稱。使用以下程式碼靜默列印文件:
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
執行完成後,文件會如輸出所示被添加到打印隊列中。
使用以下代碼開啟對話框列印文件:
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
:布林值,用於確定是否以灰階列印。 預設為假。
現在讓我們看看一個程式碼範例:
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
程式碼開始於匯入必要的命名空間 IronPrint,該命名空間大概包含了列印所需的類別和方法。
在IronPrintDemo
命名空間內,有一個名為Program的類別宣告為public。
Main 方法作為程式的入口點。
在 Main 方法內:
一個名為printSettings
的PrintSettings
對象被實例化,以配置自訂列印設定。
printSettings
的Dpi屬性設為150,表示每英寸150點的列印解析度。
printSettings
的 NumberOfCopies
屬性設置為 2,這表示將列印兩份相同的文件。
printSettings
的 PaperOrientation
屬性設置為 PaperOrientation.Portrait
,表示文件將以縱向方向列印。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 的可能性,解鎖應用程式中高效文件列印的世界。