C# Print Document Tutorial with IronPrint
IronPrint 是一款強大的列印程式庫,旨在協助 .NET C# 開發人員將列印功能整合至其應用程式中。 IronPrint 具備廣泛的相容性,涵蓋 Windows、macOS、iOS 及 Android 平台,能在各種作業系統上提供一致且可靠的運作表現。 無論您是為桌面環境、Apple 的 macOS 生態系統,還是 iOS 和 Android 等行動平台開發應用程式,IronPrint 都能簡化列印功能的實作,為您在 .NET C# 環境中的所有列印需求提供多功能且易於使用的解決方案。
快速入門:使用 IronPrint 靜默列印文件只需一行程式碼即可開始列印——無需對話方塊,毫無繁瑣步驟。 使用 IronPrint.Printer.Print(...) 可透過預設或自訂設定,將 PDF 或圖片直接傳送至印表機。
-
1Install IronPrint with NuGet Package Manager
-
2複製並運行這段程式碼片段。
IronPrint.Printer.Print("path/to/your/document.pdf");C# -
3部署以在您的實時環境中測試
今天就開始在您的專案中使用IronPrint,透過免費試用
目錄
列印文件
靜默列印
無需顯示列印對話方塊,即可無縫列印文件。 隨後即可直接在程式碼中進行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();
}
}Imports System
Imports IronPrint
Public Class SilentPrint
Public Shared Sub Main()
' Create a print document instance
Dim document As 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透過對話方塊列印
在顯示列印設定對話方塊時,啟動列印程式。 這讓使用者能夠以互動方式自訂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.
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套用列印設定
透過程式化方式調整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.
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取得印表機資訊
取得印表機名稱
取得所有可用印表機的清單。 檢索系統上已安裝的印表機名稱,供參考之用,或用於應用程式中的動態印表機選取。
// 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常見問題
如何在.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()方法實現。
Can IronPrint perform grayscale printing and adjust the number of copies programmatically?
Yes, you can set custom print settings such as enabling grayscale and specifying the number of copies directly within your C# code using IronPrint's `PrintSettings` class.

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