如何在 C# 中列印 PDF 文件

C# Print Document Tutorial with IronPrint

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 或圖片直接傳送至印表機。

  1. using NuGet 套件管理員安裝 https://www.nuget.org/packages/IronPrint

    PM > Install-Package IronPrint
  2. 請複製並執行此程式碼片段。

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

    立即透過免費試用,在您的專案中開始使用 IronPrint

    arrow pointer

目錄

列印文件

靜默列印

無需顯示列印對話方塊,即可無縫列印文件。 隨後即可直接在程式碼中進行PRINT設定。

// 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();
    }
}
' Programmatically print a document without showing the print dialog.
' Define your print job and settings here as needed.

Imports System
Imports IronPrint

Public Class SilentPrint
	Public Shared Sub Main()
		' Create a print document instance
		Dim document = New PrintDocument("sample-document.pdf")

		' Initialize a silent print job
		Dim printJob As New PrintJob(document)

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

		' Execute the print job
		printJob.PrintSilently()
	End Sub
End Class
$vbLabelText   $csharpLabel

透過對話方塊列印

在顯示列印設定對話方塊時,啟動列印程序。 這讓使用者能夠以互動方式自訂PRINT選項。

// 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();
    }
}
' Start a print job with user interaction through the print dialog.

Imports System
Imports IronPrint

Public Class DialogPrint
	Public Shared Sub Main()
		' Create a print document instance
		Dim document = New PrintDocument("sample-document.pdf")

		' Initialize a print job with dialog
		Dim printJob As New PrintJob(document)

		' Execute the print job with display of print options dialog
		printJob.PrintWithDialog()
	End Sub
End Class
$vbLabelText   $csharpLabel

套用列印設定

透過程式化方式調整PRINT設定,以滿足特定需求。 本節提供透過程式碼微調列印設定的功能。

// 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.Co/lor,
            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.Co/lor,
            DuplexMode = DuplexMode.OneSided,
            Copies = 2
        };

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

        // Print the document
        printJob.PrintSilently();
    }
}
Imports System
Imports IronPrint

Public Class PrintSettingsExample
    Public Shared Sub Main()
        ' Create a print document instance
        Dim document As New PrintDocument("sample-document.pdf")

        ' Create a print job
        Dim printJob As New PrintJob(document)

        ' Set custom print settings like duplex, color mode, etc.
        Dim settings As New PrintSettings With {
            .ColorMode = ColorMode.Color,
            .DuplexMode = DuplexMode.OneSided,
            .Copies = 2
        }

        ' Apply settings to print job
        printJob.ApplySettings(settings)

        ' Print the document
        printJob.PrintSilently()
    End Sub
End Class
$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);
        }
    }
}
' Retrieve and display a list of printer names available on the system.

Imports System
Imports IronPrint

Public Class PrinterInfo
	Public Shared Sub Main()
		' Get an enumerable list of printer names
		Dim printerNames = PrinterSettings.GetAvailablePrinters()

		' Print each printer name to the console
		Console.WriteLine("Available Printers:")
		For Each name In printerNames
			Console.WriteLine(name)
		Next name
	End Sub
End Class
$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
技術撰稿人

Curtis Chau 擁有卡爾頓大學(Carleton University)的電腦科學學士學位,專精於前端開發,並精通 Node.js、TypeScript、JavaScript 及 React。他熱衷於打造直觀且美觀的用戶介面,喜歡運用現代框架,並創建結構完善、視覺上吸引人的手冊。

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

準備開始了嗎?
Nuget 下載 41,154 | 版本: 2026.5 just released
Still Scrolling Icon

還在往下捲動嗎?

想要快速確認成果嗎? PM > Install-Package IronPrint
執行範例程式,親眼見證您的文件送印。