如何在 C# 中列印 PDF 文檔

使用 IronPrint 進行 C# 文件列印教學

This article was translated from English: Does it need improvement?
Translated
View the article in English

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

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

只需一行程式碼即可列印——無需對話框,輕鬆便捷。 使用IronPrint.Printer.Print(...)可以靜默地將 PDF 或影像直接傳送到印表機,使用預設或自訂設定。

Nuget Icon立即開始使用 NuGet 建立 PDF 檔案:

  1. 使用 NuGet 套件管理器安裝 IronPrint

    PM > Install-Package IronPrint

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

    IronPrint.Printer.Print("path/to/your/document.pdf");
  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()
    {
        // Create a print document instance
        var document = new PrintDocument("sample-document.pdf");

        // Initialize a silent print job
        var printJob = new PrintJob(document);

        // Apply specific settings as necessary
        // For example: set printer name, copies, etc.

        // Execute the print job
        printJob.PrintSilently();
    }
}
// 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()
    {
        // Create a print document instance
        var document = new PrintDocument("sample-document.pdf");

        // Initialize a silent print job
        var printJob = new PrintJob(document);

        // Apply specific settings as necessary
        // For example: set printer name, copies, etc.

        // Execute the print job
        printJob.PrintSilently();
    }
}
$vbLabelText   $csharpLabel

帶對話框的列印

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

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

using System;
using IronPrint;

public class DialogPrint
{
    public static void Main()
    {
        // Create a print document instance
        var document = new PrintDocument("sample-document.pdf");

        // Initialize a print job with dialog
        var printJob = new PrintJob(document);

        // Execute the print job with display of print options dialog
        printJob.PrintWithDialog();
    }
}
// Start a print job with user interaction through the print dialog.

using System;
using IronPrint;

public class DialogPrint
{
    public static void Main()
    {
        // Create a print document instance
        var document = new PrintDocument("sample-document.pdf");

        // Initialize a print job with dialog
        var printJob = new PrintJob(document);

        // Execute the print job with display of print options dialog
        printJob.PrintWithDialog();
    }
}
$vbLabelText   $csharpLabel

應用程式列印設定

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

// Example code to apply custom print settings programmatically.

using System;
using IronPrint;

public class PrintSettingsExample
{
    public static void Main()
    {
        // Create a print document instance
        var document = new PrintDocument("sample-document.pdf");

        // Create a print job
        var printJob = new PrintJob(document);

        // Set custom print settings like duplex, color mode, etc.
        var settings = new PrintSettings
        {
            ColorMode = ColorMode.Color,
            DuplexMode = DuplexMode.OneSided,
            Copies = 2
        };

        // Apply settings to print job
        printJob.ApplySettings(settings);

        // Print the document
        printJob.PrintSilently();
    }
}
// Example code to apply custom print settings programmatically.

using System;
using IronPrint;

public class PrintSettingsExample
{
    public static void Main()
    {
        // Create a print document instance
        var document = new PrintDocument("sample-document.pdf");

        // Create a print job
        var printJob = new PrintJob(document);

        // Set custom print settings like duplex, color mode, etc.
        var settings = new PrintSettings
        {
            ColorMode = ColorMode.Color,
            DuplexMode = DuplexMode.OneSided,
            Copies = 2
        };

        // Apply settings to print job
        printJob.ApplySettings(settings);

        // Print the document
        printJob.PrintSilently();
    }
}
$vbLabelText   $csharpLabel

取得印表機資訊

取得印表機名稱

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

// 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 = PrinterSettings.GetAvailablePrinters();

        // Print each printer name to the console
        Console.WriteLine("Available Printers:");
        foreach (var name in printerNames)
        {
            Console.WriteLine(name);
        }
    }
}
// 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 = PrinterSettings.GetAvailablePrinters();

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

常見問題解答

如何在.NET C#中靜默列印文件?

您可以使用PrintJob實例的PrintSilently()方法來執行列印作業,而無需使用者互動。這樣,就可以以程式方式列印文檔,而無需顯示列印對話框。

在.NET C#中,如何使用列印對話方塊列印文件?

您可以透過在PrintJob實例上使用PrintWithDialog()方法,以使用者互動的方式啟動列印作業。這將顯示列印設定對話框,允許使用者在列印前自訂選項。

是否可以使用 .NET C# 以程式設計方式套用自訂列印設定?

是的,您可以透過建立PrintSettings物件並配置色彩模式、雙面列印模式和份數等屬性,以程式設計方式套用自訂列印設定。然後,這些設定可以套用於PrintJob實例。

如何在.NET C#應用程式中檢索可用的印表機名稱?

您可以使用PrinterSettings.GetAvailablePrinters()方法檢索可用印表機名稱。此方法提供一個可列舉的列表,其中包含系統上已安裝的印表機名稱,可用於選擇或查看相關資訊。

我可以使用 .NET C# 函式庫列印不同格式的文件嗎?

是的,此程式庫支援列印各種文件格式,包括 PDF、PNG、HTML、TIFF、GIF、JPEG、IMAGE 和 BITMAP,從而提供多種文件列印選項。

使用 .NET C# 函式庫列印文件支援哪些平台?

該程式庫支援多種平台,例如 Windows、macOS、iOS 和 Android,確保在這些作業系統上提供一致可靠的列印功能。

在 .NET C# 中,靜默列印與基於對話方塊的列印有何不同?

靜默列印允許使用PrintSilently()方法以程式方式列印文檔,無需使用者互動。對話方塊列印則透過PrintWithDialog()方法顯示列印對話方塊供使用者自訂。

柯蒂斯·週
技術撰稿人

Curtis Chau擁有卡爾頓大學電腦科學學士學位,專長於前端開發,精通Node.js、TypeScript、JavaScript和React。他熱衷於打造直覺美觀的使用者介面,喜歡使用現代框架,並擅長撰寫結構清晰、視覺效果出色的使用者手冊。

除了開發工作之外,柯蒂斯對物聯網 (IoT) 也抱有濃厚的興趣,致力於探索硬體和軟體整合的創新方法。閒暇時,他喜歡玩遊戲和製作 Discord 機器人,將他對科技的熱愛與創造力結合。

準備好開始了嗎?
Nuget 下載 36,035 | 版本: 2025.12 剛剛發布