如何在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. 使用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.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擁有Carleton大學的電腦科學學士學位,專精於前端開發,擁有Node.js、TypeScript、JavaScript和React的專業知識。Curtis熱衷於建立直觀且美觀的使用者介面,喜愛使用現代框架並建立結構良好、視覺吸引力的手冊。

除了開發,Curtis對物聯網(IoT)有濃厚的興趣,探索創新的方法來整合硬體和軟體。在空閒時間,他喜歡玩遊戲和建立Discord機器人,結合他對技術的熱愛與創造力。

準備好開始了嗎?
Nuget 下載 44,051 | 版本: 2026.7 剛剛發布
Still Scrolling Icon

仍在滾動?

想要快速證明嗎? PM > Install-Package IronPrint
運行範例 看看您的文件如何到達印表機。