跳過到頁腳內容
使用 IRONPRINT

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

在各種軟體應用程式中,透過網路印表機進行程式化列印是一項常見任務。 無論您是開發桌面應用程式、行動應用程式或 Web 服務,將文件直接傳送到網路印表機的功能都可以大大提高軟體的可用性和效率。 在本文中,我們將探討如何使用 C# 和Iron SoftwareIronPrint在網路印表機上進行列印。

How to Print with Network Printer in 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;

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);
        }
    }
}
$vbLabelText   $csharpLabel

系統.列印

C# 中的 System.Printing 命名空間包含類別和接口,使開發人員能夠以程式設計方式與印表機、列印佇列、列印伺服器和列印作業進行互動。 此命名空間中的一些關鍵類別和介麵包括:

  1. PrintQueue: 表示連接到系統的印表機或列印設備。
  2. PrintServer: 表示網路上的列印伺服器。
  3. PrintSystemJobInfo: 提供有關列印作業的信息,例如其狀態和屬性。
  4. PrintTicket: 表示列印作業的設置,包括紙張尺寸、方向和列印品質。

System.Printing 入門:在深入程式碼之前,讓我們確保對 System.Printing 的列印工作原理有一個基本的了解:

1.選擇:您需要識別代表您要用於列印的印表機的 @@--CODE-650-- @@ 物件。 2.配置:您可以選擇配置 @@--CODE-652 --@@ 對象,以指定列印設置,例如紙張尺寸、方向和份數。 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.");
    }
}
$vbLabelText   $csharpLabel

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);
            }
        }
    }
}
$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");
        }
    }
}
$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");
        }
    }
}
$vbLabelText   $csharpLabel

程式碼解釋

  1. 使用IronPrint中的 ShowPrintDialog,我們可以透過對話方塊列印。
  2. 對話方塊開啟後,選擇要列印文件的印表機。

輸出

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

步驟 6:進階列印設定

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 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);
        }
    }
}
$vbLabelText   $csharpLabel

程式碼解釋

  1. 程式碼首先匯入必要的命名空間IronPrint,其中包含列印所需的類別和方法。
  2. IronPrintDemo 命名空間中,有一個名為 Program 的類,宣告為公用類別。
  3. Main 方法作為程序的入口點。
  4. 在主方法中:
    • 實例化一個名為 printSettingsPrintSettings 對象,用於配置自訂列印設定。
    • Dpi 屬性 printSettings 設定為 150,表示列印解析度為每英吋 150 點。
    • NumberOfCopies 屬性 printSettings 設定為 2,指定將列印該文件的兩個相同副本。
    • PaperOrientation 屬性 printSettings 設定為 PaperOrientation.Portrait,表示該文件將以縱向列印。
  5. 最後,使用參數"sample.pdf"(要列印的文件名稱)和"printSettings"(自訂列印設定)呼叫 Printer.Print 方法。 此方法使用指定的設定來處理列印過程。

輸出

如何在 C# 中使用網路印表機列印:圖 9 - 範例輸出,展示了新增至列印佇列的 PDF 文件

執照

Iron SoftwareIronPrint是一個企業級庫,需要許可證才能運作。 新增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

Curtis Chau
技術作家

Curtis Chau 擁有卡爾頓大學計算機科學學士學位,專注於前端開發,擅長於 Node.js、TypeScript、JavaScript 和 React。Curtis 熱衷於創建直觀且美觀的用戶界面,喜歡使用現代框架並打造結構良好、視覺吸引人的手冊。

除了開發之外,Curtis 對物聯網 (IoT) 有著濃厚的興趣,探索將硬體和軟體結合的創新方式。在閒暇時間,他喜愛遊戲並構建 Discord 機器人,結合科技與創意的樂趣。

鋼鐵支援團隊

我們每週 5 天,每天 24 小時在線上。
聊天
電子郵件
打電話給我