使用 IronPrint 的 C# 列印文件教學

Curtis Chau
Curtis Chau
已更新: 2026年8月2日

IronPrint 是一個功能強大的列印庫,旨在幫助 .NET C# 開發人員將列印功能整合到他們的應用程式中。 IronPrint 具有廣泛的相容性,涵蓋 Windows、macOS、iOS 和 Android 平台,可在各種作業系統上穩定可靠地運作。 無論您是為桌面環境、Apple 的 macOS 生態系統還是 iOS 和 Android 等行動平台創建應用程式,IronPrint 都能簡化列印功能的實現,為 .NET C# 環境中的所有列印需求提供多功能且用戶友好的解決方案。

快速入門:使用 IronPrint 靜默列印文件

只需一行程式碼,您就能列印 - 沒有對話框,沒有麻煩。 使用 IronPrint.Printer.Print(...) 靜默將PDF或圖像直接傳送到打印機,使用默認或自定義設置。

  1. 1使用 IronPrint 安裝 NuGet Package Manager

    PM > Install-Package IronPrint

  2. 2複製並運行這段程式碼。

    IronPrint.Printer.Print("path/to/your/document.pdf");
  3. 3部署到您的生產環境進行測試

    立即在您的項目中開始使用 IronPrint,並使用 免費試用
    arrow pointer

目錄

列印文件

靜默列印

列印文件時不顯示列印對話框。 然後可以直接在程式碼中設定列印參數。

// Programmatically print a document without showing the print dialog.
// Define your print job and settings here as needed.

using System;
using IronPrint;

public class SilentPrint
{
    public static void Main()
    {
        // Apply specific settings as necessary
        // For example: set printer name, copies, etc.
        var settings = new PrintSettings();

        // Print the document without displaying the print dialog
        Printer.Print("sample-document.pdf", settings);
    }
}

帶對話框的列印

在顯示的列印設定對話方塊中啟動列印過程。 這樣一來,用戶可以互動式地自訂列印選項。

// Start a print job with user interaction through the print dialog.

using System;
using IronPrint;

public class DialogPrint
{
    public static void Main()
    {
        var settings = new PrintSettings();

        // Show the print dialog so the user can customize print options
        Printer.ShowPrintDialog("sample-document.pdf", settings);
    }
}

應用程式列印設定

透過程式調整列印設定以滿足特定要求。 本節提供了透過程式碼微調列印配置的功能。

// Example code to apply custom print settings programmatically.

using System;
using IronPrint;

public class PrintSettingsExample
{
    public static void Main()
    {
        // Set custom print settings such as grayscale and number of copies
        var settings = new PrintSettings
        {
            Grayscale = false,
            NumberOfCopies = 2
        };

        // Print the document using the configured settings
        Printer.Print("sample-document.pdf", settings);
    }
}

取得印表機資訊

取得印表機名稱

查看所有可用印表機的清單。 檢索系統中已安裝印表機的名稱,以便用於資訊參考或在應用程式中進行動態印表機選擇。

// Retrieve and display a list of printer names available on the system.

using System;
using IronPrint;

public class PrinterInfo
{
    public static void Main()
    {
        // Get an enumerable list of printer names
        var printerNames = Printer.GetPrinterNames();

        // Print each printer name to the console
        Console.WriteLine("Available Printers:");
        foreach (var name in printerNames)
        {
            Console.WriteLine(name);
        }
    }
}

常見問題

IronPrint支援哪些平台進行文件列印?

IronPrint支援在包括Windows、macOS、iOS和Android等多種平台上的列印,確保在不同操作系統中易於整合。

如何使用IronPrint在不顯示列印對話框的情況下列印文件?

使用IronPrint,您可以使用一行程式碼靜默列印文件:IronPrint.Printer.Print("path/to/your/document.pdf");。這允許您將PDF直接傳送到打印機,而不會有對話框的中斷。

我可以使用IronPrint程式設置列印設定嗎?

可以,您可以在您的C#應用程式碼中使用IronPrint程式設置列印設定,例如設定打印機名稱、副本數量等。

IronPrint是否能夠以互動方式設置列印選項?

IronPrint允許使用者通過利用列印對話框以互動方式開始列印工作,這有助於根據使用者偏好自訂各種列印設定。

如何使用IronPrint檢索可用列印機的名稱?

您可以通過實施Printer.GetPrinterNames()方法使用IronPrint檢索可用列印機列表,此方法提供系統上安裝的印表機名稱列表。

IronPrint能處理哪些文件類型?

IronPrint能處理多種類型的文件,包括PDF、PNG、HTML、TIFF、GIF、JPEG等,使其成為滿足各種列印需求的多功能工具。

IronPrint是否與桌面和行動應用程式兼容?

是的,IronPrint與桌面環境(Windows和macOS)以及行動平台(iOS和Android)兼容,允許在不同設備上實現靈活的列印方案。

IronPrint能否進行灰階列印並程式設置副本數量?

是的,您可以直接在您的C#程式碼中使用IronPrint的PrintSettings類別設定自訂列印設定,例如啟用灰階和指定副本數量。

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

閱讀更多

準備好開始了嗎?

NuGet 下載次數 52,427版本:2026.11剛剛發布