如何在 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 下載 34,704 | Version: 2025.11 剛發表