如何在 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();
    }
}
' 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

帶對話框的列印

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

// 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

應用程式列印設定

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

// 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();
    }
}
' Example code to apply custom print settings programmatically.

Imports System
Imports IronPrint

Public Class PrintSettingsExample
	Public Shared Sub Main()
		' Create a print document instance
		Dim document = 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 = 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 擁有卡爾頓大學計算機科學學士學位,專注於前端開發,擅長於 Node.js、TypeScript、JavaScript 和 React。Curtis 熱衷於創建直觀且美觀的用戶界面,喜歡使用現代框架並打造結構良好、視覺吸引人的手冊。

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

準備好開始了嗎?
Nuget 下載 35,444 | 版本: 2025.12 剛發表